State machine or something else ?

Hi Guy's

I am trying hard to figure a way to make my program respond 3 different ways with one capsense button. I have looked at the state machine but cannot seem to be able to download the FSM library. I would like to be able to make my program either turn the led on, off or decide what to do from an analog input. My question is Is the state machine the best way to do this or can i use the ++ function to cycle thru 1,2,3 then back to 1 so it cycles thru ? Also how would i do this, i am still pretty new to this. Thanks BlackSnake

I am not quite sure what you mean by state machine library, since to me this is normally a switch-case function. Something like this:

#define INIT                0
#define STA_LED_ON    1
#define STA_LED_OFF   2
... //include the rest of your defines here. 

byte status = 0; 

loop() {

switch (status) {
    case INIT: {
        //turn led off.
        //initialize whatever you need
        status = STA_LED_OFF;
        break;}
    case STA_LED_OFF: {
        //check button. 
        if (button == PRESSED)  {
            LED = ON; 
            status = STA_LED_ON; }
        break; }
    case STA_LED_ON: {
        if (button == PRESSED)  {
            LED = OFF; 
            status = STA_LED_OFF; }
        break; }
    default : { //print something just in case; break;}
}
}

So this allows you to run a specific code depending on which status is your machine. You can also, create state machines inside states for certain parts of your program. From the top of my head, I remember using this for keypad functions where the key would have a different function depending on the state the machine was.

Did this answer your question? Without more info, there isn't much more I can help you with.

Thanks for your help i will need time to try this, i will show you what i have so far.
If you type in finite state machine in the search you will see what i am talking about.
It says to download the FSM but it’s a 2 byte file.
here is my code

 LCD CONNECTIONS:
 * LCD RS PIN TO DIGITAL PIN 12
 * LCD ENABLE PIN TO DIGITAL PIN 11
 * LCD D4 PIN TO DIGITAL PIN 5
 * LCD D5 PIN TO DIGITAL PIN 4
 * LCD D6 PIN TO DIGITAL PIN 3
 * LCD D7 PIN TO DIGITAL PIN 2
 * RED TO +5V 
 * BLACK TO GROUND
*/
// FOLLOWING ARE THE LIBRARIES REQUIRED FOR DISPLAY & TIMING.
#include <LiquidCrystal.h>
#include <Elapsed.h>

// INTERFACE PINS FOR LCD LIBRARY.
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int LINK = LOW;
int LED = 9;    // VARIABLE FOR LED / RELAY OUTPUT.
int PRESSED = false; // VARIABLE FOR TIMING ELEMENT.
int DEPRESSED = false;
int DEACTIVATE = 0;
int ACTIVATE = 0; // VARIABLE FOR TIMING ELEMENT.
#define INTERVAL 30 // LENGTH OF DELAY BEFORE OUTPUT, IN SECONDS
#define INTERVAL2 10 // SECOND INTERVAL FOR OFF DELAY.
Elapsed timer; // THE VARIABLE FOR TIME.

void setup()
{
 
lcd.begin(16, 2); // SETS THE NUMBER OF LINES & CHARACTERS ON THE DISPLAY.
pinMode(LED, OUTPUT); // SETS THE LED AS AN OUTPUT.
}
// CREATE SOME VARIABLES TO STORE DATA.
float MAINVOLT = 0; // VARIABLE FOR MAIN BATTERY VOLTAGE.
float AUXVOLT = 0; // VARIABLE FOR AUX BATTERY VOLTAGE.
float MBINPUT = 0; // VARIABLE FOR ACTUAL ANALOG INPUT.
float ABINPUT = 0; // VARIABLE FOR ACTUAL ANALOG INPUT.


void loop() {     // let's get measurin'

MBINPUT = analogRead(0); // READ ANALOG INPUT 1 AND STORE RESULT IN MBINPUT.
ABINPUT = analogRead(1); // READ ANALOG INPUT 0 AND STORE RESULT IN ABINPUT.
MAINVOLT = (MBINPUT*19.99)/1024; // CONVERTS RAW INPUT DATA INTO MILLIVOLTS.
AUXVOLT = (ABINPUT*19.99)/1024; // CONVERTS RAW INPUT DATA INTO MILLIVOLTS.

lcd.setCursor(0,0);
lcd.print("MAIN BATT:"); // Prints "Main Batt" on LCD Display
lcd.setCursor(12,0);
lcd.print(MAINVOLT,1); // Prints voltage from Sensor 1 (Analog Input 0)
if (AUXVOLT >= 5.0 && LINK == LOW)
{
   lcd.setCursor(0, 1); // Selects line 2 on LCD Display
   lcd.print("AUX BATT:"); // Prints "Aux Batt:" on LCD Display
   lcd.setCursor(9,1);
   lcd.print("   ");
   lcd.setCursor(12,1);
   lcd.print(AUXVOLT,1);
}
if (MAINVOLT >= 13.2 && !PRESSED && ACTIVATE != HIGH) // SETS THE VOLTAGE LEVEL 
// THAT THE RELAY WILL TURN ON AND STARTS THE TIMING.

  {
   DEACTIVATE = LOW;
   ACTIVATE = HIGH;
   PRESSED = true;
   timer.reset(); // RESET THE TIMER.
  }
  
if (PRESSED && timer.intervalMs()>(INTERVAL * 1000) && MAINVOLT >= 13.2) // COMPARES CURRENT MILLIS AND TIMING.
  {
    
   LINK = HIGH;
   PRESSED = false; // SETS PRESSED TO FALSE
   digitalWrite(LED, HIGH); // SETS LED OUTPUT TO HIGH.
  }
  
if (MAINVOLT <= 12.6 && !DEPRESSED && DEACTIVATE != HIGH) // SETS VOLTAGE THAT RELAY WILL DISCONNECT BOTH BATTERIES.
  {
   DEACTIVATE = HIGH;
   DEPRESSED = true;
   LINK = LOW;
   ACTIVATE = LOW;
   timer.reset();
  }
  if (DEPRESSED && timer.intervalMs()>(INTERVAL2 * 1000) && MAINVOLT <= 12.6) // COMPARES CURRENT MILLIS AND TIMING.
  {
   DEPRESSED = false; // SETS PRESSED TO FALSE
   digitalWrite(LED, LOW); // SETS LED OUTPUT TO HIGH.
   lcd.setCursor(10,1);
   lcd.print("  ");
   lcd.setCursor(12,1);
   lcd.print(AUXVOLT,1);
  } 
if (AUXVOLT <= 5.0) // SETS SAFETY BARRIER FOR AUX BATTERY.
  {
   LINK = LOW;
   ACTIVATE = LOW; // SETS ACTIVATE TO LOW.
   digitalWrite(LED, LOW); // SETS LED OUTPUT TO LOW.
   lcd.setCursor(0,1);
   lcd.print(" NO AUX BATTERY ");
  }
if (LINK == HIGH)
 {
  lcd.setCursor(10,1);
  lcd.print("LINKED");
 }

lcd.setCursor(0,0);
lcd.print("MAIN BATT:"); // Prints "Main Batt" on LCD Display
lcd.setCursor(12,0);
lcd.print(MAINVOLT,1); // Prints voltage from Sensor 1 (Analog Input 0)
/*lcd.setCursor(0, 1); // Selects line 2 on LCD Display
 lcd.print("AUX BATT:"); // Prints "Aux Batt:" on LCD Display
*/
lcd.setCursor(0, 0); // Resets LCD display for next loop
delay(50); // 50ms Delay
}

Why so complicated? Use the Button library, at the top of loop see it was pressed, if it was change state, then do a switch:case & run the code for that state.

BlackSnake: If you type in finite state machine in the search you will see what i am talking about. It says to download the FSM but it's a 2 byte file.

I did... still don't understand why you'd want to use that. It's just another layer of software taking up space and memory.

Your current code is quite confusing and I haven't got the time right now to look inside, but start by creating a list of states in a paper sheet and connect states with events (push button, analog value change, etc), then it's fairly simple to move from that sketch to code. :\

@BlackSnake: You code looks very SHOUTY and difficult to read.

You've got a mixture of variables and macros in uppercase. Commonly, "#define" macros are about the only things that should be in all uppercase. Comments most certainly shouldn't.