#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 ||??