I'm trying to build a reaction timer. It consists of an arduino decimillinove, a AC relay switched on 5v and a flex piezo sensor.
I'm using a relay board from SparkFun to switch the AC current (the same one in the Controllable Power Outlet tutorial on their site) and a piezo flex/vibration sensor (SparkFun part SEN-09199). I'm also using a standard HD44780 two line LCD for output.
Aside from the LCD connections, the flex sensor is connected to analog pin 5 in parallel with a 1M ohm resistor and the relay is switched on digital pin 9. The flex sensor is soldered into a modular phone jack housing and a 50 foot phone cable extends to another modular jack where the two pins are connected to the arduino.
The idea is this: the the LCD displays "Ready" "Set" and then after a random 2-5 second delay, the AC relay switches power to an electrical box with an outlet. This switches on a 110v light bulb in a housing behind a metal target. When the light goes on the software begins monitoring the vibration sensor. When the sensor exceeds the threshold (in practice because it got pinged by a wax bullet) or 10 seconds elapse, which ever comes first, the light goes off and the reaction time is displayed. It then goes into a 10 second delay and the whole thing starts over.
I've found that for the most part the system works while connected to USB. When I power the arduino off a 9v wall wart, the problem is the relay immediately clicks over. For some reason, the millisecond it monitors the sensor, it immediately reads threshhold value and clicks on then off. It happens so fast that the click of the relay is barely audible. Occasionally, the relay does click on and then off as its supposed to when the sensor is tapped. Then the very next cycle it fails prematurely. I get the same failure occasionally off the USB as well. The connections and code itself are pretty simple.
Even if the outlet is removed from the 110v or the whole relay is replace with just a simple LED same problem so I think its in the sensor or the code.
I think there may be noise or something in the system which is setting off false readings but I'm not sure. Or maybe I'm overloading the analog pin or something but I tried putting a delay between reads of the pin and that didn't help either.
I'm wondering if I'm missing something? This set up obviously needs to be reliable and as accurate as possible. Any help would be greatly appreciated.
#include <LiquidCrystal.h>
int lightPin = 9;
int hitPickup = 5;
int THRESHOLD = 60;
LiquidCrystal lcd(10, 11, 12, 13, 14, 15, 16);
unsigned int time;
int i = 0;
void setup() {
lcd.clear();
lcd.print("Starting Up...");
pinMode(lightPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port
digitalWrite(lightPin,LOW);
randomSeed(analogRead(6));
}
void loop() {
digitalWrite(lightPin, LOW);
lcd.clear();
lcd.print("SHOOTER READY...");
delay(2000);
lcd.setCursor(0,1);
lcd.print("SHOOTER SET....");
delay(500);
time = runTimer();
lcd.clear();
lcd.print("TIME: ");
lcd.print(time);
delay(3000);
lcd.setCursor(0,1);
lcd.print("WAITING...");
delay(5000);
}
unsigned int runTimer() {
delay(random(2000,5000));
unsigned long startTime = millis();
digitalWrite(lightPin, HIGH);
while ( (millis()-startTime) < 9999 ) {
if ( analogRead(hitPickup) >= THRESHOLD) {
digitalWrite(lightPin, LOW);
return (millis()-startTime);
}
// delay(2);
}
digitalWrite(lightPin, LOW);
return 9999;
}