Help on pushbutton project

i want to have it be able to transit between 3 modes: flashing, on, and off. so far, im able to switch between 2 of above mentioned modes, but i cant seem to get all 3 in. here is my code so far. any help is appreciated. thanks in advance.

type oconst byte ledPin = 13;
const byte buttonPin = 2;

bool flashState = false;

void setup()
{
   Serial.begin(115200);
   pinMode (ledPin, OUTPUT);
   pinMode (buttonPin, INPUT_PULLUP);
}
void loop()
{
   checkButton();
   flashLED();
}

void checkButton()
{
   static boolean lastButtonState = 0;
   boolean buttonState = digitalRead(buttonPin);
   if (buttonState != lastButtonState)
   {
      if (buttonState == LOW)
      {
         flashState = !flashState; // toggle flashState
      } 
      lastButtonState = buttonState;
   }
}
void flashLED()

{
   if (flashState == true)
   {
      digitalWrite (ledPin, LOW);
      delay(50);
      digitalWrite (ledPin, HIGH);
      delay(50);
      digitalWrite (ledPin, HIGH);
      delay(0);
   }
}r paste code here

Is this a school assignment ?

Show us a good schematic of your circuit. Show us a good image of your ‘actual’ wiring. Give links to components.

Do you know what a State Machine is ?

Create a counter variable.   Increment it each time the switch changes from not pressed to pressed.  When the counter gets to 3 reset it to 0.  Your three states are 0, 1, 2.  When counter == 0 enable whatever code you need for that state - like maybe lamp off?  When counter == 1enable code for flashing, and so on.


it kinda looks like this. the cables are male to male and the resistor in the upper left is a 220 while the one near the button is a 10k

im a middle school student so im kinda clueless on syntax and whatnot, where should this variable and its affiliated coding fit in the above mentioned code? thanks for your help and time.

Good for you for looking at whether there is a change in state in a switch !

Here is a start:

   //was there a switch change in state ?
   if (buttonState != lastButtonState)
   {
     //update to this new state    
     lastButtonState = buttonState;

     //is the switch now pressed ?
     if (buttonState == PUSHED)
      {
         counter++;
         //don’t go over the maximum 
         if(counter > maximumCount)
           {
                counter = 0;
           }
      } 
     
   }

could you explain this expression?

EDIT: never mind

Hello
I love this school assignment and I hope the TO is able to find the hidden design trap.
This school assignment will be used for the local Arduino training.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.