Ive set myself a project intented to get me more comftorable with the development process. The game is to control 3 LEDs to run in a loop in as many ways as possible. eg, loop on off, button, potentiometer etc.
In this example im trying to get LED0 to turn on as the potentiometer value drops, LED1 when its not moved, and LED2 as the value rises.
Ive copied my code and the serial monitor readout where you can see that at points 'PotRead' is > 'PotRead2' and vice versa yet the variable 'LED' doesn't update.
'LED' seems to only change when the potentiometer is turned to the lowest value, where it will flash between 2 and 3.
Im entirely self taught so Im sorry if my code is a mess, tell me how to improve. Also I understand that due to noise I dont have a stable idle value but I can address that later.
int LED;
const int LED0 = 2; //pin lables
const int LED1 = 3;
const int LED2 = 4;
void setup() {
Serial.begin(9600);
delay(10);
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(A0, INPUT);
}
void loop() {
//find if the Potentiometer value is rising, dropping or staying the same
int PotRead = analogRead(A0);
delay(100);
int PotRead2 = analogRead(A0);
Serial.print(PotRead);
Serial.print(" , ");
Serial.print(PotRead2);
Serial.print(" , ");
//create a delay if the LED changes, for development purpose
int LEDState = LED;
//using imput decide which LED to activate
if (PotRead > PotRead2) LED = 2;
if (PotRead = PotRead2) LED = 1;
if (PotRead < PotRead2) LED = 0;
if (LEDState != LED) delay(200); //delay on LED state change
Serial.print(LED);
Serial.println("");
switch (LED) //change LEDs state
{
case 0:
digitalWrite(LED0, HIGH); digitalWrite(LED1, LOW); digitalWrite(LED2, LOW);
break;
case 1:
digitalWrite(LED0, LOW); digitalWrite(LED1, HIGH); digitalWrite(LED2, LOW);
break;
case 2:
digitalWrite(LED0, LOW); digitalWrite(LED1, LOW); digitalWrite(LED2, HIGH);
break;
}
}
SM readout when knob is wiggled.
PotRead , PotRead2 , LED
475 , 474 , 2
438 , 354 , 2
304 , 220 , 2
189 , 79 , 2
0 , 5 , 2
83 , 289 , 2
470 , 613 , 2
744 , 750 , 2
630 , 504 , 2
For when turned all the way down
5 , 3 , 2
3 , 0 , 3
7 , 2 , 2
0 , 6 , 2
1 , 0 , 3
Im a beginner at this so if my method here is unconvetional, I'd be happy to hear of other ways.