The Response Timer (Beta)
Saturday, February 14th, 2009It Works!
It took way too long for such a simple thing. But, it works. I have, um, semi-successfully created a device that measures the precise time in milliseconds between an LED lighting and a user hitting the button. The button is, of course, made of an old floppy ripped to shreds and taped onto a micro-switch from some old appliance. The wiring is too simple, and would work with the configuration described under “Button” at arduino.cc. It spits numbers out to serial which can be viewed with HyperTerminal, and, in the future, Processing. At this moment, I keep pressing the button with my wrist as it’s so close to my keyboard.
I Can Has Code??
You certainly can has code. It’s based off of what I wrote about earlier, and has one minor major bug. Here goes:
int lightPin = 13;
int buttonPin = 2;
int resetPin = 4;
static int cyclesGlobal = 0;
boolean isTesting = false;
boolean killLoop = false;
void setup()
{
pinMode(lightPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, HIGH);
Serial.begin(9600);
Serial.println(124, DEC);
}
void reset()
{
digitalWrite(resetPin, LOW);
digitalWrite(resetPin, HIGH);
}
void erraticSignal()
{
digitalWrite(lightPin, HIGH);
delay(200);
digitalWrite(lightPin, LOW);
delay(100);
digitalWrite(lightPin, HIGH);
delay(200);
digitalWrite(lightPin, LOW);
delay(100);
digitalWrite(lightPin, HIGH);
delay(200);
digitalWrite(lightPin, LOW);
delay(1000);
}
void alertStart()
{
Serial.println(246, DEC);
}
void lightOn()
{
digitalWrite(lightPin, HIGH);
}
void lightOff()
{
digitalWrite(lightPin, LOW);
}
void startTiming()
{
delay(200);
while(cyclesGlobal < 10000 && killLoop != true)
{
delay(1);
cyclesGlobal++;
}
phoneHome();
lightOff();
cleanup();
}
void phoneHome()
{
Serial.print(135, DEC);
Serial.println();
delay(100);
Serial.println(cyclesGlobal);
delay(1000);
}
void stopTimer()
{
killLoop = true;
}
void cleanup()
{
lightOff();
detachInterrupt(0);
cyclesGlobal = 0;
isTesting = false;
killLoop = false;
reset();
}
void spill()
{
//debug
/* Serial.println("Now spilling the beans...");
delay(200);
Serial.print("lightPin: ");
delay(200);
Serial.println(lightPin);
delay(200);
Serial.print("buttonPin: ");
delay(200);
Serial.println(buttonPin);
delay(200);
Serial.print("cyclesGlobal: ");
delay(200);
Serial.println(cyclesGlobal);
delay(200);
Serial.print("isTesting: ");
delay(200);
Serial.println(int(isTesting));
delay(200); */
Serial.print("killLoop: ");
delay(200);
Serial.println(long(killLoop));
delay(200);
}
void startTesting()
{
isTesting = true;
startWaiting();
attachInterrupt(0, stopTimer, RISING);
lightOn();
startTiming();
}
void startWaiting()
{
delay(random(2000, 10000));
}
void loop()
{
if (digitalRead(buttonPin) == HIGH)
{
if (isTesting != true)
{
erraticSignal();
alertStart();
startTesting();
}
}
}
Insecticide Please?
I swear, I worked many hours pinpointing the bug. I haven’t found a solution for it. I was able to theorize and implement a solution, but did it work? No. Anyways, after the first test, it starts freaking out and will only say your reaction time is 0. It ends up completely skipping the timing loop, and it even thinks that “0 < 10000″ is false. Right. My solution was to send a pulse to pin 4, which would have a jumper to RESET, in hopes of a self-reset within the program. No avail, but it’s still in the code in case it works for anyone else or something.


