Execute lines of code once per button press

Trying to blink a LED infinitely if button 1 is pressed, 3 times if button two is pressed and only once if button 3 is pressed. I have the basic code for inputs and outputs etc but obviously until I can incorporate some code to cancel it at the end of each "IF" its going to behave quite different. I have almost no experience with Arduino or any other coding which hasn't helped with finding a suitable code command to help me online. If someone could help edit or even just point me in the right direction for what I need in this particular code so I can learn myself it would be much appreciated :slight_smile:

  • Board is Nano ATmega328P (Old Bootloader)
const int ledPin =  5; 
const int buttonPin1 = 2;     
const int buttonPin2 = 3;
const int buttonPin3 = 4;


int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  
  }

void loop() {
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  if (buttonState1 == HIGH) {
    digitalWrite(ledPin, HIGH);
    delay(20);
    digitalWrite(ledPin, LOW);
    delay(80);
    } 
    
    else if (buttonState2 == HIGH) {
    digitalWrite(ledPin, HIGH);
    delay(20);
    digitalWrite(ledPin, LOW);
    delay(80);
    digitalWrite(ledPin, HIGH);
    delay(20);
    digitalWrite(ledPin, LOW);
    delay(80);
    digitalWrite(ledPin, HIGH);
    delay(20);
    digitalWrite(ledPin, LOW);
    }
   
   else if (buttonState3 == HIGH) {
    digitalWrite(ledPin, HIGH);
    delay(20);
    digitalWrite(ledPin, LOW);
    }
}

How are your switches wired?

delay() is a blocking function, not a good thing to use in most sketches.

Thanks for posting your code with code tags!

D2/3/4 are connected to ground via 10kohm resistors, then connected to 5v+ via rotary select switch, also has a N/A push switch in series. LED is pin 5 with 2kohm to ground?

Thanks for the fast reply :slight_smile:

also has a N/A push switch in series

Explain.

N.O. ?

Yes sorry N.O. Been working on too many Ivecos with "not applied" switches. Also would millis be a better timer then delay?

Jaybongo:
Trying to blink a LED infinitely if button 1 is pressed...

I have the basic code for inputs and outputs etc but obviously until I can incorporate some code to cancel it at the end of each "IF" its going to behave quite different.

How is the infinite state cleared? The logic seems to indicate maintained switches.

Also would millis be a better timer then delay?

Yes

20ms and 80ms is very fast :o

Ideally button 1 would have to be held down for it to flash, button two or 3 could be held down or pressed once and get the 3 pulse or single pulse once each time they are pressed or held on

Ideally button 1 would have to be held down for it to flash

While it is pressed? i.e. it stops when released?

Still confused as how the switches are wired.

Show us a good schematic.

Yeah it is, eventually it wont actually be an led it will be a logic pnp transistor triggering 3 MOSFETS firing a high powered coil hence the short duty cycle. I was just trying to keep things simple for now.

What is this project going to be used for?

Basically a coil gun, so you have a select fire switch and then the trigger itself.

See if you can figure this version out.

Ask if you have questions.

Button 3 has not been programmed yet.

//Version 1.0

#define LEDon  HIGH
#define LEDoff !LEDon

#define PUSHED HIGH //switch pressed gives HIGH (+5V) on pin

unsigned long heartBeatMillis;
unsigned long switchMillis;
unsigned long lastMillis;
unsigned long delayTime;

const unsigned long heartBeatDelay = 500; //1/2 second
const unsigned long debounceDelay  = 50;  //50ms

const byte heartBeatLED = 13; //blinks to indicate non blocking code
const byte ledPin       = 5;

const byte buttonPin1   = 2;
const byte buttonPin2   = 3;
const byte buttonPin3   = 4;

const byte onTime       = 1000; //for testing
const byte offTime      = 2000; //for testing

byte buttonState1;
byte buttonState2;
byte buttonState3;

byte lastbuttonState1;
byte lastbuttonState2;
byte lastbuttonState3;

byte myCounter;

//4 possible states for this project
enum StateMachine {stateZero, stateOne, stateTwo, stateThree};
StateMachine mState = stateZero;

//*****************************************************************************
void setup()
{
  pinMode(heartBeatLED, OUTPUT);
  pinMode(ledPin, OUTPUT);

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);

} //END of setup()

//*****************************************************************************
void loop()
{
  //*******************************
  //toggle the heart beat LED every 1/2 second
  if (millis() - heartBeatMillis >= heartBeatDelay)
  {
    //re-initialize the timer
    heartBeatMillis = millis();

    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));

  }

  //*******************************
  //check the switch(es) every 50ms
  if (millis() - switchMillis >= debounceDelay)
  {
    //re-initialize the timer
    switchMillis = millis();

    checkSwitch();

  }

  //*******************************
  switch (mState)
  {
    //*****************
    case stateZero:
      //do nothing

      break;

    //*****************
    //flash LED while button is pressed
    case stateOne:
      if (millis() - lastMillis >= delayTime)
      {
        lastMillis = millis();

        digitalWrite(ledPin, !digitalRead(ledPin));

        if (delayTime == onTime)
        {
          delayTime = offTime;
        }

        else
        {
          delayTime = onTime;
        }
      }

      break;

    //*****************
    //Flash LED 3 times
    case stateTwo:
      if (millis() - lastMillis >= delayTime)
      {
        lastMillis = millis();

        digitalWrite(ledPin, !digitalRead(ledPin));

        if (delayTime == onTime)
        {
          delayTime = offTime;
        }
        else
        {
          delayTime = onTime;
        }

        if (myCounter > 3)
        {
          mState = stateZero;
        }
        myCounter++;

      }

      break;

    //*****************
    //Flash LED once
    case stateThree:

      break;
  }

} //END of loop()

//*****************************************************************************
void checkSwitch()
{
  byte currentState;

  //*******************************
  //buttonPin1
  currentState = digitalRead(buttonPin1);

  if (lastbuttonState1 != currentState)
  {
    //update to the current state
    lastbuttonState1 = currentState;

    //pressed
    if (currentState == PUSHED)
    {
      mState = stateOne;
      digitalWrite(ledPin, LEDon);
      delayTime = onTime;
      lastMillis = millis();
    }

    //released
    else
    {
      digitalWrite(ledPin, LEDoff);

      mState = stateZero;
    }

  } //END of  if (lastbuttonState1 != currentState)

  //*******************************
  //buttonPin2
  currentState = digitalRead(buttonPin2);

  if (lastbuttonState2 != currentState)
  {
    //update to the current state
    lastbuttonState2 = currentState;

    //closed
    if (currentState == PUSHED)
    {
      mState = stateTwo;
      digitalWrite(ledPin, LEDon);
      myCounter = 0;
      delayTime = onTime;
      lastMillis = millis();
    }

  } //END of  if (lastbuttonState2 != currentState)

  //*******************************
  //buttonPin3
  currentState = digitalRead(buttonPin3);

  if (lastbuttonState3 != currentState)
  {
    //add button 3 stuff

  } //END of  if (lastbuttonState3 != currentState)

  //*******************************

} //END of checkSwitch()

//*****************************************************************************

Wow that looks awesome, I'll go try reverse engineer it. Thanks heaps

Try to figure out what needs to be added for switch 3.

If you run into problems, ask for guidance.

EDIT:

const byte onTime = 1000; //for testing
const byte offTime = 2000; //for testing

Should be:

const int onTime = 250; //for testing
const int offTime = 500; //for testing

Will do thanks so much for your time!