NeoPixel not working like intended

I have written the following Arduino code for a project, but the NeoPixel is not updating. It is supposed to update with the emotionCode int, which is received from Processing, but the pixel doesn't light up at all. I'm guessing it somehow stays at 0, but I can't check that when also using the serial port for the data transfer between Processing and Arduino.
When I manually change that int and comment out the serial read for that int only, it does work, possibly because it doesn't get updated.

It would be great if someone could help me out!

#include <Servo.h>
#include <Adafruit_NeoPixel.h>

#define PIN 10
#define NUMPIXELS 1

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

Servo noseServo;

char val;

int emotionCode = 1;
int nosePos;
int face;

const int ledGreen = 12;
const int ledRed = 13;

void setup() {
  Serial.begin(9600);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledRed, OUTPUT);
  noseServo.attach(9);

  pixels.begin();
  pixels.setBrightness(50);
  pixels.show();
}

void loop() {
  while (Serial.available()) {
    val = Serial.read();
    if (val == 'a') {
      emotionCode = Serial.read();
    }
    if (val == 'b') {
      nosePos = Serial.read();
    }
    if (val == 'c') {
      face = Serial.read();
    }
  }

  if (face == 1) {
    digitalWrite(ledGreen, HIGH);
    digitalWrite(ledRed, LOW);
  } else if (face == 0) {
    digitalWrite(ledGreen, LOW);
    digitalWrite(ledRed, HIGH);
  }

  noseServo.write(nosePos);

  if (emotionCode == 1) {
    pixels.setPixelColor(0, pixels.Color(245, 209, 66));
    pixels.show();
  } else if (emotionCode == 2) {
    pixels.setPixelColor(0, pixels.Color(0, 255, 0));
    pixels.show();
  } else if (emotionCode == 3) {
    pixels.setPixelColor(0, pixels.Color(0, 0, 255));
    pixels.show();
  } else if (emotionCode == 4) {
    pixels.setPixelColor(0, pixels.Color(255, 0, 0));
    pixels.show();
  } else if (emotionCode == 5) {
    pixels.setPixelColor(0, pixels.Color(252, 0, 227));
    pixels.show();
  } else if (emotionCode == 6) {
    pixels.setPixelColor(0, pixels.Color(0, 225, 255));
    pixels.show();
  } else if (emotionCode == 7) {
    pixels.setPixelColor(0, pixels.Color(255, 0, 115));
    pixels.show();
  } else {
    pixels.setBrightness(0);
    pixels.clear();
  }

  Serial.print(emotionCode);
  Serial.print(nosePos);
  Serial.print(face);
  Serial.println();
}

You didn't check to see if there's anything to read.

Thanks for helping me! I actually figured that the led will do exactly what I want if I remove the last else condition, so it seems the problem was there, it now keeps updating with the integer.

Ok, so when it breaks again, just remember to make sure there's something to read before reading it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.