0

Every time I set the same color to my NeoPixel strip it chan
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Every time I set the same color to my NeoPixel strip it chan

by BeastCoder on Fri Aug 30, 2019 12:11 pm

I have some code that is getting numbers from processing, to put onto my Neopixel. I am using USB to serial cord that you would use to code the Arduino to send the data from processing. The Arduino code is:

Code: Select all | TOGGLE FULL SIZE
#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>

const int rx = 2;
const int tx = 3;
SoftwareSerial Blu (rx, tx);

const int pin = 6;
const int numOfPixels = 30;

int r = 0;
int prevr = 0;
int g = 0;
int prevg = 0;
int b = 0;
int prevb = 0;

Adafruit_NeoPixel pixels(numOfPixels, pin, NEO_GRB + NEO_KHZ800);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Blu.begin(9600);
  pixels.begin();
  pixels.show();
}

void loop() {

  pixels.show();

  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {

    pixels.clear();

    prevr = r;
    prevg = g;
    prevb = b;

    r = Serial.parseInt();
    g = Serial.parseInt();
    b = Serial.parseInt();

    delay(1000);
    setColor(r, g, b);

  }

}

void setColor(int red, int green, int blue) {
  pixels.fill(pixels.Color(red, green, blue), 0, numOfPixels);

}


The processing code is:

Code: Select all | TOGGLE FULL SIZE
import processing.serial.*;
import controlP5.*;

Serial myPort;
ControlP5 cp5;

int slider1 = 0;
int slider2 = 0;
int slider3 = 0;

void setup() {


  size(800, 800);
  cp5 = new ControlP5(this);
  PFont roboto = createFont("Roboto-Bold.ttf", 1, true);
  ControlFont font = new ControlFont(roboto, 28);
  Controller Aslider1 = cp5.addSlider("slider1")
    .setPosition(85, 100)
    .setCaptionLabel("Red")
    .setRange(0, 255)
    .setWidth(191)
    .setHeight(50);
  cp5.getController("slider1").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
  cp5.getController("slider1").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);

  Controller Aslider2 = cp5.addSlider("slider2")
    .setPosition(301, 100)
    .setCaptionLabel("Green")
    .setRange(0, 255)
    .setWidth(191)
    .setHeight(50);
  cp5.getController("slider2").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
  cp5.getController("slider2").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);

  Controller Aslider3 = cp5.addSlider("slider3")
    .setPosition(517, 100)
    .setCaptionLabel("Blue")
    .setRange(0, 255)
    .setWidth(191)
    .setHeight(50);
  cp5.getController("slider3").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
  cp5.getController("slider3").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);


  myPort = new Serial(this, "COM3", 9600);
}


void draw() {
  background(slider1, slider2, slider3);

  fill(255);
  stroke(1);
  rectMode(CENTER);
  rect(width/2, 350, 200, 75);

  fill(0);
  textSize(32);
  text("Send", width/2 - textWidth("Send") / 2, 350 + 10);

  if (mouseX > width/2 - 100 && mouseX < width/2 + 100 && mouseY > 350 - 75/2 && mouseY < 350 + 75/2) {
    if (mousePressed) {
      fill(100);
      stroke(1);
      rectMode(CENTER);
      rect(width/2, 350, 200, 75);

      fill(0);
      textSize(32);
      text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
    } else {
      fill(170);
      stroke(1);
      rectMode(CENTER);
      rect(width/2, 350, 200, 75);

      fill(0);
      textSize(32);
      text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
    }
  }
}

void mouseReleased() {
  if (mouseX > width/2 - 100 && mouseX < width/2 + 100 && mouseY > 350 - 75/2 && mouseY < 350 + 75/2) {
    fill(100);
    stroke(1);
    rectMode(CENTER);
    rect(width/2, 350, 200, 75);

    fill(0);
    textSize(32);
    text("Send", width/2 - textWidth("Send") / 2, 350 + 10);

    myPort.write(str(slider1) + " " + str(slider2) + " " + str(slider3) + " ");
    println(str(slider1) + " " + str(slider2) + " " + str(slider3));
  }
}


If I click the send button twice on processing (without changing the set color), the strip changes color...

Could anyone help me out with this? Thanks!

EDIT: This is very important! I think it has to do with the data not coming in right, because when I keep filling the color without Serial, it works.

BeastCoder
 
Posts: 1
Joined: Fri Aug 30, 2019 12:07 pm

Re: Every time I set the same color to my NeoPixel strip it

by adafruit_support_mike on Thu Sep 05, 2019 4:27 am

Throw in some diagnostic Serial.print() lines at both ends to see when the Processing code is sending information to the microcontroller, and what it thinks it's sending. Then compare that to what the microcontroller says it sees as input.

adafruit_support_mike
 
Posts: 58898
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.