I've been using Arduino ever since I built my first 3D printer about 4 years ago. Since I was already familiar with the technology, that's what I decided to use for my current slot car project. My current setup is a section of track with two photo sensors for each lane.
As I started building the circuit, weird goofy electrical interference things began to happen.. Both sensors are hooked up to the +5v, lightPin at A0, and triggerPin at A4. This simple sketch was a just to get a feel to see if I could get something working. The strange part is when I hold the A4 wire in my hand, the sensor works. When I remove my hand it stops. The sensor at A0 works the same regardless. I figured a simple sketch like this would help me get up and running. Also, I'm strongly considering using a hall sensor to detect the cars as that would help eliminate false readings and detect only the cars.
Any tips or suggestions would be greatly appreciated!
const byte lightPin = 0;
const byte triggerPin = 4;
int lightSensor = 0;
int triggerSensor = 0;
void setup(){
Serial.begin(9600);
pinMode(triggerPin, INPUT);
pinMode(lightPin, INPUT);
}
void loop(){
lightSensor = analogRead(lightPin);
triggerSensor = analogRead(triggerPin);
if(lightSensor < analogRead(1)){
Serial.println("Sensor 2 triggered");
}
if(triggerSensor < analogRead(1)){
Serial.println("Sensor 1 triggered");
}
}