Hi all,
I've hit a wall writing my code and I've tried some trouble shooting techniques but its been while since i have done some coding.
Basically I'm trying to vary what color output i get depending on the position of an analog signal (using a potentiometer)
Currently I'm just testing to make and only checking for 3 colors (GRB), but i will be adding 2 more colors and adding specific patterns to each color. That part is not in the code yet
My issue right now is, my last else statement seems to over ride anything written previous. So blue being my last color range check always comes on regardless of pot position. (green will come on then blue and back again). i tried using a prev color logic but that didn't seem to help. Where it would check to if a color is set then don't set another color unless there is change.
I appreciate everyones advice, thanks in advance.
Here is my code:
#include <Adafruit_NeoPixel.h>
#define Total_LEDS 31
#define POTpin A0 //define pin names
#define PWMpin 3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(Total_LEDS, 6, NEO_GRB + NEO_KHZ800);
int RedVal = 0; // red LED value
int GreenVal = 0; // green LED value
int BlueVal = 0; // blue LED value
int PreviousColour = 0;
int POTVal = 0;
int x;
int MicroOUT = 0;
void setup() {
strip.begin();
strip.show();
pinMode(POTpin, INPUT);
pinMode(PWMpin, OUTPUT);
Serial.begin(9600);
Serial.println("My Sketch has started");
}
void loop() {
while(1)
{
delay(1000); //PAUSE 1 SECOND
POTVal = analogRead(POTpin);
Serial.println("POTVal = ");
Serial.println(POTVal);
MicroOUT = map(POTVal, 0, 1023, 0, 255);
Serial.println("MicroOUT = ");
Serial.println(MicroOUT);
//This is important
if (MicroOUT >= 0 & MicroOUT <= 100){ // range read from POT to show (GREEN)
if (PreviousColour != 1){
GreenVal = 200; // red LED value
RedVal = 0; // green LED value
BlueVal = 0; // blue LED value
Serial.println("RedVal = ");
Serial.println(RedVal);
Serial.println("GreenVal = ");
Serial.println(GreenVal);
Serial.println("BlueVal = ");
Serial.println(BlueVal);
for (x = 0; x < Total_LEDS; x++) {
Serial.println("Green for Loop Reached = ");
strip.setPixelColor(x, RedVal, GreenVal, BlueVal);
strip.show();
}
}
PreviousColour = 1;
delay(2000);
}
else if (MicroOUT >= 101 & MicroOUT <= 150){ // range read from POT to show (AMBER)
if (PreviousColour != 2){
GreenVal = 0; // red LED value
RedVal = 200; // green LED value
BlueVal = 0; // blue LED value
Serial.println("RedVal = ");
Serial.println(RedVal);
Serial.println("GreenVal = ");
Serial.println(GreenVal);
Serial.println("BlueVal = ");
Serial.println(BlueVal);
for (x = 0; x < Total_LEDS; x++) {
Serial.println("Red for Loop Reached = ");
strip.setPixelColor(x, RedVal, GreenVal, BlueVal);
strip.show();
}
}
PreviousColour = 2;
delay(2000);
}
else (MicroOUT >= 151 & MicroOUT <= 200);{ // range read from POT to show (BLUE)
if (PreviousColour != 3){
GreenVal = 0; // red LED value
RedVal = 0; // green LED value
BlueVal = 200; // blue LED value
Serial.println("RedVal = ");
Serial.println(RedVal);
Serial.println("GreenVal = ");
Serial.println(GreenVal);
Serial.println("BlueVal = ");
Serial.println(BlueVal);
for (x = 0; x < Total_LEDS; x++) {
Serial.println("Blue for Loop Reached = ");
strip.setPixelColor(x, GreenVal, RedVal, BlueVal);
strip.show();
}
}
PreviousColour = 3;
delay(2000);
}
}
}
I forgot to mention hardware I'm using.
3-wire LED strip from Neopixel
Arduino Uno board
5VDC PS
10kOhm Pot