I want to turn on three LEDS at different moments depending on the position of a potentiometer. I am new to this. LEDs are not acting like I want them to.
int photoresistor = A0; //variable for storing the photoresistor value
int potentiometer = A1; //this variable will hold a value based on the position of the knob
int threshold = 700; //if the photoresistor reading is lower than this value the light will turn on
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
//set the LED pins to output
}
void loop() {
photoresistor = analogRead(A0); //read the value of the photoresistor
potentiometer = analogRead(A1);
Serial.print("Photoresistor value:");
Serial.print(photoresistor);
Serial.print(" Potentiometer value:");
Serial.println(potentiometer);
if (photoresistor < threshold) {
if (potentiometer > 0 && potentiometer <= 150)
analogWrite(13, HIGH);
analogWrite(12, LOW);
analogWrite(11, LOW); // Turn off the LED
// Turn off the LED
if (potentiometer > 150 && potentiometer <= 300)
analogWrite(13, LOW); // Turn off the LED
analogWrite(12, HIGH);
analogWrite(12, LOW); // Turn off the LED
if (potentiometer > 300 && potentiometer <= 450)
analogWrite(13, LOW); // Turn off the LED
analogWrite(12, LOW); // Turn off the LED
analogWrite(11, HIGH);
if (potentiometer > 450 && potentiometer <= 600)
analogWrite(13, HIGH);
analogWrite(12, HIGH);
analogWrite(11, HIGH);
if (potentiometer > 600 && potentiometer <= 750)
analogWrite(13, LOW);
analogWrite(12, LOW);
analogWrite(11, LOW);
if (potentiometer > 750 && potentiometer <= 900)
analogWrite(11, LOW );
analogWrite(12, HIGH); // Turn off the LED
analogWrite(13, LOW);
if (potentiometer > 900)
analogWrite(11, HIGH);
analogWrite(12, HIGH);
analogWrite(13, HIGH);
}
}
if (potentiometer > 0 && potentiometer <= 150)
analogWrite(13, HIGH);
analogWrite(12, LOW);
analogWrite(11, LOW); // Turn off the LED
First, I think you need some {} braces/brackets there. Otherwise only the first analogWrite() depends on the "if" and the other 2 execute no matter what the potentiometer reading.
Second, with analogWrite(), you need to give a value between 0 and 255. With digitalWrite() you need to give HIGH or LOW.
So:
if (potentiometer > 0 && potentiometer <= 150) {
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW); // Turn off the LED
}
int photoresistor = A0; //variable for storing the photoresistor value
int potentiometer = A1; //this variable will hold a value based on the position of the knob
int threshold = 700; //if the photoresistor reading is lower than this value the light will turn on
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
//set the LED pins to output
}
void loop() {
photoresistor = analogRead(A0); //read the value of the photoresistor
potentiometer = analogRead(A1);
Serial.print("Photoresistor value:");
Serial.print(photoresistor);
Serial.print(" Potentiometer value:");
Serial.println(potentiometer);
if (photoresistor < threshold) {
if (potentiometer > 0 && potentiometer <= 150)
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW); // Turn off the LED
// Turn off the LED
}
if (potentiometer > 150 && potentiometer <= 300)
{
digitalWrite(13, LOW); // Turn off the LED
digitalWrite(12, HIGH);
digitalWrite(12, LOW); // Turn off the LED
}
if (potentiometer > 300 && potentiometer <= 450)
{
digitalWrite(13, LOW); // Turn off the LED
digitalWrite(12, LOW); // Turn off the LED
digitalWrite(11, HIGH);
}
if (potentiometer > 450 && potentiometer <= 600)
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
}
if (potentiometer > 600 && potentiometer <= 750)
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
}
if (potentiometer > 750 && potentiometer <= 900)
{
digitalWrite(11, LOW );
digitalWrite(12, HIGH); // Turn off the LED
digitalWrite(13, LOW);
}
if (potentiometer > 900)
{
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
}
}
}