buttonState1' was not declared in this scope

hello everyone ,

i am trying to make a program just i getting a error . And i dont know why

#include <Arduino.h>


int greenLightPin = 6;
int redLightPin = 4;
int PinButton1 = 7;
int PinButton2 = 8;


void knipperled(int pinnummer, int aantalkeer,int delaytijd){    // functie  ===>(pinnummer variable) 
  for (int i=0;  i <= aantalkeer; i++){
    digitalWrite(pinnummer,HIGH);
    delay (delaytijd);
    digitalWrite(pinnummer,LOW);
  }
}



int geefafstand(){
  //welke parameter pinnummer 
   
}

void buzzer(){
  
}

void setup() {
  Serial.begin(9600);
  pinMode(greenLightPin, OUTPUT);
  pinMode(redLightPin, OUTPUT);
  pinMode(PinButton1, INPUT);
  pinMode(PinButton2, INPUT);
}

void loop(){ 

  buttonState1 = digitalRead(PinButton1);
  buttonState2 = digitalRead(PinButton2);
  
  if (buttonState1 == HIGH ){
    knipperled(greenLightPin,10,1000);
    
  }
  
  if (buttonState2 == HIGH ){
    knipperled(redLightPin,5,500);//VARIABLE //parameter (redLightPin,5,500) 
    
  }
}

This is not a tutorial

Hello
You have to declare the variable in this case locally.

change to

bool buttonState1 = digitalRead(PinButton1);
bool buttonState2 = digitalRead(PinButton2);

Have a nice day and enjoy coding in C++.

thank you all its working :slight_smile:

Hello
INT or BOOL ?

@ paulpaulson

i used int if that is correct

bool can only be used as true or false

@opelvectra

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Neither.
int.

…or bool (my choice in this example)

(- suggested because the C languages put a lot of emphasis in values being zero or non-zero…)

Why "bool"?

digitalRead doesn't return a bool.

Imagine the pin is an input, with pullup.
So, a closed switch returns LOW, or false in your parlance.
S'gonna make a nonsense of the naming of the variable you assign it to, dontcha think?

Hello
You are right. The function digitalRead() will return either HIGH or LOW. These results can be interpreted as TRUE or FALSE.
Have a nice day and enjoy coding in C++.

...or even true or false, as they're more usually written.

cheers

AWOL,
You’re absolutely right, and this unfortunately is where the Arduino school is teaching ‘half truths’ to beginners. (or half falses)

This falls back to the fundamental zero/non-zero case… false or true which emerged long before Arduino… simple tests in machine language to ‘branch’ in code.

Luckily the true/false test interprets the int (or other numeric type) to the correct state for the test.

It’s not ideal, but as long as the 0/1 test holds true, the language holds together.

I hate to think what would happen if this was changed in the C standard, maybe 80% of all C derived code would fail !