State Machine or Event Driven programming

Hi all,

I'm trying to understand how I can use the Arduino UNO to do FSM/Event driven programming. My end result is going to be a complicated system of servos moving in certain patterns, but needing to react to sensor feedback, button presses, etc. while running the scripted movements of the servos.

I'm starting from scratch to refresh my old programming brain as I graduated from university so long ago, I've forgotten some of this stuff... (new to Arduino, but comp sci major and have been working as a sys admin/programmer for many years)

To start off I've created a blinking LED(just like the Blinking LED without delay example) and then threw in another LED, a button, and a tilt sensor.

My basic idea(for learning) is to have a red LED flash, while I control a blue LED via both the button and the tilt sensor. I then will go more complicated after I get things working as expected.

I've got things hooked up, and the tilt sensor works fine, but the button seems to act odd. If I press and hold it, the LED flashes very quickly. If I press and release the LED will be on/off depending on how long I hold on. I feel like this is actually an issue with the type of button itself, or my understanding of the button/code is working.

I thought when the button is pressed, it will be sending a 1 or HIGH signal to the code the entire time?

Here is a similar button(unfortunately I got mine in a "starter kit" so I don't know the exact vendor/model number):

Here is the code:

// State Machine to flash red LED
// and turn on Blue LED when button or
// tilt activated

// pin setup
const int buttonPin = 2;
const int tiltPin = 3;
const int redLED = 4;
const int blueLED = 5;

// state variables
int blueLEDState = LOW;
int redLEDState = LOW;
int buttonState = LOW;
int tiltState = LOW;
long currentMillis = 0;
long previousRedLEDMillis = 0;

// interval variables
long redLEDInterval = 100;


void setup() {
  // put your setup code here, to run once:
  pinMode(buttonPin,INPUT);
  pinMode(tiltPin,INPUT);
  pinMode(redLED,OUTPUT);
  pinMode(blueLED,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  currentMillis = millis();
  manageButton();
  manageTilt();
  manageRedLED();
  
}

void manageButton(){
  if(digitalRead(buttonPin) == HIGH){
    if( blueLEDState == LOW ){
      digitalWrite(blueLED,HIGH);
      blueLEDState = HIGH;
    }
    else{
      digitalWrite(blueLED,LOW);
      blueLEDState = LOW;
    }
  }
}

void manageTilt(){
  int newTiltState = digitalRead(tiltPin);
  if(newTiltState != tiltState){
    tiltState = newTiltState;
    if( blueLEDState == LOW ){
      digitalWrite(blueLED,HIGH);
      blueLEDState = HIGH;
    }
    else{
      digitalWrite(blueLED,LOW);
      blueLEDState = LOW;
    }
  }
}


void manageRedLED(){
  if (currentMillis - previousRedLEDMillis > redLEDInterval ){
    if(redLEDState == LOW){
      digitalWrite(redLED,HIGH);
      redLEDState = HIGH;
      previousRedLEDMillis = currentMillis;
    }
    else{
      digitalWrite(redLED,LOW);
      redLEDState = LOW;
      previousRedLEDMillis = currentMillis;
    }      
  }
}

Any other input on how I'm going about learning this, direction to online resources, anything really... much appreciated.

Thanks!
Mike.

You aren't detecting transitions on the button, so naturally it will flash quickly.

omg... I built that into the tilt handler but not the button handler....

I feel like a doofus :stuck_out_tongue:

Thank you very much!
Mike.