I'm trying to create a simple Game.
The idea is to count in millis
- how fast the player reacts(Releases a switch when a RedLight turns ON
2)how fast he can solve the puzzle (when puzzle solved he pussh a different Switch)
Following PaulS advice...i rebuilded the code made here Game Reaction tester and two capacitive sensors - Programming Questions - Arduino Forum to the following
#include <CapacitiveSensor.h>
const int RedLight = 6; // red stop LED
const int BlueLight = 10; // red stop LED
const int StartSwitch = 13; // reaction timer button
int StartState = 0; // variable for reading the pushbutton status
int BaseState = 0; //this variable controls if BaseSensor is ON=1 or OFF=0
CapacitiveSensor BaseSensor = CapacitiveSensor(7,8); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
int EndState = 0; //this variable controls if EndSensor is ON=1 or OFF=0
CapacitiveSensor EndSensor = CapacitiveSensor(7,9); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
long randomDelayTime;
long timerStartMillis; // the time when the timer started
long timerMidMillis; // the time when in Mid state
long timerEndMillis; // the time when the timer finished
long difference2;
long difference;
boolean PrepareState = true; // in introduction mode
void setup() {
pinMode(RedLight, OUTPUT); // red LED is an output
pinMode(BlueLight, OUTPUT); // Blue LED is an output
pinMode(StartSwitch, INPUT); // button is an input
BaseSensor.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
EndSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);
long start = millis();
long total1 = BaseSensor.capacitiveSensor(10);
long total2 = EndSensor.capacitiveSensor(10);
pinMode(StartSwitch, INPUT); // button is an input
randomSeed(analogRead(2)); // use unconnected pin to seed random sequence
Serial.begin(9600);
}
void loop()
{
StartState = digitalRead(StartSwitch);
long start = millis();
long total1 = BaseSensor.capacitiveSensor(10);
long total2 = EndSensor.capacitiveSensor(10);
if (total1 > 20){ BaseState =1; } else { BaseState =0; }
if (total2 > 20){ EndState =1; } else { EndState =0; }
if(PrepareState) // Prepare State
{
digitalWrite(BlueLight, HIGH); // PREPARE STATE = BLUE LIGHT ON
if(BaseState == 1) // ===================================================== TEST BASE LIGHT
{
Serial.println(" Prapare State and Base Pressed ");
digitalWrite(RedLight, HIGH);
}
else
{
Serial.println(" Prepare ");
digitalWrite(RedLight, LOW);
}
if(EndState == 1) // ======================================================= TEST END LIGHT
{
Serial.println(" Prapare State and END Pressed ");
digitalWrite(RedLight, HIGH);
}
else
{
Serial.println(" Prepare ");
digitalWrite(RedLight, LOW);
}
if(BaseState ==1 && StartState == 1)// StartState Pressed Prepare State OFF
{
Serial.println(" Prepare State and Start State Pressed ");
digitalWrite(BlueLight, LOW); // NO PREPARE STATE = BLUE LIGHT ON
digitalWrite(RedLight, LOW);
//randomDelayTime = random(10000); // this is the random amount to be used 0-10 seconds
//delay(randomDelayTime);
delay(2000);
timerStartMillis = millis(); // get the current time
PrepareState=false ;
}
}
else //No Prepare State
{
Serial.print(" TIMER START ");
Serial.println(timerStartMillis);
digitalWrite(RedLight,HIGH);
if (BaseState==0) // =============================================================== Base Released
{
timerMidMillis = millis(); // get the current time
difference = timerMidMillis - timerStartMillis; // time taken is difference between times
Serial.print(" TIMER MID ");
Serial.println(timerMidMillis);
while(EndState == 1) // ============================================= End Pressed Prepare State ON
{
timerEndMillis = millis(); // get the current time
difference2 = timerEndMillis - timerMidMillis; // time taken is difference between times
Serial.print(" TIMER END ");
Serial.println(timerEndMillis);
digitalWrite(RedLight,LOW);
}
// Serial.print(" difference ");
// Serial.print(difference);
// Serial.print(" difference2 ");
// Serial.println(difference2);
if (StartState==1)
{
PrepareState = true;
}
}
} // No Prepare State
}
The problem is at the last stages of the code..
Take a look at the values i'm taking in serial
TIMER START 4574 <<<<<---------------------timerStartMillis = millis();
TIMER START 4574
TIMER START 4574
TIMER START 4574
TIMER START 4574
TIMER START 4574
TIMER MID 4803
TIMER START 4574
TIMER MID 4841 <<---------------------When BaseState ==0 (Switch released)
TIMER START 4574
TIMER START 4574
TIMER MID 4897
TIMER START 4574
TIMER MID 4935
TIMER START 4574
TIMER MID 4973
TIMER START 4574
TIMER MID 5010
TIMER START 4574
TIMER MID 5048
TIMER START 4574
TIMER MID 5085
TIMER END 5103 << -----------------------------When EndState ==1 (End switch pressed)
TIMER END 5121
TIMER END 5138
TIMER END 5155
TIMER END 5174
TIMER END 5191
TIMER END 5209
TIMER END 5226
TIMER END 5244
TIMER END 5262
TIMER END 5279
TIMER END 5297
TIMER END 5315
TIMER END 5332
TIMER END 5350
TIMER END 5368
TIMER END 5386
TIMER END 5403
TIMER END 5421
although i'm taking the desired values the loop continues for ever so i cant calculate the differences i want to count
Serial.print(" difference ");
Serial.print(difference);
Serial.print(" difference2 ");
Serial.println(difference2);
So my questions are :
- What did i have to do in order to display the two values difference and difference2 stable?
- Is it possible when all values displayed correctly when i StartState==1 to start over the Game ?
PS1. Later , I'm planning to use an LCD screen in order to display results
PS2. I'm struggling several days on this...with no luck ..
Thank you in advance.