Hello,
I'm new to Arduino and currently doing some of the courses in the "Student Kit - Lesson Dimmer Switch - Code Creation – Blinking Indicator" “Lesson4_V4”.
In this activity, 'else if conditional structure' is introduced, and I have tried everything to get this to run on my Arduino R4 UNO WIFI, but I just can't get 'digitalWrite()' to work properly with the following code:
int readValue = 0;
int writeValue = 0;
void setup() {
pinMode(9, OUTPUT); // declare the first LED pin as output
pinMode(10, OUTPUT); // declare the second LED pin as output
pinMode(11, OUTPUT); // declare the third LED pin as output
Serial.begin(9600);
}
void loop() {
readValue = analogRead(A0); // store the value from the potentiometer
writeValue = readValue / 4; // divide the readValue by 4 and store as the writeValue
if (readValue < 300) { // if the value from the potentiometer is less than 300 then:
digitalWrite(9,LOW); // turn off the first LED
digitalWrite(10,LOW); // turn off the second LED
digitalWrite(11,LOW); // turn off the third LED
}
else if (readValue < 600) { //if the value from the potentiometer is less than 600 then:
analogWrite(9, writeValue); // turn on the first LED
digitalWrite(10,LOW); // turn off the second LED
digitalWrite(11,LOW); // turn off the third LED
}
else if (readValue < 900) { //if the value from the potentiometer is less than 900 then:
analogWrite(9, writeValue); // turn on the first LED
analogWrite(10, writeValue); // turn on the second LED
digitalWrite(11,LOW); // turn off the third LED
}
else {
analogWrite(9, writeValue); // turn on the first LED
analogWrite(10, writeValue); // turn on the second LED
analogWrite(11, writeValue); // turn on the third LED
}
Serial.print(readValue);
Serial.print(" : ");
Serial.println(writeValue);
delay(100);
}
With this code the Arduino lights up the LED's in sequences according to my 'if' and 'else if' statements for 300, 600 and 900 turn on 1st, 2nd and 3rd LED. But when the potentiometer is dialed down they remain ON.
After some troubleshooting I went to tinkercad to set up the same configuration on a virtual Arduino UNO R3, with the exact same wiring and code it runs without any issues.
So I adjusted my code for my R4 UNO by instead using analogWrite(PIN,0); instead of digitalWrite(PIN,LOW);
int readValue = 0;
int writeValue = 0;
void setup() {
pinMode(9, OUTPUT); // declare the first LED pin as output
pinMode(10, OUTPUT); // declare the second LED pin as output
pinMode(11, OUTPUT); // declare the third LED pin as output
Serial.begin(9600);
}
void loop() {
readValue = analogRead(A0); // store the value from the potentiometer
writeValue = readValue / 4; // divide the readValue by 4 and store as the writeValue
if (readValue < 300) { // if the value from the potentiometer is less than 300 then:
analogWrite(9,0); // turn off the first LED
analogWrite(10,0); // turn off the second LED
analogWrite(11,0); // turn off the third LED
}
else if (readValue < 600) { //if the value from the potentiometer is less than 600 then:
analogWrite(9, writeValue); // turn on the first LED
analogWrite(10,0); // turn off the second LED
analogWrite(11,0); // turn off the third LED
}
else if (readValue < 900) { //if the value from the potentiometer is less than 900 then:
analogWrite(9, writeValue); // turn on the first LED
analogWrite(10, writeValue); // turn on the second LED
analogWrite(11,0); // turn off the third LED
}
else {
analogWrite(9, writeValue); // turn on the first LED
analogWrite(10, writeValue); // turn on the second LED
analogWrite(11, writeValue); // turn on the third LED
}
Serial.print(readValue);
Serial.print(" : ");
Serial.println(writeValue);
delay(100);
}
With this updated code it runs without any issues on my R4 UNO WIFI and on the virtual R3 UNO. Both turning LED's ON for respective values and turning them OFF. But I still don't understand why the digitalWrite(PIN,LOW); does work on R3 UNO but not on R4 UNO WIFI?