Help with Flex Sensor and AC Relay

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;
}

I don't have any theories about the USB vs battery thing but I imagine you'd get a lot of noise on a 50 ft phone cable with a 1M resistance at the end. You could try a short lead as an experiment to see if that's it.

could your long lead be picking up something from the wallwart?

When I was prototyping it I was using about a 10 inch lead to the flex sensor and I still had the problem. I thought it was maybe bad connections which is why I went forward with actually soldering the jacks and making permanent connections.

How could I possibly reduce electrical noise on the sensor or the leads? Is the 1M resistor too munch or too little? Seemed to be right based on tutorials and trial and error?

I try to output the readings off the sensor through the serial interface and print them on the command line but it seems too slow. It can't print the output fast enough to accurately model the values its receiving in real time from what I can tell.

Could you post the circuit and pint to the example you were working from?

Here are the links to the components I'm using:

Flex Sensor:

5v AC Relay (includes schematic):

Tutorial for AC Relay Circuit:
http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=119

I don't have a drawing of the circuit right now but its pretty simple.

The HD44780 LCD is connected directly to the Arduino pins 10, 11, 12, 13, 14, 15, 16.

The positive side of the flex sensor is connected to analog pin 5 (signal) and the negative to the arduino ground. A 1M resistor is in parallel.

Digital pin 9 goes to the relay circuit board which is connected to the 5v and ground on the arduino. I've constructed the PCB relay and electrical box almost exactly as in the tutorial. Its getting 110v from the wall and the relay is cutting cutting the hot side of the plug.

Here is a video on YouTube of the project working properly off USB:

In this video you can see all the parts and it working properly off the USB.

Here is a video of it flaking out on the 9v wall transformer:

Note that it does the exact same behavior off the USB as well. Its just the USB works 9 out of 10 times and the wart DOESNT work 9 out of 10 times.

You can see in the first video that power comes into the electrical box and to the switch which acts to power the entire system. Power is tapped off the 110v to the brown plug where the 9v transformer is plugged in and to the electrical socke. The hot side of the electrical socket is wired into the pcb relay exactly as in the tutorial. The plug controls the light bulb socket mounted in the steel target.

The LCD is running off the Electronic Brick shield for simplicity. I have an LCD I've hand wired DSub connectors to for the final product which is mounted in the lid of the project box (not shown), the brick system just makes everything easier. You can see the flex sensor soldered with the 1M resistor into the modular phone jack housing. It runs off a standard phone line to the other modular jack in the box. That jack goes to the analog pin 5 and ground.

I don't think any of the AC electrical connections are causing the problem nor is it a bad connection in the sensor (I think). Originally, the light was a simple LED and the sensor leads were bread boarded with 10 inches directly to the arduino and I had the same problem.

That's all there is to it. Any insight would be awesome. Thanks in advance!!!

Yes but you still haven't posted the circuit of what you have wired up. It is a bit confusing looking at the video.
Almost certainly you need some power supply decoupling:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html
I would also put a 0.1uF capacitor across the analogue input you have connected the flex sensor to and also try reducing the 1M resistor. That is a high value and is very prone to stray pickup from the mains switching which almost certainly your problem.

I added the capacitor and now it doesn't trigger immediately but it also doesn't trigger at all. I'm working on learning Eagle and making a proper schematic. I'll have it posted soon.