Programming Buttons

Hello, what I'm trying to do is probably pretty simple but I need help. What I want to do is this - when one button is pressed (then depressed), and after that a different button is pressed (then depressed), an LED turns on. A great example for this is a vending machine, you press say "D" and then the number "5" and then the snack will dispense. Instead, all I want is a specific LED to turn on. If there is a simple way to code this please let me know, I'm trying to do this with 4 LEDs and 5 Buttons so here is my code so far. As of right now the code does not do anything while connected to the breadboard with buttons/LEDs. :slight_smile:

// Constants won't change.
// Set button pins and LED pins:
const int buttonPinA = 2;      // Button that symbolizes A
const int buttonPin1 = 3;      // Button that symbolizes 1
const int buttonPin2 = 4;      // Button that symbolizes 2 
const int buttonPin3 = 5;      // Button that symbolizes 3
const int buttonPin4 = 6;      // Button that symbolizes 4
const int ledPin1 =  9;        // LED that symbolizes snack1
const int ledPin2 =  10;       // LED that symbolizes snack2
const int ledPin3 =  11;       // LED that symbolizes snack3
const int ledPin4 =  12;       // LED that symbolizes snack4
const int partyPin = 8;        // Step between Pin A and Pin ( 1, 2, 3, or 4)

// Variables will change:
int buttonStateA = 0;           // Variable for reading the pushbutton status
int buttonState1 = 0;           // Variable for reading the pushbutton status
int buttonState2 = 0;           // Variable for reading the pushbutton status
int buttonState3 = 0;           // Variable for reading the pushbutton status
int buttonState4 = 0;           // Variable for reading the pushbutton status

void setup() {
  // Initialize the LED pins as outputs:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  // Initialize the pushbutton pins as inputs:
  pinMode(buttonPinA, INPUT);
  pinMode(buttonPin1, INPUT); 
  pinMode(buttonPin2, INPUT); 
  pinMode(buttonPin3, INPUT); 
  pinMode(buttonPin4, INPUT);
}

void loop(){
  // Read the state of the pushbutton value:
  buttonStateA = digitalRead(buttonPinA);
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  
  if (buttonPinA == HIGH){
    digitalWrite(partyPin, HIGH);
}

  if (partyPin && buttonPin1 == HIGH){
    digitalWrite(ledPin1, HIGH);
    delay(5000);
    digitalWrite(ledPin1, LOW);
  }
}

If there is a simple way to code this please let me know

The state change detection example would be a good place to start.

A state machine would be a good place to start. You have only a few states - waiting for first switch, waiting for second switch, dispensing the snack. There are some things you do to transition from one state to the next - record the first switch number, record the second switch number, dispense the selected snack and reset both numbers.

Some transitions happen when external events happen (waiting for first switch transitions to waiting for second switch when the user presses a switch). Some happen under program control (dispensing the snack transitions to waiting for first switch when the snack has been dispensed).

How are the switches wired? Connecting one leg to a digital pin and one leg to ground, and using INPUT_PULLUP as the pin mode is far simpler. The switch will read HIGH when not pressed, and LOW when pressed.

Gammon's Tip 1: Use arrays

This is almost certainly wrong

if (partyPin && buttonPin1 == HIGH){

partyPin and buttonPin1 have the values of the pin numbers, not the values that come from digitalRead()

Also partyPin is an output so it will have the value you gave it with digitalWrite()

You don't seem to set pinMode() for partyPin

...R