Lines Of Code I Can't Seem To Fix

I’m using this program to make Arduino act as a switch. I will be using an external pulse on the input (Button 1-4) to activate the the outputs (LED 1-4) So a pulse to say input 4 will turn on output 4, another pulse to input 4 will turn output 4 off. This is suppose to have a De-bouncing section and this is where everything stops. While running the debugger I have all lines fixed except under loop section, the “if” statement and it says <“old_val not declared in this scope” > I have searched on the forum and have not found anything about this. This is basically the first few lessons in learning Arduino tutorial. I have never programmed before so this is kicking my butt.

Thanks
Greg

#define LED_1 2
#define LED_2 3
#define LED_3 4
#define LED_4 5  
// Channel 1-4 Output

#define BUTTON_1 6
#define BUTTON_2 7
#define BUTTON_3 8
#define BUTTON_4 9
// Channel 1-4 Input
 
int val = 0;                      
// val will be used to store the state
// of the input pin 
//int old_val = 0; 
// this variable stores the previous
// value of "val" 

int state = 0;                    
// 0 = LED off and 1 = LED on 

void setup() { 
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT); 
  // tell Arduino LED is an output 

  pinMode(BUTTON_1, INPUT);
  pinMode(BUTTON_2, INPUT);
  pinMode(BUTTON_3, INPUT);
  pinMode(BUTTON_4, INPUT); 
  //tell Arduino BUTTON in an input
} 

void loop(){ 
  val = digitalRead(BUTTON_1); 
  val = digitalRead(BUTTON_2);
  val = digitalRead(BUTTON_3);
  val = digitalRead(BUTTON_4);
// read input value and store it 
// check if there was a transition 

  if ((val == HIGH) && (old_val == LOW)){ 
    state = 1 - state;
    delay(10);
  } 

  old_val = val;                                  
  // val is now old, let's store it 

  if (state == 1) {      
    digitalWrite(LED_1, HIGH); 
    digitalWrite(LED_2, HIGH);
    digitalWrite(LED_3, HIGH);
    digitalWrite(LED_4, HIGH);
    // turn LED ON 
  } 
  else { 
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, LOW);
    // turn LED OFF
   
  } 
}
//int old_val = 0;

That was easy.

AWOL: //int old_val = 0;

That was easy.

So does this replace the if statement?

Greg

// comments out everything following it on a line so

//int old_val = 0; is treated as a COMMENT!

you want

int old_val = 0;

No, your "declaration" of "old_val" is inside a comment, so isn't seen by the compiler. Revoce the "//"

OK I see it now. I read over it several times before I saw it. Its is now complied so I'll load it and see if it works.

Thanks Greg

  val = digitalRead(BUTTON_1); 
  val = digitalRead(BUTTON_2);
  val = digitalRead(BUTTON_3);
  val = digitalRead(BUTTON_4);

You're going have to look at why this doesn't work next.

Been there. Done that!

My son in law is a programmer but I'm trying to do this without his help because he is to busy now without me adding to it. But I did tell him about this and he said their are programs available that will compare 2 different files so You can see the differences. Anyone used these?

Greg

I use BeyondCompare, but there are plenty of others

I use Textpad on Windows (at work), and it has a compare files option which works pretty well. On *nix systems, I just use diff.

Well the program compiled and loaded but does not work and don't have a clue as to why. The only different from the original program is I added more inputs and outputs. Any suggestions?

Thanks Greg

#define LED_1 2
#define LED_2 3
#define LED_3 4
#define LED_4 5  
// Channel 1-4 Output

#define BUTTON_1 6
#define BUTTON_2 7
#define BUTTON_3 8
#define BUTTON_4 9
// Channel 1-4 Input

int val = 0;                      
// val will be used to store the state
// of the input pin 

int old_val = 0; 
// this variable stores the previous
// value of "val"

int state = 0;                    
// 0 = LED off and 1 = LED on 

void setup() { 
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT); 
  // tell Arduino LED is an output 
  
  pinMode(BUTTON_1, INPUT);
  pinMode(BUTTON_2, INPUT);
  pinMode(BUTTON_3, INPUT);
  pinMode(BUTTON_4, INPUT); 
  //tell Arduino BUTTON in an input
} 

void loop(){ 
  val = digitalRead(BUTTON_1); 
  val = digitalRead(BUTTON_2);
  val = digitalRead(BUTTON_3);
  val = digitalRead(BUTTON_4);
// read input value and store it 
// check if there was a transition 

  if ((val == HIGH) && (old_val == LOW)){ 
    state = 1 - state;
    delay(10);
  } 

  //old_val = val;                                  
  // val is now old, let's store it 

  if (state == 1) {      
    digitalWrite(LED_1, HIGH); 
    digitalWrite(LED_2, HIGH);
    digitalWrite(LED_3, HIGH);
    digitalWrite(LED_4, HIGH);
    // turn LED ON 
  } 
  else { 
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, LOW);
    // turn LED OFF
   
  } 
}

See reply #6

After looking at the reference I think its not working because “val=digitalRead(BUTTON_1)” also needs to “digitalWrite(ledPin, val); // sets the LED to the button’s value”

May be

Well after reading further I see digitalWrite will make the pin go high or low.

Greg

Look at it this way

val = 1;
val  = 0;
val = 1;
val  = 0;

What does val contain now?

val contains no value just digitalread the button.

val contains no value just digitalread the button.

Wrong. The digitalRead() function returns a value - the state of the switch (HIGH or LOW).

If you read 4 times, val contains the result of the last read, and only the last read.

PaulS:

val contains no value just digitalread the button.

Wrong. The digitalRead() function returns a value - the state of the switch (HIGH or LOW).

If you read 4 times, val contains the result of the last read, and only the last read.

So how do I get it to read each switch?

Greg

So how do I get it to read each switch?

Just like you are doing. You just need to store the return code in four variables, not one.