Multi Functional Button?

Hello everyone,

Newbie here again.

I am trying to write my first sketch with the help of snipits and forum stars.

Along with my other project which is waiting for parts, I wanted to start my next project.

The new project involves a multi functional button and some wait times. I am trying to re-create an alarm feature I want but for a classic car.

Here is what i am trying to do, hopefully it makes sense to someone:

IF BUTTON PRESSED (RFID LATER ON)

UNLOCK DOORS (activate led 1)

IF BUTTON PRESSED AGAIN WITHIN 10 SECONDS OF 1ST PRESS

TURN ON IGNITION (activate led2)
WAIT FOR NEXT INPUT

THEN IF BUTTON IS HELD DOWN FOR 1000ms,

START ENGINE (activate led3)
(TURN STARTER AS LONG AS BUTTON IS HELD)

IF BUTTON IS PRESSED AGAIN

KILL ENGINE (all leds off)

IF BUTTON IS PRESSED AGAIN WITHIN 3 SECONDS, IGNITION TURNS BACK ON, WAITING FOR START COMMAND.

ELSE IF NOTHING PRESSED FOR 3 SECONDS, KEEP ALL OFF GO BACK TO STEP 1.
)

end return to 1.

The LEDS are just so i can visualize without the LCD that i have been using. Eventually the LEDS will be replaced with relays and hard wired into the ignition.

Here is my code so far:

// include the library code:
#include <LiquidCrystal.h>
int powerLed = 8;
int ignitionLed = 9;
int starterLed = 10;
const int buttonPin = 6;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int backLight = 13;    // pin 13 will control the backlight

void setup () 
{
    // set up the LCD's number of columns and rows: 
  lcd.begin(16,2);
    pinMode(backLight, OUTPUT);
    pinMode(powerLed, OUTPUT);
    pinMode(ignitionLed, OUTPUT);
    pinMode(starterLed, OUTPUT);
    pinMode(buttonPin, INPUT);    //button
}

void loop() 
{
     buttonState = digitalRead(buttonPin); // RFID Setup  <--- Rewrite this for RFID later on, using button for testing.
     
 
 
  // Listen for RFID Authorized Access
  
  if (buttonState == HIGH)  {  // Listen for RFID
       // Power Light
       digitalWrite(powerLed, HIGH);

       // Unlock Car
           lcd.clear();  // LCD USE ONLY, This clears the screen in case there is anything there.
           digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
           delay(500);
           lcd.print("Car Unlocked");
           delay(1000);




              // Turn on Ignition
              
              
  
  
                    // Activate Starter
                    
                    
  
  
                          // Kill Ignition
                          
                          
  
  
                                // Loop with 5000ms
                                
                                


       //  Delay 10     
           lcd.clear();
           for (int thisChar = 9; thisChar > 0; thisChar--) {
             lcd.print("Waiting for ");
             lcd.print (thisChar);
             lcd.print ("s");
           delay(1000);
           lcd.clear();
          
         }
         
         
         
         // Auto Re-Lock
           lcd.print("Car Now Locked");
           delay(2000);  // wait 2 seconds
           digitalWrite(backLight, LOW); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
           
         // Power Light Off
         digitalWrite(powerLed, LOW);
    
    
           
  } // IF UNLOCK / END
  else
  {
    // Do Nothing
  } // IF UNLOCK / ELSE END
 
  
} // CODE END

Hopefully someone can point me in right direction for how to write the second command.

Thank you everyone. :slight_smile:

Looks like a state machine to me (but then again, most things do).

Name each state: Off&Locked, Off&Unlocked, Ignition&unlocked, Starting&unlocked, Running&unlocked, Running&locked

Your car will be in one of those states. Within loop() you put a switch statement for the states. Within each state you check to see if the conditions call for moving to a different state and you perform whatever operation you need to enter that state (unlocking, noting the time).

It's EASY! :slight_smile:

That makes it sound more obtainable, thank you. Question is now, where can I find more info on how to write a state machine? So far i have been learning the most from videos and web docs, but out side of the Finite State Machine library I am not coming up with anything that has Arduino and State Machine in it. Do you have any suggestions on where I should look?

Also, is this similar to cases? I have seen that in codes before.

Thank you! :slight_smile:

Scott

State machines are a universal concept, there is nothing arduino specific about them.