I have a dragster track made out of PVC for an upcoming LEGO EV3 Mindstorms summer robotics program. I have everything working with one photoresistor at the end of the finish line to display time.
that works great.
The issue is that the finish line is 3 feet wide and expecting kids to perfect drive over one photoresistor is not going to happen. In my prototype I envision about 5 photoresistors spaced across the finish line so no matter which one they trigger it will signal the finish and display time.
I don't know how to either code or wire this properly?
I attempted a second photoresistor plugged into the breadboard along same lines as the original. However, the timer did not stop until both photoresistors were triggered. I want it so that only one(does not matter which) will trigger the end of the run.
Here is the code. Currently, there is not code for a 2nd, 3rd, 4th, or 5th photoresistor because I don’t know how to incorporate them into the code.
Thanks for the help.
/********************************************************************************
Arduino Inventor's Guide - Project 9 (Listing 9-2)
Drag Race Timer - Single Car
Uses a button to trigger moving a servo arm up (starting gate) to start the
race. Timer runs until the light sensor is crossed at the end.
Elapsed raceTime is shown on the LCD.
Written by: Derek Runberg & Brian Huang, March 2017
SparkFun Electronics & No Starch Press
Remixed by Aaron Maurer(a non expert so careful using this code for your own! Use the orinal code by the creators
*******************************************************************************/
#include<LiquidCrystal.h>
#include<Servo.h>
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
Servo startingGate;
const byte buttonPin = 5;
const byte finishSensor1Pin = A0;
const int darkThreshold = 850;
int finishSensor1;
boolean finishFlag = false;
long startTime;
long stopTime;
float raceTime;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.clear();
lcd.print("Drag Race Timer");
lcd.setCursor(0, 1);
lcd.print("Push to start!");
while (digitalRead(buttonPin) == HIGH)
{
}
lcd.clear();
lcd.print("Go!");
startTime = millis();
}
void loop()
{
finishSensor1 = analogRead(finishSensor1Pin);
Serial.println(finishSensor1);
if ((finishFlag == false) && (finishSensor1 < darkThreshold))
{
finishFlag = true;
stopTime = millis();
raceTime = stopTime - startTime;
lcd.clear();
lcd.print("Finish Time:");
lcd.setCursor(0, 1);
lcd.print(raceTime / 1000, 3);
}
}
coffeechug:
I attempted a second photoresistor plugged into the breadboard along same lines as the original. However, the timer did not stop until both photoresistors were triggered. I want it so that only one(does not matter which) will trigger the end of the run.
So is this not true? You said you attempted it. If you want people to write code for you, hire them. If you want people to help you for free, then make an effort.
Could be a start.
I would use arrays and for loops, but this might be easier to understand for a beginner.
You need ofcourse to declare the extra pins at the beginning of the sketch.
Leo…
Just so others don’t think I am not trying. I finally was able to get two of them to work with this code. As I add more photoresistors to the finish line, my question is how do I add code where I press a second button to reset the code back to the top?
/********************************************************************************
Arduino Inventor's Guide - Project 9 (Listing 9-2)
Drag Race Timer - Single Car
Uses a button to trigger moving a servo arm up (starting gate) to start the
race. Timer runs until the light sensor is crossed at the end.
Elapsed raceTime is shown on the LCD.
Written by: Derek Runberg & Brian Huang, March 2017
SparkFun Electronics & No Starch Press
Remixed by Aaron Maurer(a non expert so careful using this code for your own! Use the orinal code by the creators
*******************************************************************************/
#include<LiquidCrystal.h>
#include<Servo.h>
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
Servo startingGate;
const byte buttonPin = 5;
const byte finishSensor1Pin = A0;
const byte finishSensor2Pin = A1;
const int darkThreshold = 850;
int finishSensor1;
int finishSensor2;
boolean finishFlag = false;
long startTime;
long stopTime;
float raceTime;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.clear();
lcd.print("Drag Race Timer");
lcd.setCursor(0, 1);
lcd.print("Push to start!");
while (digitalRead(buttonPin) == HIGH)
{
}
lcd.clear();
lcd.print("Go!");
startTime = millis();
}
void loop()
{
finishSensor1 = analogRead(finishSensor1Pin);
finishSensor2 = analogRead(finishSensor2Pin);
Serial.println(finishSensor1);
Serial.println(finishSensor2);
if ((finishFlag == false) && (finishSensor1 < darkThreshold))
{
finishFlag = true;
stopTime = millis();
raceTime = stopTime - startTime;
lcd.clear();
lcd.print("Finish Time:");
lcd.setCursor(0, 1);
lcd.print(raceTime / 1000, 3);
}
if ((finishFlag == false) && (finishSensor2 < darkThreshold))
{
finishFlag = true;
stopTime = millis();
raceTime = stopTime - startTime;
lcd.clear();
lcd.print("Finish Time:");
lcd.setCursor(0, 1);
lcd.print(raceTime / 1000, 3);
}
}
coffeechug:
my question is how do I add code where I press a second button to reset the code back to the top?
Add another, separate, if() statement which checks for finish flag AND reset switch pressed. Inside the if clause you can reset/initialize all your values to some start state.
However, as it stands you’ll have to move the start code down into loop() which will involve some modifications - setup() only executes once, right? Uber simple would be to just press the reset switch on the Arduino board.