user46332: I have a finite state machine that goes to a function called patternOne, I can't seam to get out of this function though once I am in it and get back to the finite state machine. When I press a button my serial port shows it's been pressed but still stays in the function patternOne, I don't know how to get out of it and why this is happening. My code is below: 
#include <SPI.h>
SPISettings settingsA(2000000, MSBFIRST, SPI_MODE1);
#include <SFE_MicroOLED.h>  // Include the SFE_MicroOLED library
#include <Wire.h>  // Include Wire if you're using I2C
volatile unsigned long prevTime = 0;

int time_interval_for_chip = 5;
int blinking_interval = 500;
unsigned long current_time = 0;
#define PIN_RESET A6  // Connect RST to pin 9

#define DC_JUMPER 0

MicroOLED oled(PIN_RESET, DC_JUMPER);// I2C declaration

const int buttonpin = A8;
volatile int buttonState = 0;   // variable for reading the pushbutton status
volatile int buttonState2 = 0;
int buttonstate3 = 1;
const int buttonpin2 = A7;

enum State {flip, intro, option1, pattern1, intro_flip, option1_flipped, pattern1_flipped, pattern2, pattern3};
//enum is the data type that consists of user defined varaibles ie like the words pattern, intro, ext
State state = flip; //telling the intial state to start at intro

unsigned int column_pattern1[] = {0b0000000100000100, 0b0000001000001000,0b0100000010000010};
//unsigned int column_off_pattern1[]={0b0000000000000100, 0b00000000000001000,0b0100000000000010};
const int css1 = 10;
const int css2 = A0;
void setup() {
  oled.begin();    // Initialize the OLED
  oled.clear(ALL); // Clear the display's internal memory
  //  oled.display();  // Display what's in the buffer (splashscreen)
  //  delay(1000);     // Delay 1000 ms
  oled.clear(PAGE); // Clear the buffer.
  //


  pinMode(css1, OUTPUT);
  pinMode(css2, OUTPUT);
  Serial.begin(9600);
  SPI.begin();
  SPI.beginTransaction(settingsA);

  pinMode(buttonpin, INPUT_PULLDOWN);
  pinMode(buttonpin2, INPUT_PULLDOWN);
  // watermark generates this interrupt
  attachInterrupt(buttonpin, button_pressed, FALLING); // interrrupt 1 is data ready
  //attachInterrupt(buttonpin2, button_pressed2, FALLING); // interrrupt 1 is data ready

}

void loop() {


  // blinking output 4 and 8 while using a finite state machine to change from intro to a patter
  //turning off the desplay when a pattern is chosen

  switch (state) {
    case flip:
    oled.setFontType(1);
  oled.setCursor(0, 0);  // Set the text cursor to the upper-left of the screen.
  //oled.setFontType(2);
  //  oled.setCursor(0, 0);
  oled.print(" Flip  Screen?");
  oled.setFontType(0);
  oled.setCursor(0, 30);
  oled.print("yes");
  oled.setCursor(40, 30);
  oled.print("no");
  oled.circle(10, 43, 3.5);
  oled.circle(50, 43, 3.5);
  oled.display();
  buttonState = 0;


  if (digitalRead(buttonpin2) == HIGH) {
        oled.circleFill(50, 43, 3.5);
        oled.display();
        oled.clear(PAGE); // Clear the buffer.
        delay(500);
        //buttonstate3=0;
     state=intro;
  }
   if (digitalRead(buttonpin) != buttonState) {
    oled.circleFill(10, 43, 3.5);
     oled.display();
        oled.clear(PAGE); // Clear the buffer.
        delay(500);
        state=intro_flip;
   }
  break;


    case intro:

      oled.setFontType(1);
      oled.setCursor(0, 0);  // Set the text cursor to the upper-left of the screen.
      //oled.setFontType(2);
      //  oled.setCursor(0, 0);
      oled.print("choose pattern");
      oled.circle(10, 40, 7);
      oled.display();
      buttonState = 0;

      if (digitalRead(buttonpin) != buttonState) {

        buttonState = 0; //reset button state
        oled.circleFill(10, 40, 7);
        oled.display();
        delay(500);
        state = option1;

      }
      break;


    case option1:
      oled.clear(PAGE); // Clear the buffer.

      //delay(300);
      oled.setFontType(0);
      oled.setCursor(0, 0);  // Set the text cursor to the upper-left of the screen.
      //oled.setFontType(2);
      //  oled.setCursor(0, 0);
      oled.print("Pattern 1");
      oled.setCursor(0, 10);
      oled.print("next");
      oled.setCursor(40, 10);
      oled.print("yes");
      oled.circle(10, 40, 7);
      oled.circle(50, 40, 7);
      oled.display();

      if (digitalRead(buttonpin2) == HIGH) {
        Serial.println("right button pressed");


        oled.circleFill(50, 40, 7);
        oled.display();
        delay(500);
        state = pattern1;

      }
      break;
        case pattern1:
      oled.clear(PAGE);
      oled.display();

      patternONE();
      buttonState=0;
      if (digitalRead(buttonpin) != buttonState) {
        Serial.println("left button pressed");
        //         //delay to register that the button was pressed
        delay(500);
        //
        state = intro;
      }
      break;

////////////////////////////////////////////////////////
///////////FLIPPED FINITE STATE MACHINE/////////////////
////////////////////////////////////////////////////////


case intro_flip:
  oled.flipVertical(true);
    oled.flipHorizontal(true);
     oled.setFontType(1);
      oled.setCursor(0, 0);  // Set the text cursor to the upper-left of the screen.
      //oled.setFontType(2);
      //  oled.setCursor(0, 0);
      oled.print("choose pattern");
      oled.circle(50, 40, 7);
      oled.display();
      buttonState = 0;

      if (digitalRead(buttonpin) != buttonState) {

        buttonState = 0; //reset button state
        oled.circleFill(50, 40, 7);
        oled.display();
        delay(500);
        state = option1_flipped;

      }
      break;

      case option1_flipped:
oled.flipVertical(true);
    oled.flipHorizontal(true);
      oled.clear(PAGE); // Clear the buffer.

      //delay(300);
      oled.setFontType(0);
      oled.setCursor(0, 0);  // Set the text cursor to the upper-left of the screen.
      //oled.setFontType(2);
      //  oled.setCursor(0, 0);
      oled.print("Pattern 1");
       oled.setCursor(0, 10);
      oled.print("yes");
      oled.setCursor(40, 10);
      oled.print("next");
      oled.circle(10, 40, 7);
      oled.circle(50, 40, 7);
      oled.display();

      if (digitalRead(buttonpin2) == HIGH) {
        Serial.println("right button pressed");


        oled.circleFill(10, 40, 7);
        oled.display();
        delay(500);
        state = pattern1_flipped;

      }
      break;

       case pattern1_flipped:
      oled.clear(PAGE);
      oled.display();

      patternONE();
//      buttonState=0;
      if (digitalRead(buttonpin) != buttonState) {
        Serial.println("left button pressed");
        //         //delay to register that the button was pressed
        delay(500);
        //
        state = intro_flip;
      }
      break;


  }
}////////////////////////////////////////////////////////
///////////Pattern/////////////////
////////////////////////////////////////////////////////
void patternONE() {
buttonState = 0;
   unsigned int chip1 = 0;
  unsigned int chip2 = 0;
  byte inbyte = 0;
  byte inbyte2 = 0;

  for (int i = 0; i < 3; ++i) {



    digitalWrite(css2, LOW);
    chip2 = SPI.transfer16(column_pattern1[i]);
    Serial.print("chip2 column off=");
    Serial.println(chip2, BIN);
    digitalWrite(css2, HIGH);


    digitalWrite(css1, LOW);
    chip1 = SPI.transfer16(0b0001010000000000);
    Serial.print("chip1 row 8 on high driver= ");//not the chip is a look behind so the print statments are the previous output
    Serial.println(chip1, BIN);
    digitalWrite(css1, HIGH);

    delay(1);
    digitalWrite(css1, LOW);
    chip1 = SPI.transfer16(0b0100000100000000);
    Serial.print("chip1 row 4 and 6 on low driver= ");
    Serial.println(chip1, BIN);
    digitalWrite(css1, HIGH);

    delay(2000);

    digitalWrite(css1, LOW);
    chip1 = SPI.transfer16(0b0001010001010000);
    Serial.print("chip1 row 8 on low driver= ");
    Serial.println(chip1, BIN);
    digitalWrite(css1, HIGH);

    delay(1);

    digitalWrite(css1, LOW);
    chip1 = SPI.transfer16(0b0100000100000100);
    Serial.print("chip1 row 4 and 6 on high driver= ");
    Serial.println(chip1, BIN);
    digitalWrite(css1, HIGH);

    digitalWrite(css2, LOW);
    chip2 = SPI.transfer16(column_pattern1[i]);
    Serial.print("chip2 column on high driver= ");
    Serial.println(chip2, BIN);
    digitalWrite(css2, HIGH);

    //turn off the previous column 
//    digitalWrite(css2, LOW);
//    chip2 = SPI.transfer16(column_off_pattern1[i]);
//    Serial.print("chip2 column on high driver= ");
//    Serial.println(chip2, BIN);
//    digitalWrite(css2, HIGH);
//    
    delay(2000);


  }

}

// watermark generates this interrupt
void button_pressed()
{
  cli();
  //confirmitng that the button was pressed and that its not just like a bounce" 
  if (millis() - prevTime > 50) {
    Serial.println("left button pressed");
    buttonState = 1;
  Serial.println("buttonState =");
  Serial.println(buttonState);
  }

  sei();
  prevTime = millis();

}


user313: From your question, it sounds like you are expecting PatternONE() to return on a button press but I don't see any code in the function that refers to a button or acts on one. The function defines a local variable buttonState and initializes it to 0, but never refers to it again.
I suspect you meant that as a declaration of the global buttonState, but you have two problems: 1) the function never uses buttonState to initiate a return; and 2) the local definition buttonState hides the global one while it is in scope; i.e., while the function is executing.

46332: <p>I have a finite state machine that goes to a function called patternOne, I can't seam to get out of this function though once I am in it and get back to the finite state machine. When I press a button my serial port shows it's been pressed but still stays in the function patternOne, I don't know how to get out of it and why this is happening. My code is below: </p>

<pre><code>#include &lt;SPI.h&gt;
SPISettings settingsA(2000000, MSBFIRST, SPI_MODE1);
#include &lt;SFE_MicroOLED.h&gt;  // Include the SFE_MicroOLED library
#include &lt;Wire.h&gt;  // Include Wire if you're using I2C
volatile unsigned long prevTime = 0;

int time_interval_for_chip = 5;
int blinking_interval = 500;
unsigned long current_time = 0;
#define PIN_RESET A6  // Connect RST to pin 9

#define DC_JUMPER 0

MicroOLED oled(PIN_RESET, DC_JUMPER);// I2C declaration

const int buttonpin = A8;
volatile int buttonState = 0;   // variable for reading the pushbutton status
volatile int buttonState2 = 0;
int buttonstate3 = 1;
const int buttonpin2 = A7;

enum State {flip, intro, option1, pattern1, intro_flip, option1_flipped, pattern1_flipped, pattern2, pattern3};
//enum is the data type that consists of user defined varaibles ie like the words pattern, intro, ext
State state = flip; //telling the intial state to start at intro

unsigned int column_pattern1[] = {0b0000000100000100, 0b0000001000001000,0b0100000010000010};
//unsigned int column_off_pattern1[]={0b0000000000000100, 0b00000000000001000,0b0100000000000010};
const int css1 = 10;
const int css2 = A0;
void setup() {
  oled.begin();    // Initialize the OLED
  oled.clear(ALL); // Clear the display's internal memory
  //  oled.display();  // Display what's in the buffer (splashscreen)
  //  delay(1000);     // Delay 1000 ms
  oled.clear(PAGE); // Clear the buffer.
  //


  pinMode(css1, OUTPUT);
  pinMode(css2, OUTPUT);
  Serial.begin(9600);
  SPI.begin();
  SPI.beginTransaction(settingsA);

  pinMode(buttonpin, INPUT_PULLDOWN);
  pinMode(buttonpin2, INPUT_PULLDOWN);
  // watermark generates this interrupt
  attachInterrupt(buttonpin, button_pressed, FALLING); // interrrupt 1 is data ready
  //attachInterrupt(buttonpin2, button_pressed2, FALLING); // interrrupt 1 is data ready

}

void loop() {


  // blinking output 4 and 8 while using a finite state machine to change from intro to a patter
  //turning off the desplay when a pattern is chosen

  switch (state) {
    case flip:
    oled.setFontType(1);
  oled.setCursor(0, 0);  // Set the text cursor to the upper-left of the screen.
  //oled.setFontType(2);
  //  oled.setCursor(0, 0);
  oled.print(" Flip  Screen?");
  oled.setFontType(0);
  oled.setCursor(0, 30);
  oled.print("yes");
  oled.setCursor(40, 30);
  oled.print("no");
  oled.circle(10, 43, 3.5);
  oled.circle(50, 43, 3.5);
  oled.display();
  buttonState = 0;


  if (digitalRead(buttonpin2) == HIGH) {
        oled.circleFill(50, 43, 3.5);
        oled.display();
        oled.clear(PAGE); // Clear the buffer.
        delay(500);
        //buttonstate3=0;
     state=intro;
  }
   if (digitalRead(buttonpin) != buttonState) {
    oled.circleFill(10, 43, 3.5);
     oled.display();
        oled.clear(PAGE); // Clear the buffer.
        delay(500);
        state=intro_flip;
   }
  break;


    case intro:

      oled.setFontType(1);
      oled.setCursor(0, 0);  // Set the text cursor to the upper-left of the screen.
      //oled.setFontType(2);
      //  oled.setCursor(0, 0);
      oled.print("choose pattern");
      oled.circle(10, 40, 7);
      oled.display();
      buttonState = 0;

      if (digitalRead(buttonpin) != buttonState) {

        buttonState = 0; //reset button state
        oled.circleFill(10, 40, 7);
        oled.display();
        delay(500);
        state = option1;

      }
      break;


    case option1:
      oled.clear(PAGE); // Clear the buffer.

      //delay(300);
      oled.setFontType(0);
      oled.setCursor(0, 0);  // Set the text cursor to the upper-left of the screen.
      //oled.setFontType(2);
      //  oled.setCursor(0, 0);
      oled.print("Pattern 1");
      oled.setCursor(0, 10);
      oled.print("next");
      oled.setCursor(40, 10);
      oled.print("yes");
      oled.circle(10, 40, 7);
      oled.circle(50, 40, 7);
      oled.display();

      if (digitalRead(buttonpin2) == HIGH) {
        Serial.println("right button pressed");


        oled.circleFill(50, 40, 7);
        oled.display();
        delay(500);
        state = pattern1;

      }
      break;
        case pattern1:
      oled.clear(PAGE);
      oled.display();

      patternONE();
      buttonState=0;
      if (digitalRead(buttonpin) != buttonState) {
        Serial.println("left button pressed");
        //         //delay to register that the button was pressed
        delay(500);
        //
        state = intro;
      }
      break;

////////////////////////////////////////////////////////
///////////FLIPPED FINITE STATE MACHINE/////////////////
////////////////////////////////////////////////////////


case intro_flip:
  oled.flipVertical(true);
    oled.flipHorizontal(true);
     oled.setFontType(1);
      oled.setCursor(0, 0);  // Set the text cursor to the upper-left of the screen.
      //oled.setFontType(2);
      //  oled.setCursor(0, 0);
      oled.print("choose pattern");
      oled.circle(50, 40, 7);
      oled.display();
      buttonState = 0;

      if (digitalRead(buttonpin) != buttonState) {

        buttonState = 0; //reset button state
        oled.circleFill(50, 40, 7);
        oled.display();
        delay(500);
        state = option1_flipped;

      }
      break;

      case option1_flipped:
oled.flipVertical(true);
    oled.flipHorizontal(true);
      oled.clear(PAGE); // Clear the buffer.

      //delay(300);
      oled.setFontType(0);
      oled.setCursor(0, 0);  // Set the text cursor to the upper-left of the screen.
      //oled.setFontType(2);
      //  oled.setCursor(0, 0);
      oled.print("Pattern 1");
       oled.setCursor(0, 10);
      oled.print("yes");
      oled.setCursor(40, 10);
      oled.print("next");
      oled.circle(10, 40, 7);
      oled.circle(50, 40, 7);
      oled.display();

      if (digitalRead(buttonpin2) == HIGH) {
        Serial.println("right button pressed");


        oled.circleFill(10, 40, 7);
        oled.display();
        delay(500);
        state = pattern1_flipped;

      }
      break;

       case pattern1_flipped:
      oled.clear(PAGE);
      oled.display();

      patternONE();
//      buttonState=0;
      if (digitalRead(buttonpin) != buttonState) {
        Serial.println("left button pressed");
        //         //delay to register that the button was pressed
        delay(500);
        //
        state = intro_flip;
      }
      break;


  }
}////////////////////////////////////////////////////////
///////////Pattern/////////////////
////////////////////////////////////////////////////////
void patternONE() {
buttonState = 0;
   unsigned int chip1 = 0;
  unsigned int chip2 = 0;
  byte inbyte = 0;
  byte inbyte2 = 0;

  for (int i = 0; i &lt; 3; ++i) {



    digitalWrite(css2, LOW);
    chip2 = SPI.transfer16(column_pattern1[i]);
    Serial.print("chip2 column off=");
    Serial.println(chip2, BIN);
    digitalWrite(css2, HIGH);


    digitalWrite(css1, LOW);
    chip1 = SPI.transfer16(0b0001010000000000);
    Serial.print("chip1 row 8 on high driver= ");//not the chip is a look behind so the print statments are the previous output
    Serial.println(chip1, BIN);
    digitalWrite(css1, HIGH);

    delay(1);
    digitalWrite(css1, LOW);
    chip1 = SPI.transfer16(0b0100000100000000);
    Serial.print("chip1 row 4 and 6 on low driver= ");
    Serial.println(chip1, BIN);
    digitalWrite(css1, HIGH);

    delay(2000);

    digitalWrite(css1, LOW);
    chip1 = SPI.transfer16(0b0001010001010000);
    Serial.print("chip1 row 8 on low driver= ");
    Serial.println(chip1, BIN);
    digitalWrite(css1, HIGH);

    delay(1);

    digitalWrite(css1, LOW);
    chip1 = SPI.transfer16(0b0100000100000100);
    Serial.print("chip1 row 4 and 6 on high driver= ");
    Serial.println(chip1, BIN);
    digitalWrite(css1, HIGH);

    digitalWrite(css2, LOW);
    chip2 = SPI.transfer16(column_pattern1[i]);
    Serial.print("chip2 column on high driver= ");
    Serial.println(chip2, BIN);
    digitalWrite(css2, HIGH);

    //turn off the previous column 
//    digitalWrite(css2, LOW);
//    chip2 = SPI.transfer16(column_off_pattern1[i]);
//    Serial.print("chip2 column on high driver= ");
//    Serial.println(chip2, BIN);
//    digitalWrite(css2, HIGH);
//    
    delay(2000);


  }

}

// watermark generates this interrupt
void button_pressed()
{
  cli();
  //confirmitng that the button was pressed and that its not just like a bounce" 
  if (millis() - prevTime &gt; 50) {
    Serial.println("left button pressed");
    buttonState = 1;
  Serial.println("buttonState =");
  Serial.println(buttonState);
  }

  sei();
  prevTime = millis();

}
</code></pre>
 313: <p>From your question, it sounds like you are expecting PatternONE() to return on a button press but I don't see any code in the function that refers to a button or acts on one. The function defines a local variable <code>buttonState</code> and initializes it to 0, but never refers to it again.</p>

<p>I suspect you meant that as a declaration of the global <code>buttonState</code>, but you have two problems: 1) the function never uses buttonState to initiate a return; and 2) the local definition <code>buttonState</code> hides the global one while it is in scope; i.e., while the function is executing.</p>
