Program not going into switch case

#include <Wire.h>

typedef enum
{
  STARTING,
  PLAYING,
  WAITING
} EnumState;

int value;

int beginTime;
int endTime = 0;
int buttonPin = 2;
int interruptPin = 3;
int addressPin = 8;
int lastEventTime = 0;
int CurrentAddress;
EnumState GameState;
bool CheckMessage = false;
bool SetupAddress = false;

int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;

void setup()
{
  Serial.begin(9600);
  Serial.println("in setup");
  pinMode(buttonPin, INPUT);
  pinMode(interruptPin, OUTPUT);
  pinMode(addressPin, INPUT);
  GameState = WAITING;
  digitalWrite(interruptPin, LOW);
                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
  Serial.println("at the end of setup");
}

void loop()
{
//  int value = analogRead(addressPin);
//  if (value < 415 && value > 350)
//    Serial.println(value);
GameState = WAITING;
Serial.println(GameState);
  switch (GameState)
  {
    Serial.println("in switch case");
    case STARTING:
      if (SetupAddress)
      {
        Serial.println("setupaddress is true");
        GetAddress();
        Wire.begin(CurrentAddress);
        digitalWrite(interruptPin, HIGH);
        SetupAddress = false;
      }
      if (CheckMessage)
      {
        GameState = PLAYING;
      }
      break;
    case PLAYING:
      break;
    case WAITING:
      Serial.println("in waiting");
      CheckForAddressPin();
      break;
  }
  if (ButtonDebounce(digitalRead(buttonPin)))
  {
    digitalWrite(interruptPin, HIGH);
  }
}

void CheckForAddressPin()
{
  beginTime = millis();
  Serial.println(millis());
  if (beginTime - endTime > 1000)
  {
    Serial.println("checking for address");
    value = analogRead(addressPin);
    if (value > 415 || value < 350)
    {
      Serial.println("address is correct");
      GameState = STARTING;
      SetupAddress = true;
    }
    endTime = millis();
  }
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  int startTime = millis();
  if (startTime - lastEventTime >= 100)
  {
    Wire.write("ACK");
    digitalWrite(interruptPin, LOW);
    lastEventTime = millis();
  }
}

void GetAddress()
{
  int addressValue = analogRead(addressPin);
  if (addressValue >= 0 && addressValue < 300)
  {
    CurrentAddress = 1;
    digitalWrite(interruptPin, HIGH);
  }
  else if (addressValue > 300 && addressValue < 700)
  {
    CurrentAddress = 2;
    digitalWrite(interruptPin, HIGH);
  }
  else if (addressValue > 700 && addressValue < 1000)
  {
    CurrentAddress = 3;
    digitalWrite(interruptPin, HIGH);
  }
  else if (addressValue > 1000)
  {
    CurrentAddress = 4;
    digitalWrite(interruptPin, HIGH);
  }
}

int ButtonDebounce(int reading)
{
  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState)
  {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay)
  {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState)
    {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH)
      {
        lastButtonState = reading;
        return 1;
      }
    }
  }
  lastButtonState = reading;
  return 0;
}

the problem is in the loop where it does not go into the switch case. the state i put on purpose in the for loop but this still did not work. the only time it goes into the switch case is when i check for the value within getaddresspin function change the if statement to && instead of ||. does someone know what i could do so it will work while also using ||??

This statement will never execute. Your code will always enter the switch (GameState).

Are you sure this is correct? What board are you using?

int addressPin = 8;

a seeeduino XIAO

What value are you reading from pin 8?

it can be different values, because i have a resistor setup with multiple outputs so i can put a different wire in the pin to have a different outcome to work with. this is why i use the analogread with pin 8 aka the addresspin.

OK. Let me rephrase that then. Are you reading the values you expect from pin 8?

It's not clear to me that should work. I've never seen it used. The Serial.println is not associated with any case. Very unusual attempt.

Also, your switch() statement has no 'default' case. You should always have one.

I've never done that before either. I tried it in TinkerCad and it didn't print. I assume the compiler makes a jump table or some such thing and that line of code is ignored or optimized out out since it is unreachable.

yes i get the correct values that i want from the pin

i did not think correct when adding that line and will remove that after seeing that it has no sense of being there

  Serial.println("in switch case");
  switch (GameState)
  {

    case STARTING:

Seems like that's what you meant to happen.

a7

okay wow after removing it, it did work thanks for the help guys :sweat_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.