FSM + neopixels

Hi Guys,

I’m trying to build a system with state machine where LEDs ( 7 neopixels) will be turned on, and then one by one will be turned off.

However probably due to my inexperience, it is going wrong, attached is what I have now.

//Define the states of the machine
#define LED_OFF 0
#define LED_ON_WAIT 1
#define LED_ON 2
#define LED_OFF_WAIT 3

//This is the memory element that contains the state variable
uint8_t fsm_state = LED_OFF;

//This holds the number of milliseconds that have been counted
uint16_t msCounts = 0;

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
#define PIN            7
#define NUMPIXELS      16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 500;
void setup()
{
pixels.begin();
}

// the loop function runs over and over again forever
void loop()
{
  //state machine
  switch (fsm_state)
  {
    case LED_OFF:
      //Statements to execute every time LED_OFF is reached
      for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,0,0)); // Moderately bright green color.
    pixels.show();
      fsm_state = LED_ON_WAIT;
      break;
      
    case LED_ON_WAIT:
      //Statements to execute every time LED_OFF_WAIT is reached
      if(msCounts >= 1000)
      {
        fsm_state = LED_ON;
        msCounts = 0;
      }
      break;
      
    case LED_ON:
      //Statements to execute every time LED_ON is reached
     // digitalWrite(13, HIGH);
     for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.

    pixels.show();
    delay(delayval)
      fsm_state = LED_OFF_WAIT;
      break;
      
    case LED_OFF_WAIT:
      //Statements to execute every time LED_ON_WAIT is reached
      if(msCounts >= 1000)
      {
        fsm_state = LED_OFF;
        msCounts = 0;
      }
      break;
    default:
      break;
  }


  //Time keeping:
  delay(1);              // wait for a millisecond
  if(msCounts < 0xFFFF)  // Don't let the msCounts overflow
  {
    msCounts++;
  }
}

In the first case, i’m getting a crosses initialization of ‘int I’ error.

what is this and how should I try to solve it?

Thanks in advance!

Fsm_neopixels.ino (1.93 KB)

Welcome to the Forum. Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the “Temp” folder and manually remove it. It’s much easier to just view the code in your post.

You declare the variable i multiple times in the same scope. Don’t do that.
You can put one declaration at the top and then just use the variable I in your loop instead of declaring it in each loop.
Or you can use a different variable in each loop.

You are missing a ; after one of your delay() statements.

You are missing come right curly brackets. CTRL T to autoformat helps me find mismatched brackets.

edit: actually, I think the missing curly bracket would fix the multiple declarations in the same scope.
I am guessing that this is what you really intend with one of the loops.

    case LED_OFF:
      //Statements to execute every time LED_OFF is reached
      for (int i = 0; i < NUMPIXELS; i++) {

        // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
        pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Moderately bright green color.
      }
      pixels.show();
      fsm_state = LED_ON_WAIT;
      break;

Thanks!!

It fixed those problems. However, it does not work yet. I'm going to try to figure this out, of course, any ideas are welcome and appreciated!!!

Yes got it, wrong PIN.....

now i still have to find a way to turn them off one by one