Advice Needed On Debounce Sketch and Logic - Newbie to Programming

Hello,

I have just purchased an Arduino Nano… actually its still in the post, so i decided to start to learn the process of writing sketches, having had no prior knowledge of programming so i would appreciate some guidance. This wee bit has taken 3 hrs to do…but is it right?

At present i have three questions

  1. Will this sketch work as intended (subject to meeting the below requirements), it verifies is this a good sign ?

  2. Is there a command to furfill pre-requistes on push button switches…my sketch at present still means that any of the push buttons can be operated by the user and something will happen. It needs a sequence of events.

By which i mean if there are 3 push buttons, (button1, button2, button3). How do i tell the program that button 1 must be HIGH before button 2 can be HIGH, Button2 Must be HIGH before Button 3 can be High.

  1. The push buttons need a debounce command which at present i have left out… Can i put 1 command statement in that will cover all the buttons used in this sketch? i have worked out how to input an individual statement…but that’s as far as i got.

So Here is my code so far

// Project Eridge Sequence Control Panel - Part 1

// Part 1 - Dispatching Trains from Fiddle Yards to Main Layout.

// Objective - Using a series of pushbuttons and LEDs to communicate to different layout operators the correct sequence
//of operation.

// 1. Operator 1 will inform Operator 2 that the Train is Ready for Dispatch by pressing a pushbutton (Ready Dispatch)
      //this will illuminate an Amber LED on both Operator 1 and 2 control panels and cancel Ready for Next Service.
// 2. Operator 2 will confirm Train Accepted by pressing pushbutton (Train Accepted), cancelling the output described 
     //above, and this will illuminate a Green LED on both Operators Control Panels.
// 3. Operator 2 will notify Operator 1 when Ready for Next service by pressing a pushbutton (Ready Next Service) this 
     //will illuminate an Orange LED on both operator Control Panels and switch all above outputs to LOW
     //(restarting sequence)




  //Program starts here:

  // constants won't change. They're used here to set pin numbers

const int buttonReadyDispatch = 2;       // Assign Ready Dispatch Pushbutton Pin2
const int buttonTrainAccepted = 3;       // Assign Train Accepted Pushbutton Pin 3
const int buttonReadyNextService = 4;    // Assign Ready Next Service Pushbutton Pin 4

const int ledReadyDispatch =  11;      // Assign Amber LED Train Ready fotr Dispatch Pin 11
const int ledTrainAccepted = 12;       // Assign Green LED Train Accepted Pin 12
const int ledReadyNextService = 13;    // Assign Orange LED Ready Next Service Pin 13


  // variables will change:
int buttonState = 0;         // variable for reading the pushbutton state


void setup()
{
  // Sets the pushbuttons as Input:
  
  pinMode(buttonReadyDispatch, INPUT);  
  pinMode(buttonTrainAccepted, INPUT);
  pinMode(buttonReadyNextService, INPUT);
  
  // Sets the LEDs as Outputs:
  
  pinMode(ledReadyDispatch, OUTPUT);
  pinMode(ledTrainAccepted, OUTPUT);
  pinMode(ledReadyNextService, OUTPUT);
  
  
}

void loop()
{
  // Informing program that LEDs are ON only when assigned button is HIGH
  
  // read the state of the Ready for Dispatch pushbutton value:
  
  buttonState = digitalRead(buttonReadyDispatch);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
  // turn LED on:    
    digitalWrite(ledReadyDispatch, HIGH);  
  }
  else {
  // turn LED off:
      digitalWrite(ledReadyDispatch, LOW); }
      
      
  // read the state of the Train Accepted pushbutton value:
  
  buttonState = digitalRead(buttonTrainAccepted);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
  // turn LED on:    
    digitalWrite(ledTrainAccepted, HIGH);  
  }
  else {
  // turn LED off:
      digitalWrite(ledTrainAccepted, LOW); }
      
  // read the state of the Ready for Next Service pushbutton value:
  
  buttonState = digitalRead(buttonReadyNextService);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
  // turn LED on:    
    digitalWrite(ledReadyNextService, HIGH);  
  }
  else {
  // turn LED off:
      digitalWrite(ledReadyNextService, LOW); }
  
  
  
  // informing Program to cancel (set LOW) certain pushbutton Outputs when other pushbuttons Outputa are HIGH 
  
  
  // Objective 1. - Operator 1 will inform Operator 2 that the Train is Ready for Dispatch by pressing a pushbutton
                // (Ready Dispatch) this will illuminate an Amber LED on both Operator 1 and 2 control panels
                // and cancel Ready for Next Service.
  // i.e When buttonReadyDispatch is HIGH then buttonReadyNextService must be LOW; 
  //also buttonTrainAccepted must be LOW:
  
  // read the state of the Ready for Dispatch pushbutton value:
      
  buttonState = digitalRead(buttonReadyDispatch);
  
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn Ready Train Accepted pushbutton off:    
      digitalWrite(buttonTrainAccepted, LOW); } 
      
       buttonState = digitalRead(buttonReadyDispatch);
  
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn Ready Next Service pushbutton off:    
      digitalWrite(buttonReadyNextService, LOW); } 
  

      
  //Objective 2. - Operator 2 will confirm Train Accepted by pressing pushbutton (Train Accepted), 
                 //Cancelling the Ready Dispatch pushbutton and Ready Dispatch LED output to LOW:
  // i.e When buttonTrainAccept is HIGH then buttonReadyDispatch must be LOW
      
  // read the state of the Train Accepted pushbutton value:
       buttonState = digitalRead(buttonTrainAccepted);

  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn Ready Dispatch pushbutton off:    
      digitalWrite(buttonReadyDispatch, LOW); } 
      
      
       buttonState = digitalRead(buttonTrainAccepted);

  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn Ready Next Service pushbutton off:    
      digitalWrite(buttonReadyNextService, LOW); } 
  
 
        
  // Objective 3. - Operator 2 will notify Operator 1 when Ready for Next service by pressing a pushbutton 
                 //(Ready Next Service) this will illuminate an Orange LED on both operator Control Panels 
                 // and switch all Other outputs to low (restarting sequence)
  // i.e When buttonReadyNextservice is HIGH then buttonReadyDispatch and buttonTrainAccepted must be LOW
  
  // read the state of the Ready for Next Service pushbutton value:
       buttonState = digitalRead(buttonReadyNextService);

  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn Ready Dispatch pushbutton off:    
      digitalWrite(buttonReadyDispatch, LOW); } 

  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn ReadyDispatch pushbutton off:    
    digitalWrite(buttonTrainAccepted, LOW);  
  
  }
  
     
      
}

:blush: Ooops Sorry, My Apology for all the narrative in the code, it’s there for my benifit :blush:

Thank You for taking the time to view my post
To those that reply I’m grateful for your valuable advise, positive and negative I welcome any suggestions

Mark

Eridge_Sequennce_Test_1.ino (5.77 KB)

if (buttonState == HIGH) {    
  // turn LED on:    
    digitalWrite(ledReadyDispatch, HIGH);  
  }
  else {
  // turn LED off:
      digitalWrite(ledReadyDispatch, LOW); }

Much simpler digitalWrite(ledReadyDispatch, buttonState);

There's no reason I can see for buttonState to be a global.

To learn Arduino, you start with the Blink example for a blinking led. After that, send a message to the serial monitor. Next thing is reading the buttons. Do you have a pull-up or pull-down resistor with the buttons ?.

The code you want to write is a well known problem. The Arduino is many times used for some kind of state machine with sequences, delays, buttons and outputs. But it is not the easiest thing to program. You have to be able to convert a real world situation into a mathematical program.

You can write the sequence in code, or create an array with the sequence. In both cases you need a variable that keeps track of the current state. You could use a single variable with the current state, or use the state of the three buttons.

AWOL: if (buttonState == HIGH) {      // turn LED on:        digitalWrite(ledReadyDispatch, HIGH);    }   else {   // turn LED off:       digitalWrite(ledReadyDispatch, LOW); }

Much simpler digitalWrite(ledReadyDispatch, buttonState);

Or even:

digitalWrite(ledReadyDispatch, digitalRead(buttonReadyDispatch));

There's no reason I can see for buttonState to be a global.

I can see no reason for buttonState.

P4_Modeller: At present i have three questions

  1. Will this sketch work as intended (subject to meeting the below requirements), it verifies is this a good sign ?

The fact that it verifies only tells you that the syntax is correct, not whether it will work as intended.

  1. Is there a command to furfill pre-requistes on push button switches....my sketch at present still means that any of the push buttons can be operated by the user and something will happen. It needs a sequence of events.

By which i mean if there are 3 push buttons, (button1, button2, button3). How do i tell the program that button 1 must be HIGH before button 2 can be HIGH, Button2 Must be HIGH before Button 3 can be High.

Try something like this:

if(button1 && button2 && button3){...
// only true if all three buttons are HIGH at the same time

if(button1 || button2 || button3){...
//true if any one (or more) of the three buttons is HIGH
//false if none of the buttons are pressed

//or you could use nested if statements

if(button1){...
   if(button2){...  //here, both buttons must be high for this to be true
       if(button3){...  // here, all three buttons must be high for this to be true
       }
   }
}

Thanks Everyone for the Comments So Far

I now have some useful directions to explore and learn :)

Mark