Hi, I am trying to figure out some code for my project. I currently have a working FSM, but now I need to reset to code to the default state if I type 'R' into the serial monitor. Any Help? Thanks.
Always show us a good schematic of your proposed circuit. Show us good images of your ‘actual’ wiring. Give links to components.
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.
would it help if you had a picture of the code I am working on?
Welcome to the forum
Write a function that sets the default state and call it initially from setup() and at any other time that you need to get back to the default state
If you need more detailed help then please post your full sketch, using code tags when you do
A Picture of your code ?
No
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum
What is that? Erase the controller to empty without code?
Maybe erase the stuff on the LCD?
Try this version
#include <LiquidCrystal.h>
LiquidCrystal lcd( A5, A4, 5, 6, 7, 8 ); //define pins to use
// S t a t e M a c h i n e
//============================================^================================================
//the states in our machine
enum MachineStates : byte
{STATE_WELCOME, STATE_UP, STATE_UP_FZN, STATE_DOWN, STATE_DOWN_FZN};
MachineStates state = STATE_WELCOME;
int count; // declare count
unsigned long timer; //set up timer
//============================================^================================================
void setup()
{
Serial.begin(9600);
pinMode(4, INPUT_PULLUP);
lcd.begin( 16, 2 ); //start LCD lcd.clear(); //clear the display
count = 0; //set count to 0
timer = millis(); // timer is counting in milliseconds
} //END of setup()
//============================================^================================================
void loop()
{
//================================================
//for testing, slow things down
//delay(200);
switch (state)
{
//=====================
case STATE_WELCOME:
if (digitalRead(4) == LOW)
{
Serial.println("up");
state = STATE_UP;
//restart the TIMER
timer = millis();
}
else
{
lcd.setCursor( 0, 0 );
lcd.print("WELCOME");
Serial.println("WELCOME");
}
break;
//=====================
case STATE_UP:
if (digitalRead(4) == LOW)
{
Serial.println("UP");
//50ms expired ?
if ( millis() - timer >= 50 )
{
//restart the TIMER
timer += 50;
count++;
lcd.clear();
lcd.setCursor( 0, 0 ); // sets cursor
lcd.print( count ); // display count
}
}
else
{
state = STATE_UP_FZN;
}
break;
//=====================
case STATE_UP_FZN:
if (digitalRead(4) == HIGH)
{
Serial.println("UP FZN");
}
else
{
state = STATE_DOWN;
//restart the TIMER
timer = millis();
}
break;
//=====================
case STATE_DOWN:
if (digitalRead(4) == LOW)
{
Serial.println("DOWN");
if ( millis() - timer >= 50 )
{
//restart the TIMER
timer += 50;
if (count > 0)
{
count--;
lcd.clear();
lcd.setCursor( 0, 0 ); // sets cursor
lcd.print( count ); // display count
}
}
}
else
{
state = STATE_DOWN_FZN;
}
break;
//=====================
case STATE_DOWN_FZN:
if (digitalRead(4) == HIGH)
{
Serial.println("DOWN FZN");
}
else
{
state = STATE_UP;
}
break;
} //END of switch/case
//================================================
if (Serial.available() > 0)
{
char input = Serial.read();
if (toupper(input) == 'R')
{
state = STATE_WELCOME;
count = 0; // Reset the count to 0
lcd.clear();
Serial.println("Program reset to WELCOME state.");
}
}
} //END of loop()
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.