Go Down

Topic: New State machine tutorial (Read 3791 times) previous topic - next topic

jbellavance

IHi,

I have just finished a 3 parts State machine tutorial.

You can find it here.

Let me know what you think about it.

Jacques
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Welease_Woggah

Bit of a leap of faith to think that someone new to FSMs will know what an enum is.


jbellavance

Quote
What is a state machine?
It is a way to describe what a system is designed to do (output) depending of the state is presently in. As an example, we could use an LED. An LED can only be in one of two states: {ON, OFF}. Each state is mutually exclusive of the other. An LED cannot be simultaneously ON and OFF. There has to be one of the states that have to be declared as the start state. We will also have to remember in which state the machine currently is (the current state).
enum LedStates{ON, OFF};  //The names of all states
LedStates ledState = ON;  //The start state (and the current state after that)
I believe that those two lines, along with the the paragraph above them is simple enough to understand.

Someone new to the state machine concept are not necessarily new to programming. This tutorial is not about C++. It is about implanting a state machine in Arduino's environment which happens to be written in C++.

I my mind, using a "switch" construct is not easier than using enum, since I have met on this site, a few persons that only uses "if" constructs, "since you can to everything with that construct" and won't have anything to do with "switch". But I insist in using it because the "switch" construct is , IMHO, ideal to describe a state machine's behaviour.

So, yes, it is a (double) leap of faith.

Jacques
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

LukeAWarren

#3
Feb 07, 2018, 04:59 am Last Edit: Feb 07, 2018, 04:14 pm by LukeAWarren
Just finished Part 1.  This is great, thank you so much!

I'm using an Arduino Uno and had to ground my push button with a 10k ohm resistor:

https://www.arduino.cc/en/Tutorial/Button


kumarshuvo


jbellavance

I am glad if it helps.

Jacques
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

mennamorato

Thanks this is just what i was looking for to translate a growing messy project into something reasonable.

noweare

I haven't done the exercises leading up to the elevator problem. I just started trying to solve it but it is tougher than I expected. Esp. the part of taking button presses and responding to them as the elevator is moving in the same direction as the request. Sounds like a queue needs to be used but I will go through the tutorials.

noweare

In the TwoStateMachines.ino of Part1 tutorial   this routine:

void switchMachine() {
  byte pinIs = digitalRead(switchPin);
  if (switchMode == PULLUP) pinIs = !pinIs;
  switch (switchState) {
    case IS_OPEN:    { if(pinIs == HIGH) switchState = IS_RISING;  break; }
    case IS_RISING:  {                   switchState = IS_CLOSED;  break; }
    case IS_CLOSED:  { if(pinIs == LOW)  switchState = IS_FALLING; break; }
    case IS_FALLING: { toggleMachine();  switchState = IS_OPEN;    break; }
  }

Turns the led off but how does the Led get turned on again ?


jbellavance

Hi,

In fact, it is toggleMachine() that turns the LED on and/or off.

Jacques
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up