Pages: [1]   Go Down
Author Topic: Help please - delux reaction timer  (Read 743 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys

I'm having some trouble with a question i have to solve, heres the question:

Write a program which waits for a random period of time (between 3 & 7 seconds) and when this time has elapsed lights one of the LEDs on the I/O board.

In response you must press one of the buttons on the I/O board as quickly as possible. Your program should measure your reaction time and print this to the serial monitor in milliseconds.

In this version of the program you are allowed several attempts and an average reaction time is calculated. Your program should have the following features:

·         The number of attempts (which must be between 3 and 9) is selected using one of the pots and displayed on the 7 segment display.

·         When the display is showing the number you want, pressing a button confirms your selection.

·         The I/O shield acknowledges this with a short 'beep' to indicate that it has started.

·         The program performs the chosen number of tests, when complete this is indicated by three short beeps ascending in tone.

The following is printed to the serial monitor:

·         The times for each attempt

·         The total time taken
·         The average time taken
·         The best (average) time for the current session

·         The best (average) time ever

You must write/use a function to display digits on the 7 segment display. You should also make use of functions, where appropriate, to enhance your program.


Heres what i got so far:
Code:
#define POT1  0
#define POT2  1
#define POT3  2

#define KNOCK    5

#define BUTTON1  10
#define BUTTON2  11
#define BUTTON3  12

#define LED1  5
#define LED2  6

#define BUZZER   3

#define TEMP  4

#define LIGHT  3

#define LATCH 7
#define CLOCK 8
#define DATA 4



const byte ledCharSet[10] = {
  B10111101, B10101101, B10000101, B10110000, B10011001, B10010010, B10000010, B11111000, B10000000, B10010000
};

int i;
int DispNum;

void setup()
{
  Serial.begin(9600);
 
  // Setup the pins to be used in the correct manner
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA,OUTPUT);
 
 
 
  Serial.println("To begin press select the number of try's using Pot1");
}

#define WAITING_STATE 0 // waiting for user to begin
#define PLAYING_STATE 1 // waiting to light up LED
#define REACTION_STATE 3 // waiting for user to respond

byte state = 0;
byte time_expired = 0; // time passed before pressing go
int time = 0; // time it took them to press it in 100ths of a second
int count;


int numberSeg()
{
i = analogRead(POT1)/102;
if (i >= 3 && i <= 9)
{
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, ledCharSet[i]);
digitalWrite(LATCH, HIGH); 
}
confirm();
}

// Confirm Selection Code

int confirm()
{
if (!digitalRead(BUTTON1))
{
DispNum = i; // Number of tries
}

delay(400);

}

void loop() {
//------ FUNCTIONS CALLED BELOW -------- 
numberSeg();
for (count = 0; count = DispNum; ++count){
           
    if(state == WAITING_STATE) {
        if(!digitalRead(BUTTON1)) {
          while(!digitalRead(BUTTON1)); // wait for them to release it
          state = PLAYING_STATE;
          time_expired = 0;
          time = 0;
          Serial.println("Test Begun! Hit Button 2 on seeing the LEDs!");
        }
      }
      else if(state == PLAYING_STATE){
        if(time_expired >= 3 && ( time_expired >= 7 || random(7) <= time_expired )) {
          state = REACTION_STATE;
        }
        else {
          time_expired ++;
          delay(1000);
        }
      }
      else if(state == REACTION_STATE) {
          // turn on the LEDs
          if(!digitalRead(BUTTON2)) {
            // they hit the button
            noTone(BUZZER);
            while(!digitalRead(BUTTON2)); // wait for them to release it

            Serial.print("Time taken: ");
            float Realtime = (float) time / 100;
            Serial.print(Realtime);
            Serial.println(" seconds.");
            Serial.println("To try again press button 1");
            digitalWrite(LED1,LOW);
            digitalWrite(LED2,LOW);
            state = WAITING_STATE;
          }
          else {
            digitalWrite(LED1,HIGH);
            digitalWrite(LED2,HIGH);
            tone(BUZZER, 10);
            delay(10);
            noTone(BUZZER);
            time ++;
          }
      }
}

it runs the test how ever many times you would like it to by keep pressing button 1 but i only want it to loop how ever many times the user selects on the 7seg; as you can see ive tried a for loop but no success smiley-sad

Code:
for (count = 0; count = DispNum; ++count)

any help will be appreciated, and also if you think you could help with any of the other functions it has to produce it would be great
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When is our assignment due to be handed in?
Code:
count = DispNum;
Oldest one in the book.
Code:
Serial.println("To begin press select the number of try's using Pot1");
sp. "tries"
« Last Edit: February 24, 2011, 08:05:58 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks very much for your fast reply, it's in for tomorrow i have been working on it all week :/

im not sure i understand what you mean though?
thanks
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
im not sure i understand what you mean though?
You misspelled (and mis-punctuated - it isn't a possessive or an elision) "tries".
« Last Edit: February 24, 2011, 08:18:28 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i meant with the code part count = DispNum

would you be able to help me with looping the function please?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A "for" statement consists of three parts
1) a one-off initialisation
2) a per-loop termination condition
3) an end of loop action.

You're missing (well, it's there, but incorrect) 2)

Along with putting semicolons on preprocessor statements, it's probably the commonest noob mistake.
http://arduino.cc/en/Reference/For
« Last Edit: February 24, 2011, 08:45:13 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So do i put the whole of the function for the reaction timer in this loop?
Logged

Pages: [1]   Go Up
Jump to: