64 yr old newby would like some help

larryd:
If it is easy to read and your audience doesn’t have to strain to find things you 'will' get help.

You provided the best reason for me to do what you have suggested as you possibly could.

I spent most of the morning getting my code formatted like yours. I could have just saved yours but by doing it myself I understand better.

I spent the rest of this morning and this far this after noon reading state machine code and reference material. My brain hurts!

Is the main reason to do state machine to save memory?

I'm going to take a break for a while and let the fog in my head clear.

Thanks

The StateMachineProject.ini file I attached in post #235 has everything that your sketch has.
If you give it a try, you will see it gives the same results as yours.

Don’t beat your head in trying to understand what’s happening, ask questions when you need help with an section.

A State Machine format has many advantages:

  • It is logical, clean, easy to follow and maintain.
  • All state coding follows one format.
  • It is extremely easy to add new states and associated tasks.
  • In this example, all timing is handled in one place, by the simple CheckTime() function.
  • States can be run once, repeatedly run, suspended/disabled and schedule.
  • More importantly, a State Machine lends itself to 'non-blocking' behaviour.
  • Spaghetti style code is avoided when you follow proper State Machine formatting.

After using State Machines a few times in your larger projects, you will not go back to sequential code writing.

Here is how you can modify the StateMachineProject example I offered.
We will add 'Green house heater control' to the code.

Lines that have 1.01 append to them were added or modified.

See:
Attached StateMachineProject.ino

If you are at all serious about this stuff, you should master this example !

See Post #248 for the latest version of the sketch

Larry are you sure you grabed the right file? I can't find any lines with 1.01.

Yes

If you download the file in post #242, you will see this at the top:
//*******************************************************************************
//  YY/MM/DD  Version    Reason
//  18/01/08  1.01       Added GHheatControl section

Then you will see 5 lines further down the sketch with 1.01 at the far right side of the screen.

You may have to 'maximize' your IDE window.

Edit:
#include <DHT.h>
#include <DHT_U.h>
#include "DHT.h" <----<<<< this is redundant

Alright I got it. Had to delete some older stuff first.

Are you sure I'm ready for this? I don't even have my training wheels off!!

Are you trying to make a programmer out of me?

I can kinda follow what's happening but I'm not understanding a whole lot of this.

When you first threw this out, it made me think that you were fishing to see if I'd bite. I went to Red Lake Ontario for 20 some years. I'd move to a new spot, change up the bait, etc. just to see if the walleye's would bite. I can just see you "changing the bait" - smiling the whole time - to see if I'll take the bait. :slight_smile: :slight_smile:

I'll bite. I'll swallow this deep. Be prepared cause I'll have LOT"S of questions.

There are others following this from the # of downloads. You folks pipe in here. You may ask the question that makes my "ah ha" moment. Believe me I can use all of of those I can get!

Hi,
We will make sure you don't catch any Red Herrings.... :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Tom.. :slight_smile:

Software bug was found in the StateMachineProject.ino file.

Use the attached file below for corrected code.

StateMachineProject.ino (11.9 KB)

I've done some poking around today to try to understand this better.

Moore published in 1956 and is most restrictive.

Mealy published in 1955 and can work with inputs to the system and within transitions.

Harel in the 80's working with the aviation industry and added some extras. (Notice the 30 year delay)

These guys are all mathematicians!!!!!!!!!!!!

This interests me as I had an uncle that was a mathematics professor and another uncle that programmed for NASA for a while. That would have been back in the 50's - 70's.

Given that the computer used on the Moon landing had less power that the first basic hand calculators - It had to be a state machine. There were lot's of buttons pushed and switched flicked.

I'm starting to get it - I think. How am I doing?

Questions coming up next.

We have a wrench ready for the "Training Wheels".

Having the wrench out is encouraging!

I'm not quite sure how to present my questions. I can copy the lines and paste in the post (that's how I'm going to start). If there is another way let me know what you want to see.

#1. State could be any term? Sate makes sense just curious.

I suppose, 'State' could mean just about anything.

In your example we are checking different sections of code to see if they need attention.

In the latest version, we have defined 6 possible states that your machine can be in.
StateStart, StateDHT22, StateUniversal, StateSoil_moisture, StatePrint and StateGHheatControl.

StateStart is when we first power on the controller. It is entirely possible that you do nothing here.
StateDHT22 is when we check to see if it is time to handle the DHT22 stuff.
. . .
StateGHheatControl is when we determine if the green house heater should be turned on or off.

In this example, a dedicated timer is running for each possible state. The timer is used to determine if that 'State' code should be executed.

larryd:
StateGHheatControl is when we determine if the green house heater should be turned on or off.

By green house heater you are thinking an emergency heat source, electric?

My primary heat will be: #1 solar hot water - 550 gal storage. #2 Compost - which will also feed into the water. Last options will be to use juice.

Your last comment answered a couple other questions.

By green house heater you are thinking an emergency heat source, electric?

I just suggested that.

We had a green house 20 years back. It used an electric heater for auxiliary heat.

States mState = StateStart; //we start out in this machine state

Please explain what this is doing.

There are 6 states defined below:
//define the available states that we can have for this sketch
enum States
{
//add more states as needed
StateStart, StateDHT22, StateUniversal, StateSoil_moisture, StatePrint, StateGHheatControl // 1.01
};

States mState = StateStart; //we start out in this machine state

enum States >>>---> defines a new 'type' called State
This similar to int, byte and float etc.
Where we have type int, type byte and type float

Below, we are giving names to all the possible states in your sketch.
You can refer to these in your code just like you do with other variables.
These are 'integers values' starting at 0 going up.
Where StateStart = 0, StateDHT22 = 1, StateUniversal = 2 . . .StateGHheatControl = 5.
You really don't need to know what these variables equal as the compiler figures it out for you.
{
//add more states as needed
StateStart, StateDHT22, StateUniversal, StateSoil_moisture, StatePrint, StateGHheatControl // 1.01
};

States mState = StateStart; >>>---> we are creating a variable called mState (machine state) and are setting it equal to what StartState equals.
mState can have the value of 0-5.
This is similar to int myVariable = 0. Where int is the 'type', myVariable is similar to mState and 0 is similar to StartState

Test time: Let's say we have the following lines of code, what is printed?
mState = StatePrint;
Serial.println(mState);

Ok - I'm kinda following that. I'll refer back to it a few more times before I'm done.

Next;

unsigned long testMillis; Questions on what this is used for.
unsigned long currentMillis; I get what this is doing.
unsigned long BlockingMillis; Questions on what this is used for.

You skipped the test.

Test time: Let's say we have the following lines of code, what is printed?
mState = StatePrint;
Serial.println(mState);

unsigned long testMillis; We are define a new global variable called testMillis. It has a 'type' of
unsigned long. During sketch writing, we often use temporary variables to debug things. Hence testMillis was used for debugging purposes only.

unsigned long currentMillis;

unsigned long BlockingMillis; We are define a new global variable called BlockingMillis. It has a 'type' of unsigned long. This is a variable that is used in flashing a LED found on pin 13 on the Mega. I use it to show when the program is running properly i.e. if it flashes things are tickety boo.