Hello all,
I cannot seem to get my code to write. I am trying to have 4 LEDs, in a straight line connected each to an analog output. The potentiometer is a connected to a 5 Volt power source. I would like to have four categories of 0-1.25V, 1.25 to 2.5 Volt, 2.5 to 3.75 volt, and 3.75 to 5 Volt and I am trying to write the program so that the brightness of the LED changes with respect to voltage. For each 1.25V interval, the brightness of one LED will be proportional to the output voltage of the connected potentiometer. Below is my code that is not working:
[color=#16192b]int LED1 = 9; // LED 1 connected to digital pin 9[/color]
[color=#16192b]int LED2 = 10; //LED 2 connected to digital pin 10[/color]
[color=#16192b]int LED3 = 11; //LED 2 connected to digital pin 11[/color]
[color=#16192b]int LED4 = 12; //LED 2 connected to digital pin 12[/color]
[color=#16192b]int analogPin = 3; // potentiometer connected to analog pin 3[/color]
[color=#16192b]int val = 0; // variable to store the read value[/color]
[color=#16192b] [/color]
[color=#16192b]void setup()[/color]
[color=#16192b]{[/color]
[color=#16192b] pinMode(LED1, OUTPUT); // sets the pin as output[/color]
[color=#16192b] pinMode(LED2, OUTPUT); // sets the pin as output[/color]
[color=#16192b] pinMode(LED3, OUTPUT); // sets the pin as output[/color]
[color=#16192b] pinMode(LED4, OUTPUT); // sets the pin as output[/color]
[color=#16192b]}[/color]
[color=#16192b] [/color]
[color=#16192b]void loop()[/color]
[color=#16192b]{[/color]
[color=#16192b] val = analogRead(analogPin); // read the input pin[/color]
[color=#16192b] float voltage = val*(5.0/ 1023.0) //convert it so we can make our switch case[/color]
[color=#16192b] if(voltage < 1.25)[/color]
[color=#16192b] {[/color]
[color=#16192b] analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] [/color]
[color=#16192b] [/color]
[color=#16192b] }[/color]
[color=#16192b] else[/color]
[color=#16192b] {[/color]
[color=#16192b] if (( x > 1.25)&&(x < 2.5) )[/color]
[color=#16192b] {[/color]
[color=#16192b] analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] [/color]
[color=#16192b] }[/color]
[color=#16192b] else[/color]
[color=#16192b] {[/color]
[color=#16192b] if (( x > 2.5)&&(x < 3.75) )[/color]
[color=#16192b] {[/color]
[color=#16192b] analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] }[/color]
[color=#16192b] else[/color]
[color=#16192b] if (( x > 3.75)&&(x < 5) )[/color]
[color=#16192b] {[/color]
[color=#16192b] analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] }[/color]
}
I am not sure where I am going wrong. Thanks!