ButtonPressed Not Declared in Scope

Hi I am a beginner and I need some help! I have tried everything to resolve this error and I can’t figure it out. The code is a little long, sorry for that. The line that is being highlighted is the " DispatchButtonPressed = false; " It is in the first part of the code in the Void Loops section. It says " ‘DispatchButtonPressed’ was not declared in this scope" as the error. I can’t figure out why. Thanks any help.

test.ino (3.28 KB)

Where is “DispatchButtonPressed” defined? :wink:

If you post your code as directed by the forum guidelines more members will see it.

You have defined a buttonPressed that you never use. You don't have a DispatchButtonPressed or for that matter StopButtonPressed or StopPressed.

You also have a problem trying to use Keyboard. What Arduino are you compiling this for?

You really need to check ALL of the error messages.

Steve

Another problem is that INPUT_PULLUP gives LOW when pressed.
And you test the pin number not the button state:

if (DispatchButton == true)

This version should do better (if I didn’t miss any other mistake :slight_smile: ):

const int PowerSwitch = 12;
const int Floorlight = 11;
const int FloorButton = 10;
const int Stopresetlight = 9;
const int StopresetButton = 8;
const int Stoplight = 7;
const int StopButton = 6;
const int GatesSwitch = 5;
const int Harnesslight = 4;
const int HarnessButton = 3;
const int Dispatchlight = 2;
const int DispatchButton = 1;

boolean DispatchButtonPressed; //1 if pressed, 0 if not
boolean StopButtonPressed;

#include <Keyboard.h>

void setup() {
  pinMode(Dispatchlight, OUTPUT);
  pinMode(DispatchButton, INPUT_PULLUP);
  pinMode(Harnesslight, OUTPUT);
  pinMode(HarnessButton, INPUT_PULLUP);
  pinMode(GatesSwitch, INPUT_PULLUP);
  pinMode(StopButton, INPUT_PULLUP);
  pinMode(Stoplight, OUTPUT);
  pinMode(StopresetButton, INPUT_PULLUP);
  pinMode(Stopresetlight, OUTPUT);
  pinMode(FloorButton, INPUT_PULLUP);
  pinMode(Floorlight, OUTPUT);
  pinMode(PowerSwitch, INPUT_PULLUP);

  digitalWrite(DispatchButton, HIGH);
  digitalWrite(HarnessButton, HIGH);
  digitalWrite(GatesSwitch, HIGH);
  digitalWrite(StopButton, HIGH);
  digitalWrite(StopresetButton, HIGH);
  digitalWrite(FloorButton, HIGH);
  digitalWrite(PowerSwitch, HIGH);
}

void loop() {
  if (digitalRead(DispatchButton) == LOW) {
    DispatchButtonPressed = true;
    delay(30);  // debounce
  } else {
    DispatchButtonPressed = false;
  }

  if (digitalRead(StopButton) == LOW) {
    StopButtonPressed = true;
    delay(30);  // debounce
  } else {
    StopButtonPressed = false;
  }

  if (DispatchButtonPressed == true) {
    digitalWrite(Dispatchlight, HIGH);
    digitalWrite(Stopresetlight, LOW);
    digitalWrite(Floorlight, LOW);
    digitalWrite(Harnesslight, LOW);
    digitalWrite(Stoplight, LOW);
    delay(500);
    digitalWrite(Stoplight, HIGH);
    delay(500);
    Keyboard.press(0xCD);
    Keyboard.release(0xCD);
  } else {
    digitalWrite(Dispatchlight, LOW);
    digitalWrite(Harnesslight, LOW);
    digitalWrite(Stoplight, LOW);
    digitalWrite(Stopresetlight, LOW);
    digitalWrite(Floorlight, LOW);
    delay(500);
    digitalWrite(Dispatchlight, HIGH);
    digitalWrite(Harnesslight, HIGH);
    digitalWrite(Stoplight, HIGH);
    digitalWrite(Stopresetlight, HIGH);
    digitalWrite(Floorlight, HIGH);
    delay(500);

    if (StopButtonPressed == true) {
      digitalWrite(Stoplight, HIGH);
      digitalWrite(Dispatchlight, LOW);
      digitalWrite(Harnesslight, LOW);
      digitalWrite(Floorlight, LOW);
      digitalWrite(Stopresetlight, HIGH);
      delay(250);
      digitalWrite(Stopresetlight, LOW);
      delay(250);
      Keyboard.press(0xD1);
      Keyboard.release(0xD1);
    } else {
      digitalWrite(Dispatchlight, LOW);
      digitalWrite(Harnesslight, LOW);
      digitalWrite(Stoplight, LOW);
      digitalWrite(Stopresetlight, LOW);
      digitalWrite(Floorlight, LOW);
      delay(500);
      digitalWrite(Dispatchlight, HIGH);
      digitalWrite(Harnesslight, HIGH);
      digitalWrite(Stoplight, HIGH);
      digitalWrite(Stopresetlight, HIGH);
      digitalWrite(Floorlight, HIGH);
      delay(500);
    }
  }
}

But you use many times the instruction delay, which stops the code execution. So this may prevent some buttons press to be catched. You need to remove those delays and learn how to male a non blocking code. There are many tutorials on the net, and a very good one in the tutorial section of the forum. Read it…

So I figured fixed my problem by renaming my booleans so each boolean manages a different button. The next thing I need to figure out is when a button is pressed it stops the boolean and turns on or off the led inside the button. The problem I am having is when the button pressed it activates the ‘buttonpressed == true’ statement. When I let off the button it goes back to the default ‘buttonpressed == false’ I understand that it is doing exactly what i should be doing. Is a boolean the right thing to be using for my circumstances? Any help is much appreciated. Code is below! Thanks!!

const int PowerSwitch = 12;
const int Floorlight = 11;
const int FloorButton = 10;
const int Stopresetlight = 9;
const int StopresetButton = 8;
const int Stoplight = 7;
const int StopButton = 6;
const int GatesSwitch = 5;
const int Harnesslight = 4; 
const int HarnessButton = 3;
const int Dispatchlight = 2;
const int DispatchButton = 1;
boolean DispatchButtonbuttonPressed; 
boolean HarnessButtonbuttonPressed;
boolean FloorButtonbuttonPressed; 
boolean StopButtonbuttonPressed;
boolean StopresetButtonbuttonPressed;
boolean GatesSwitchbuttonPressed;
boolean PowerSwitchbuttonPressed;
#include <Keyboard.h> 
void setup() {
  
  pinMode(Dispatchlight,OUTPUT); 
  pinMode(DispatchButton, INPUT_PULLUP);
  pinMode(Harnesslight, OUTPUT);
  pinMode(HarnessButton, INPUT_PULLUP);
  pinMode(GatesSwitch, INPUT_PULLUP);
  pinMode(StopButton, INPUT_PULLUP);
  pinMode(Stoplight, OUTPUT);
  pinMode(StopresetButton, INPUT_PULLUP);
  pinMode(Stopresetlight, OUTPUT);
  pinMode(FloorButton, INPUT_PULLUP);
  pinMode(Floorlight, OUTPUT);
  pinMode(PowerSwitch, INPUT_PULLUP);

  digitalWrite(DispatchButton, HIGH);
  digitalWrite(HarnessButton, HIGH);
  digitalWrite(GatesSwitch, HIGH);
  digitalWrite(StopButton, HIGH);
  digitalWrite(StopresetButton, HIGH);
  digitalWrite(FloorButton, HIGH);
  digitalWrite(PowerSwitch, HIGH);

}

void loop() {
 if (digitalRead(StopButton) == LOW) {
  StopButtonbuttonPressed = true;
 } else {
  StopButtonbuttonPressed = false;
 }  {

    if(StopButtonbuttonPressed == true) {
        digitalWrite(Stoplight, HIGH); 
        Keyboard.press(0xD1);
        digitalWrite(Stopresetlight, HIGH);
        delay(250);
        digitalWrite(Stopresetlight, LOW);
        delay(250);
        
        
    }
    else {
        digitalWrite(Stoplight, LOW);
        delay(500);
        digitalWrite(Stoplight, HIGH);
        delay(500);
    }
 if (digitalRead(StopresetButton) == LOW) {
  StopresetButtonbuttonPressed = true;
 } else {
 StopresetButtonbuttonPressed = false;
 }

    if(StopresetButtonbuttonPressed == true) {
        digitalWrite(Stopresetlight, HIGH); 
        delay(5000);
        digitalWrite(Stopresetlight,LOW);
        Keyboard.release(0xD1);
    }
    else {
        digitalWrite(Stopresetlight, LOW);
    }

 {


}
  
 }
}  {

I’ve never seen that construct in a C program before.

Could you maybe try using the IDE’s auto-format tool before posting code?

Sorry!