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:
#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 ![]()
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