Using a potentiometer, a pull-up button and LEDs - HELP PLS

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

  if(4 == HIGH)

HIGH is defined as a value of ?? (I let you find out the correct #define by yourself).

do you think the number 4 can ever be equal to HIGH ?

Perhaps this was meant?
if ( digitalRead(4) == HIGH )

Using names vs pin numbers makes things like that more clear.

J-M-L:

  if(4 == HIGH)

HIGH is defined as a value of ?? (I let you find out the correct #define by yourself).

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.

CrossRoads:
Perhaps this was meant?
if ( digitalRead(4) == HIGH )

Using names vs pin numbers makes things like that more clear.

I tried this, using a dummy variable:
int D4Read = digitalRead(4);

and then substituted all "4 == HIGH"s with "D4Read == HIGH"s

No change in state when the button is pressed.

Sorry if I'm missing something guys, literally my second day on this.

Can you post your current code please: easier to see what you did that way, and we can just lift your code straight into the ide.

OP's circuit for convenience

Sure

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

Blinkv2.txt (1.1 KB)

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:

if ( digitalRead(4) == HIGH )

The genius in this forum as truly shown itself today.
You guys are amazing.

Obama out!

Oop I made typo in previous, meant to start:

That trick with int D4Read = digitalRead(4); does NOT DO what you expect.

All good, appreciate the help. Karma for everybody!!

So it's doing what you want? Cool....

Always a good idea for the record to post your working code so the next helpee might draw from the experience.

Working 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(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
}