3 LEDs on/off dependent on potentiometer position. Code and chart provided

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);
  }
}

pot___3_LEDs.ino (1.91 KB)

Please read the forum guide in the sticky post at the top of the forum section, then modify your post above and add the code tags.

I am new to this.

You are not new to this forum. You were asked, twice, to do this in your other topic, and you did not do it. Now you are breaking forum rules again.

I can see exactly why your code is not working as expected. Follow forum rules and I will tell you.

Is that the correct format for adding code?

Yes! Thankyou. +1 karma

Here's the problems I spotted:

    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
    }

This works beautifully!

I stuck with digitalWrite and added the brackets!

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);
  }

  }
}

Hi,
In the IDE, press CTRL-T, it will format your code indents to make it easier to read.

Thanks.. Tom... :slight_smile: