Hello,
We are developing a game for our senior design engineering class. We were given a project that will rely heavily on Arduino yet we have very little programming experience in general.
The Arduino will be hooked up with 5 individual buttons that is to be stepped on by the player. When the program begins, a timer will start and the player will be tasked with pressing the appropriate buttons in the (same) correct order. If the player messes up on the order, the program will add a second to their overall time.
The part we are stuck on is pressing the buttons in the correct order. At first we thought it would be as simple as throwing a bunch of if/elseif statements into the void loop() function in the order that the steps need to go in and throw a flag for when a bad step is made. This is to be done for a total of around 200 steps.
Below is part of the code, there is currently only two buttons (labeled sensors) as we are still trying to get a hang of it:
const int sensorA = 2;
const int sensorB = 3;
const int led = 13; //For testing purposes
int steps; //How many steps in total
int badSteps; //How many bad steps in total (out of order)
int value = LOW; //LED testing
int blinking; //LED testing
long previousMillis = 0;
int sensorAState;
int sensorBState;
int lastButtonState = HIGH;
long interval = 100;
long fractional;
long startTime;
long elapsedTime;
void setup() {
Serial.begin(9600);
pinMode(sensorA, INPUT);
pinMode(sensorB, INPUT);
pinMode(led, OUTPUT);
digitalWrite(sensorA, HIGH);
digitalWrite(sensorB, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
sensorAState = digitalRead(sensorA);
sensorBState = digitalRead(sensorB);
if (sensorAState == LOW && lastButtonState == HIGH && blinking == false && sensorBState == HIGH)
{
//Initialize program
Serial.print("Begin ");
startTime = millis();
blinking = true;
steps++;
delay(50);
lastButtonState = LOW;
}
else if (sensorBState == LOW && lastButtonState == LOW && blinking == true && sensorAState == HIGH)
{
//Next button press is supposed to be this button if it is no badstep++
steps++;
delay(50);
if (sensorAState == LOW)
{
//But if it is badstep++
delay(50);
badSteps++;
}
}
else if (sensorAState == LOW && lastButtonState == LOW && blinking == true)
{
//When the last step is made, the program ends, blinking of led stops, and it assesses the overall time
elapsedTime = millis() - startTime;
blinking = false;
lastButtonState = sensorAState;
long convertedTime = elapsedTime/1000 + badSteps;
Serial.print("Amount of steps: ");
Serial.print(steps);
Serial.print(" ");
Serial.print("Bad steps taken: ");
Serial.print(badSteps);
Serial.print(".");
Serial.println("Final: ");
Serial.print(convertedTime);
Serial.print(" ");
//Serial.println(" Final: ");
// Serial.print(" ");
// Serial.print(convertedTime);
fractional = (long)(elapsedTime % 1000);
if (fractional == 0)
{
Serial.print("000");
}
else if (fractional < 10)
{
Serial.print("00");
}
else if (fractional < 100)
{
Serial.print("0");
}
else if (fractional < 1000)
{
Serial.print("");
}
//Serial.print("Last time: ");
// Serial.print(elapsedTime);
Serial.print(fractional);
}
else
{
lastButtonState = sensorAState;
}
if ( (millis() - previousMillis > interval) ) {
if (blinking == true){
previousMillis = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (value == LOW)
value = HIGH;
else
value = LOW;
digitalWrite(led, value);
}
else{
digitalWrite(led, LOW); // turn off LED when not blinking
}
}
sensorAState = digitalRead(sensorA);
}
This is our attempt at something that is not our forte so any help at all would be greatly appreciated!
NOTE: Some parts of the code such as creating the decimal points for the time and utilizing the LED to keep track of the timer is not our own!