[SOLVED] Two LEDs problem

Hi! I'm trying to make a project which contains two LEDs, two switches, and one potentiometer with Arduino UNO R3. By pressing the switches I could switch between two LEDs, and I can adjust the intensity of the LED by adjusting the potentiometer.
I succeeded by using S4A but failed to convert it into code. Here's my approach, could anyone helps me, I really appreciate it.

int led1 = 5;
int led2 = 6;
int switchStatus1 = 2;
int switchStatus2 = 3;
int potentiometer = A0;

int readValue;
int writeValue;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(switchStatus1, INPUT);
  pinMode(switchStatus2, INPUT);
  pinMode(potentiometer, INPUT);
}

void loop(){
  analogWrite(led1, 255);
  if (switchStatus1 == HIGH){
    while (switchStatus2 == HIGH){
      readValue = analogRead(potentiometer);
      writeValue = (255./1023.)*readValue;
      analogWrite(led1, writeValue);
      analogWrite(led2, 0);
    }
  }
  if (switchStatus2 == HIGH){
    while (switchStatus1 == HIGH){
      readValue = analogRead(potentiometer);
      writeValue = (255./1023.)*readValue;
      analogWrite(led2, writeValue);
      analogWrite(led1, 0);
    }
  }
}

01.PNG

due_LED.ino (801 Bytes)

Put this in setup():
analogWrite(led1, 255);
analogWrite(led2, 0);

Change this
writeValue = (255./1023.)*readValue;
to
writeValue = readValue>>2; // divide by 4, so 0-1023 read becomes a 0-255 write.

CrossRoads:
Put this in setup():
analogWrite(led1, 255);
analogWrite(led2, 0);

Change this
writeValue = (255./1023.)*readValue;
to
writeValue = readValue>>2; // divide by 4, so 0-1023 read becomes a 0-255 write.

Hi, CrossRoads,
I adjusted the code as you suggested. Still, it doesn't work. Did I do something wrong? Thanks!

int led1 = 5;
int led2 = 6;
int switchStatus1 = 2;
int switchStatus2 = 3;
int potentiometer = A0;

int readValue;
int writeValue;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(switchStatus1, INPUT);
  pinMode(switchStatus2, INPUT);
  pinMode(potentiometer, INPUT);
  analogWrite(led1, 255);
  analogWrite(led2, 0);
}

void loop(){
  if (switchStatus1 == HIGH){
    while (switchStatus2 == HIGH){
      readValue = analogRead(potentiometer);
      writeValue = readValue>>2;
      analogWrite(led1, writeValue);
      analogWrite(led2, 0);
    }
  }
  if (switchStatus2 == HIGH){
    while (switchStatus1 == HIGH){
      readValue = analogRead(potentiometer);
      writeValue = readValue>>2;
      analogWrite(led2, writeValue);
      analogWrite(led1, 0);
    }
  }
}

Here's a 15s short clip of my goal by using S4A:https://youtu.be/qn51Vw0KJM0

Forget S4A to blink an LED, it's just crap. Your wiring is wrong somewhere.

Use this tutorial to connect 1 LED. Once you have learned how it works for one LED, you should be able to blink 2 LEDs with the same potentiometer plus 2 switches. Although it is for a UNO, it's the same for a DUE:

Beware: the DUE is only 3.3V compliant. Do not hook 5V on an input pin.

Thank you CrossRoads and ard_newbie for helping. I finally figured out how to solve the problem. I should set the expression of both while loops to LOW instead of HIGH. Here's my code. Thanks for helping!

int led1 = 5;
int led2 = 6;
int potentiometer = A0;

int readValue;
int writeValue;

void setup(){
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(potentiometer, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

}

void intensity(){
  readValue = analogRead(potentiometer);
  writeValue = (255./1023.)*readValue;
}

void loop(){
  intensity();
  analogWrite(led1, writeValue);
  analogWrite(led2, 0);
  if (digitalRead(2) == HIGH){
    while(digitalRead(3) == LOW){
      intensity();
      analogWrite(led1, writeValue);
      analogWrite(led2, 0);
    }
  }
  if (digitalRead(3) == HIGH){
    while(digitalRead(2) == LOW){
      intensity();
      analogWrite(led1, 0);
      analogWrite(led2, writeValue);
    }
  }
}

two_LED.ino (736 Bytes)