Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.4 kB
1
Indexable
Never
#include <Adafruit_NeoPixel.h>

#define PIN 28
#define NUMPIXELS 1

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

char receivedChar;
bool newData = false;

int redValue = 0;
int greenValue = 0;
int blueValue = 0;

void setup() {
  Serial.begin(9600);
  pixels.begin(); 
  pixels.show(); 
}

void loop() {
  receiveData();
  if (newData) {
    processCommand();
    newData = false;
  }
}

void receiveData() {
  if (Serial.available() > 0) {
    receivedChar = Serial.read();
    newData = true;
  }
}

void processCommand() {
  switch (receivedChar) {
    case 'R':
      setColor(255, 0, 0); 
      break;
    case 'r':
      setColor(0, greenValue, blueValue); 
      break;
    case 'G':
      setColor(0, 255, 0);
      break;
    case 'g':
      setColor(redValue, 0, blueValue);
      break;
    case 'B':
      setColor(0, 0, 255); 
      break;
    case 'b':
      setColor(redValue, greenValue, 0); 
      break;
    case 'W':
      setColor(255, 255, 255);
      break;
    case 'w':
      setColor(0, 0, 0); 
      break;
    default:
      break;
  }
}

void setColor(int red, int green, int blue) {
  redValue = red;
  greenValue = green;
  blueValue = blue;
  pixels.setPixelColor(0, pixels.Color(redValue, greenValue, blueValue));
  pixels.show();
}
Leave a Comment