+ 2

2 LED's one pot Arduino

Trying to control 2 LED's with one pot, I want one led to turn on only in one certain point of the pot and the other to remain on while in all other positions. So far the best I can come up with is something like this but im not sure how to modify for just two LED's and the specifics above. Am I on the right track or can someone help me please. byte ledPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //pins used for LED's int potPin = 0;  // analog pin used to connect the potentiometer int val;    // variable to read the value from the analog pin void setup() {   for(int i = 0; i < 13; i++){          pinMode(ledPin[i],OUTPUT);   }      } void loop() { val = analogRead(potPin);            // reads the value of the potentiometer (value between 0 and 1023)   for (int i = 0; i < 13; i++) {     if( (i * 102) > val) { digitalWrite(ledPin[i], HIGH);}     else      { digitalWrite(ledPin[i], LOW);}   } }

21st Dec 2019, 7:28 PM
Robert Herron
Robert Herron - avatar
3 Réponses
+ 2
I don't understand very well your post(because the question and code are kinda unrelated to each other). But if you want a led to turn on only in a certain point and the other to be off, but in any other point the first led to be off and the other to be on, no matter the point: if( val == certain_point){ digitalWrite(led_1, HIGH); digitalWrite(led_2, LOW); } else{ digitalWrite(led_1, LOW); digitalWrite(led_2, HIGH); } I don't know if this helps. Feel free to ask me anything about this.
21st Dec 2019, 7:53 PM
molang
molang - avatar
+ 2
I think better use switch statement. In this case you can precisely select your range for led
21st Dec 2019, 7:53 PM
id001x
id001x - avatar
+ 2
Thank you molang that is what I needed. And thank you id001x any info always helps. Thank you everyone.
21st Dec 2019, 9:45 PM
Robert Herron
Robert Herron - avatar