Skip to content

Instantly share code, notes, and snippets.

@sibomots
Created March 3, 2022 00:13
Show Gist options
  • Save sibomots/7a266ea76cef239d56c1fbeb9da7a834 to your computer and use it in GitHub Desktop.
Save sibomots/7a266ea76cef239d56c1fbeb9da7a834 to your computer and use it in GitHub Desktop.
Arduino Hints

The code used in Arduino is very C-like.
What trips people up is exactly how the underlying system invokes your code.
You're essentially writing two functions. setup and loop.

The platform of Arduino, will come out of reset and then invoke the setup function ONCE.
then the Platform of Ardruino will call the loop function repeatedly over and over as fast as it can.

What people do (by mistake) is put a while(1) { ... } loop inside the loop function.
They think they need to run an infinite loop themselves. T hey do not need to do that. Let Ardruino call loop for you as fast as it can repeatedly.
What this means though is in your program, global varaibles (variables declared OUTSIDE of loop) need to maintain state.
flags like int x = 1 where x is some flag variable.
so you can decide inside loop to do something if (x == 1) { .....}
and inside loop change x to a different value to cause new flow of execution in the loop..

Here's an example:

// start of the Arduino file

// declare a state variable called x
// We're going to assume that in our program we have three states.
// the state when x = 0, the state when x = 1 and the state when x = 2
//
// You might have other states, more states, or more types of fine-grained approach to differentiate two different code
// paths to take in your software.

// assuming just three states, and we keep track with a variable x
// initialized as 0
int x = 0;

// other code
setup() {
   // your setup code
   // make sure x = 0 on initialization:
   x = 0;
}

loop() 
{

   // Loop is called as often as Ardruino is running and it will be called repeatedly as fast as the platform can
   
   So each time we "enter" loop function we're really making a decision of what to do at this point.
   
   // do stuff here that happens before any decisions are made about the "state"
   // regardless of what the state is.. if necessary.

  switch(x) {
     case 0:  
          // do what should happen at "stage 0"
          // do stuff.
          x = 1;
          break; // important -- don't forget the break statement
     case 1:  // do what should happen at "stage 1"
          // do what should happen at "stage 1"
          // do more stuff
          x = 2;
          break;
     case 2:
          // do stuff at stage 2
          // do stuff
          x = 0;
          break;
   }

   // do stuff that happens after those decisions no matter what stage you are at.

} // end of loop() function.

When this code runs on your hardware this is what effectively happens.

setup()
loop()  (where x = 0) so whatever it means in the case-statement for x = 0, that happens, 
         set x = 1
         it falls out of the swtich block
         and continues to the end of the loop() function.

loop() is called again (now x = 1, because it was set to 1 inside the case block when x = 0 -- read the code!)
       then do whatever it means at "state is 1"
       set x = 2
       then break out
       run to the end of the loop() function
       
loop() is called again, (now x = 2, because it was set to 2 inside the case block when x = 1 --read the code!)
       then do whatever it means at state is 2\
       set x = 0;
       then break out
       run to the end of the loop() function

since x = 0, the "state machine" will go back to the state of when "x = 0"..

so it goes

state 0, state 1, state 2, state 0, state 1, state 2, over and over forever.

YOUR code may have a job to 'read a sensor in state 0'  then in state 1 "peform an action", and so on...  

Remember to declare variables that are needed between calls to loop OUTSIDE of loop (make them global) where you declared x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment