Hi.
Based on this .. Arduino Reaction Timer with LCD Display | derekmolloy.ie
I'm trying to count two different times based on two capacitive sensors.
As you can see in my test code
- When StartSwitch pressed and BaseSensor is 1 the code waiting for a random amount of time and then RedLight goes ON
- Then i'm counting the time in millis between LED ON and Release BaseState and later displaying it in my LCD.
What i also want to count ...unsuccesfully so far .. is the time between the BaseState released and the EndState=1 and then display it to my LCD.
Can please someone help me ?
Thank you in advance
/*
Arduino Reaction Timer using an RGB LCD 16x2 Character Display
Derek Molloy, DCU, Ireland.
Using LCD library from:
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
#include <CapacitiveSensor.h>
#include <LiquidCrystal.h> // include LCD library
// Set up the display with 4 bits - R/W tied to GND
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // RS, E, D4-D7
int RedLight = 6; // red stop LED
int BlueLight = 10; // red stop LED
int StartSwitch = 1; // reaction timer button
//int BaseSwitch = 9; // reaction timer button
//int EndSwitch = 7; // reaction timer button
const int PotBase = A0; // analog IN Sensitinity Base
int PotBaseVal = 0; // variable to store the value read from PotBase A3
int BaseSense = 0; // value to adjust Base sensitivity
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
const int PotEnd = A1; // analog IN Sensitinity End
int PotEndVal = 0; // variable to store the value read from PotEnd A1
int EndSense = 0; // value to adjust End sensitivity
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
// States of execution
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
//int buttonState1 = 0; // current state of the button
//int lastButtonState1 = 0; // previous state of the button
long randomDelayTime; // holds the random time amount
boolean prepareState = true; // in introduction mode
boolean isTiming = false; // timing the press state
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;
// Setup function - called only once
void setup() {
pinMode(RedLight, OUTPUT); // red LED is an output
pinMode(BlueLight, OUTPUT); // Blue LED is an output
BaseSensor.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
EndSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);
// pinMode(StartSwitch, INPUT); // button is an input
// pinMode(BaseSwitch, INPUT); // button is an input
lcd.begin(16, 2); // 16 Columns by 2 Rows
randomSeed(analogRead(2)); // use unconnected pin to seed random sequence
}
void loop() {
PotBaseVal = analogRead(PotBase); // read the input pin
BaseSense = map(PotBaseVal, 0, 1023, 0, 62);
long start = millis();
long total1 = BaseSensor.capacitiveSensor(10);
long total2 = EndSensor.capacitiveSensor(10);
//long total3 = cs_4_8.capacitiveSensor(30);
if (total1 > 20){ BaseState =1; } else { BaseState =0; }
if (total2 > 20){ EndState =1; } else { EndState =0; }
buttonState = digitalRead(StartSwitch);
// buttonState1 = digitalRead(EndSwitch);
if(prepareState)
{ // prepare state - give out the instruction to press button
lcd.setCursor(0,0); // set cursor to 0,0 (top left)
lcd.print("Reaction Tester:"); // string on the top row
lcd.setCursor(0,1); // next row
lcd.print(" [Press Button] "); // string on the next row
//============================================================= TEST BASE & End =====================================================
if(BaseState == 1) {digitalWrite(RedLight, HIGH);} else { digitalWrite(RedLight, LOW); }
if(EndState == 1) {digitalWrite(BlueLight, HIGH);} else { digitalWrite(BlueLight, LOW); }
//============================================================= TEST BASE & End =====================================================
if (BaseState ==1 && buttonState != lastButtonState) // if the button is pressed
{
digitalWrite(RedLight, LOW);
lcd.clear(); // clear the display
lcd.setCursor(0,0);
lcd.print("---- Ready -----");
randomDelayTime = random(10000); // this is the random amount to be used 0-10 seconds
while (BaseState !=1 ) {} // wait until the button is released
prepareState = false; // finished prepare state - lets move on
}
}
else // not in prepare state
{
if (!isTiming) // the timer isn't running, so we are pausing for random amount
{
delay(randomDelayTime); // delay for the random amount
digitalWrite(RedLight, HIGH); // when finished - set red LED high
lcd.setCursor(0,0); // press now message
lcd.print("------ GO ------");
isTiming = true; // now we are ready to start timing reactions
timerStartMillis = millis(); // get the current time
}
else // now we are timing person's reaction
{
if (BaseState == 0) // when they release the button
{
timerMidMillis = millis(); // get the current time
digitalWrite(RedLight, LOW); // turn off the red led
long difference = timerMidMillis - timerStartMillis; // time taken is difference between times
lcd.clear(); // clear the LCD
lcd.setCursor(0,0);
if (difference < 10 ) // If the difference is 0 they held the button - or are supermen!
{
lcd.print("--- Too Soon ---");
digitalWrite(RedLight, HIGH); // turn off the red led
delay(200);
digitalWrite(RedLight, LOW); // turn off the red led
delay(200);
digitalWrite(RedLight, HIGH); // turn off the red led
delay(200);
digitalWrite(RedLight, LOW); // turn off the red led
delay(200);
digitalWrite(RedLight, HIGH); // turn off the red led
delay(200);
digitalWrite(RedLight, LOW); // turn off the red led
}
else // valid time
{
lcd.setCursor(0,0);
lcd.print("ReacTime : ");
lcd.print(difference);
lcd.setCursor(0,1);
lcd.print("GameTime : ");
lcd.print(difference2); // milliseconds
while (EndState ==1) // when they hit end button
{
timerEndMillis = millis(); // get the current time
difference2 = timerEndMillis - timerMidMillis; // time taken is difference between times
//lcd.clear();
//lcd.setCursor(0,0);
//lcd.print("ReacTime : ");
//lcd.print(difference);
lcd.setCursor(0,1);
lcd.print("GameTime : ");
lcd.print(difference2);
}
}
delay(5000); // leave the message on the screen for 5 seconds
isTiming = false; // ready to start timing again
prepareState = true; // ready to start all over
//setDisplayRGB(255,255,255); // set the LCD backlight to white
}
}
}
}