So hey guys, I'm trying to get a code working with my Nano which does 2 things:
When I rotate the potentiometer, my 2 LEDs blink at variable rates (alternating blinks)
When I push the button down, my 2 LEDs synchronise their blinks
(I have attached my circuit diagrams for you to relate to)
I tried two ways as you can see.
This is my Primary Code:
int VR = A1; //defining variable VR as the Analog Pin 1
int SV = 0; //defining variable SV as an arbitrary value with the initial value "0"
void setup() {
pinMode(13, OUTPUT); //defining Digital Pin 13 as an Output
pinMode(3, OUTPUT); //defining Digital Pin 3 as an Output
}
void loop() {
SV = 2*analogRead(VR); //read Variable Resistor's Value
digitalWrite(13, HIGH); // turn the RED LED on
//If statement for Blue LED to sync with Red LED if button is pressed
if(4 == HIGH)
{
digitalWrite(3, HIGH); // turn the BLUE LED off
}
else
{
digitalWrite(3, LOW); // turn the BLUE LED on
}
//end If statement
delay(SV); // wait as per reading
digitalWrite(13, LOW); // turn the RED LED off
//If statement for Blue LED to sync with Red LED if button is pressed
if(4 == HIGH)
{
digitalWrite(3, LOW); // turn the BLUE LED on
}
else
{
digitalWrite(3, HIGH); // turn the BLUE LED off
}
//end If statement
delay(SV); // wait as per reading
}
Alternative method:
int VR = A1; //defining variable VR as the Analog Pin 1
int SV = 0; //defining variable SV as an arbitrary value with the initial value "0"
void setup() {
pinMode(13, OUTPUT); //defining Digital Pin 13 as an Output
pinMode(3, OUTPUT); //defining Digital Pin 3 as an Output
}
void loop() {
if (4 == LOW)
{
SV = 2*analogRead(VR); //read Variable Resistor's Value
digitalWrite(13, HIGH); // turn the RED LED on
digitalWrite(3, LOW); // turn the BLUE LED off
delay(SV); // wait as per reading
digitalWrite(13, LOW); // turn the RED LED off
digitalWrite(3, HIGH); // turn the BLUE LED on
delay(SV); // wait as per reading
}
else
{
SV = 2*analogRead(VR); //read Variable Resistor's Value
digitalWrite(13, HIGH); // turn the RED LED on
digitalWrite(3, HIGH); // turn the BLUE LED on
delay(SV); // wait as per reading
digitalWrite(13, LOW); // turn the RED LED off
digitalWrite(3, LOW); // turn the BLUE LED off
delay(SV); // wait as per reading
}
}
do you think the number 4 can ever be equal to HIGH ?
Umm by 4 I meant the value into the Digital Pin 4 which would be HIGH if there are 5V going in there directly and LOW if it has been grounded via a resistor.
I think CrossRoads might be onto something there so I'll give that a go.
Instead of synchronising the blinking, I now have the blinks at half speed (ie. delay twice as large) when I press the button down.
Current code I'm trying to implement:
int VR = A1; //defining variable VR as the Analog Pin 1
int SV = 0; //defining variable SV as an arbitrary value with the initial value "0"
int D4Read = digitalRead(4);
void setup() {
pinMode(13, OUTPUT); //defining Digital Pin 13 as an Output
pinMode(3, OUTPUT); //defining Digital Pin 3 as an Output
void loop() {
SV = 2*analogRead(VR); //read Variable Resistor's Value
digitalWrite(13, HIGH); // turn the RED LED on
//If statement for Blue LED to sync with Red LED if button is pressed
if(D4Read == HIGH)
{
digitalWrite(3, HIGH); // turn the BLUE LED off
}
else
{
digitalWrite(3, LOW); // turn the BLUE LED on
}
//end If statement
delay(SV); // wait as per reading
digitalWrite(13, LOW); // turn the RED LED off
//If statement for Blue LED to sync with Red LED if button is pressed
if(D4Read == HIGH)
{
digitalWrite(3, LOW); // turn the BLUE LED on
}
else
{
digitalWrite(3, HIGH); // turn the BLUE LED off
}
//end If statement
delay(SV); // wait as per reading
}
Alternative code:
int VR = A1; //defining variable VR as the Analog Pin 1
int SV = 0; //defining variable SV as an arbitrary value with the initial value "0"
int D4Read = digitalRead(4);
void setup() {
pinMode(13, OUTPUT); //defining Digital Pin 13 as an Output
pinMode(3, OUTPUT); //defining Digital Pin 3 as an Output
}
void loop() {
if (D4Read == LOW)
{
SV = 2*analogRead(VR); //read Variable Resistor's Value
digitalWrite(13, HIGH); // turn the RED LED on
digitalWrite(3, LOW); // turn the BLUE LED off
delay(SV); // wait as per reading
digitalWrite(13, LOW); // turn the RED LED off
digitalWrite(3, HIGH); // turn the BLUE LED on
delay(SV); // wait as per reading
}
else
{
SV = 2*analogRead(VR); //read Variable Resistor's Value
digitalWrite(13, HIGH); // turn the RED LED on
digitalWrite(3, HIGH); // turn the BLUE LED on
delay(SV); // wait as per reading
digitalWrite(13, LOW); // turn the RED LED off
digitalWrite(3, LOW); // turn the BLUE LED off
delay(SV); // wait as per reading
}
}
That trick with int D4Read = digitalRead(4); does edit NOT DO what you expect.
It assigns whatever the value of the read is at the time you do that declare, then just uses that value every time you do the if (D4Read == HIGH), without updating the read.
You actually need to do it the way CrossRoads said:
int VR = A1; //defining variable VR as the Analog Pin 1
int SV = 0; //defining variable SV as an arbitrary value with the initial value "0"
void setup() {
pinMode(13, OUTPUT); //defining Digital Pin 13 as an Output
pinMode(3, OUTPUT); //defining Digital Pin 3 as an Output
void loop() {
SV = 2*analogRead(VR); //read Variable Resistor's Value
digitalWrite(13, HIGH); // turn the RED LED on
//If statement for Blue LED to sync with Red LED if button is pressed
if(digitalRead (4) == HIGH)
{
digitalWrite(3, HIGH); // turn the BLUE LED off
}
else
{
digitalWrite(3, LOW); // turn the BLUE LED on
}
//end If statement
delay(SV); // wait as per reading
digitalWrite(13, LOW); // turn the RED LED off
//If statement for Blue LED to sync with Red LED if button is pressed
if(digitalRead (4) == HIGH)
{
digitalWrite(3, LOW); // turn the BLUE LED on
}
else
{
digitalWrite(3, HIGH); // turn the BLUE LED off
}
//end If statement
delay(SV); // wait as per reading
}