Button Sequence Comparison

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!

I am not clear on how the player will know which order the buttons should be pressed in

You could store the required button sequence in an array which would make it easy to determine whether the button pressed was the correct one in the sequence.

I reckon you need to figure out a logical system to deal with your problem before you start writing code. Put away the Arduino for a little while and just write down a clear, precise, step by step description of what needs to happen using plain language, not computer code.

From a quick glance at your code you are heading straight for a bowl of spaghetti or a bird's nest that will be completely unmanageable.

You need to use arrays to manage the buttons and the sequences so that the same piece of code can deal with all the buttons. Short code has less room for typos.

Don't use the delay() function anywhere.

You may get some ideas in Planning and Implementing a Program

...R

for our senior design engineering class.

High school or college?

yet we have very little programming experience in general.

Who's fault is that? Who is going to hire an engineer that can't program?

PaulS:
Who's fault is that? Who is going to hire an engineer that can't program?

The same people that hire engineers that can't engineer.

@aarg:

ROFL

Engineer: "Were you able to fix the vacuum pump"?
Me: "Yep, got the tank pulled down to 25 inches".
Engineer: "Great, pull it on down to 35".
Me: "Will do".

aarg:
The same people that hire engineers that can't engineer.

I hope that that would only be people that see someone with an engineering degree and no experience, and think that the person is educate-able. OP seems rather proud of his/her lack of skills.

PaulS:
I hope that that would only be people that see someone with an engineering degree and no experience, and think that the person is educate-able. OP seems rather proud of his/her lack of skills.

Good point. I put a lot of stock in learning on the job. Unfortunately, we are also seeing the decline of proper apprenticeship or guidance from experienced people for new hires in a lot of places. A lot of companies see engineers as commodities that they can buy and trash for new ones when they need a new skill. So they don't see much value in long term investment in the employee's skill set. In spite of fancy words to the contrary. Probably, they think they will lay off or fire the employee before they get any return from having an experienced person spend time educating them.

Which is sad, because the school can only really prepare them for the job. It can't give them the experience.