im writing code for making a drag tree using two arduinos, I was helped by other people on this forum, when i run the code nothing happens any input would be great what im trying to do is when a car goes over the first laser the pre stage light comes on, then it moves forward to the second laser the stage light comes on and then the arduino waits for the signal from the other arduino saying both cars are staged, the yellow lights start if someone leaves the laser before it gets to green it turns red, when it turns green it starts the first timer (reaction time) that stops when the cars leave the second laser, and start the second timer that stops when the cars cross the finish line my code is this
// Pin values for Lights
const byte LEDWhite1 = 2;
const byte LEDWhite2 = 4;
const byte ARDUINOoutput = 3;
const byte ARDUINOInput = 5;
const byte LEDYellow1 = 6;
const byte LEDYellow2 = 8;
const byte LEDYellow3 = 10;
const byte LEDGreen = 11;
const byte LEDRed = 12;
// Pin values for Lasers
const byte StagingLinePin = A0;
const byte StartingLinePin = A1;
const byte FinishLinePin = A2;
// Delay between the countdoown lights
const unsigned InitialDelay = 7000; // between Stage and first Yellow
const unsigned YellowDelay = 500; // between each Yellow and Green
const int LaserInterruptSensitivity = 80; // sensitivity value to determine if sensor is blocked
// State Flags
bool RaceHasStarted = false; // Set 'true' when Green light is turned on
bool RaceHasFinished = false; // Set 'true' when Finish Line is crossed
// Timers for Race
unsigned long RaceStartTime = 0; // Set to micros() when Green light is turned on
//unsigned long ReactionTime = 0; // Set to micros() when car finishes going past start line
unsigned long RaceFinishTime = 0; // Set to micros() when Finish Line is crossed
// Timer for Light Countdown
// Note: Doubles as a state flag: if != 0, countdown is in progress
unsigned long CountdownStart = 0; // Set to millis() when Start Line is crossed
// Checks if sensor on analog input 'pin' is blocked
bool isSensorBlocked(byte pin)
{
return analogRead(pin) > LaserInterruptSensitivity;
}
// Turns on Light on pin 'lightPin'
void turnOnLight(byte lightPin)
{
digitalWrite (lightPin, HIGH);
}
// Turns off Light on pin 'lightPin'
void turnOffLight(byte lightPin)
{
digitalWrite (lightPin, LOW);
}
// turn on Red LED, turn off all others
void falseStart()
{
// turn on Red LED
turnOnLight(LEDRed);
// turn off all other LEDs
turnOffLight(LEDWhite1);
turnOffLight(LEDWhite2);
turnOffLight(LEDYellow1);
turnOffLight(LEDYellow2);
turnOffLight(LEDYellow3);
turnOffLight(LEDGreen);
}
void setup()
{
Serial.begin(9600);
pinMode(LEDWhite1, OUTPUT); // Pre-stage: Car is on Staging Line
pinMode(LEDWhite2, OUTPUT); // Staged: Car has reached Start Line
pinMode(ARDUINOoutput, OUTPUT); // Arduino sends out signal to other arduino saying car is staged
pinMode(ARDUINOInput, INPUT); // Arduino recieves signal from other arduino saying car is staged
pinMode(LEDYellow1, OUTPUT); // Turns on 'InitialDelay' milliseconds after White2
pinMode(LEDYellow2, OUTPUT); // Turns on 'YellowDelay' milliseconds after Yellow1
pinMode(LEDYellow3, OUTPUT); // Turns on 'YellowDelay' milliseconds after Yellow2
pinMode(LEDGreen, OUTPUT); // Start: Turns on 'YellowDelay' milliseconds after Yellow3
pinMode(LEDRed, OUTPUT); // Fault: Leaving Staging Line between White2 and Green
}
void loop()
{
// Get current time
unsigned long now = millis();
/*
Everything below this line is AFTER race finished
*/
if (RaceHasFinished)
{
//restart program
}
/*
Everything below this line is for AFTER race starts
*/
else if (RaceHasStarted)
{
// The race has not finished but it has started to the
// race is in progress. Waiting for car to cross the finish line
if (isSensorBlocked(FinishLinePin))
{
// race has just finished
RaceFinishTime = micros(); // Time of Finish
RaceHasFinished = true;
Serial.print("Finishing time (sec): ");
Serial.println((RaceFinishTime - RaceStartTime) / 1000000.0, 6);
}
else
{
// race is still in progress
Serial.print("Reaction Time (sec): ");
Serial.println((micros() - RaceStartTime) / 1000000.0, 6);
}
}
/*
Everything below this line is BEFORE the RACE starts
*/
else if (CountdownStart != 0) // The contdown to the start is in progress
{
// The race has not started or finished but the countdown to start
// is in progress so we light up the lights in sequence
unsigned long elapsedCountdown = now - CountdownStart;
// The car has left the Starting Line before the countdown ended!
if (!isSensorBlocked(StartingLinePin))
{
falseStart();
// DO NOT CONTINUE COUNTDOWN!
CountdownStart = 0;
return;
}
if (elapsedCountdown >= (InitialDelay))
turnOnLight(LEDYellow1);
if (elapsedCountdown >= (InitialDelay + YellowDelay))
turnOnLight(LEDYellow2);
if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay))
turnOnLight(LEDYellow3);
if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay + YellowDelay))
{
// START!
turnOnLight(LEDGreen);
RaceHasStarted = true;
RaceStartTime = micros(); // Measure race in microseconds
CountdownStart = 0; // Countdown is done
}
}
/*
Everything below this line is Pre-Stage or Stage
*/
else
{
// The race is not finished or running and the countdown isn't in
// progress so we are waiting for pre-staging and staging.
while (analogRead(StagingLinePin) > LaserInterruptSensitivity) {//waiting for Stage laser to be blocked
}
// White led 1 is ON when Staging Line sensor is blocked
if (isSensorBlocked(StagingLinePin))
{
turnOnLight(LEDWhite1);
}
else
{
turnOffLight(LEDWhite1);
}
while (analogRead(StartingLinePin) > LaserInterruptSensitivity) {//waiting for Start laser to be blocked
}
// if Starting Line sensor is blocked turn on white led 2 and check other arduino for car stage
if (isSensorBlocked(StartingLinePin))
{
turnOnLight(LEDWhite2);
digitalWrite (ARDUINOoutput, HIGH);
}
while (digitalRead(ARDUINOInput) == LOW) { //waiting for high signal from other arduino
}
}
if (isSensorBlocked(StartingLinePin) && digitalRead(ARDUINOInput) == HIGH)
{
CountdownStart = millis(); // Start the countdown to GREEN
}
}
Wiring looks good.
Introduce Serial.print in strategic points to tell how the execution proceeds! Prrint "Now I am here", "Now I am there", "Waiting for ...". etc.
First, + karma for using code tags.
Next, please don't use Fritzing. Pretty Fritzing pictures are worthless for diagnostic purposes- a hand-drawn schematic is far superior to a Fritzing picture.
How is the "other" Arduino connected?
Have you verified that the photoresistors are working as expected?
I would put some Serial.println() statements in the code to show what is happening.
im going to add the Serial.Print to check out what is going on and maybe need some modificationm the other arduino is connected bassically they both send each other a high signal when the car is on the second laser
For the time being, maybe You can replace that other Arduino by a switch or a cable moved between Vcc and GND. That rules out that other controller.
Check up one controller at the time. Not both together.
I connected the arduino to itself for the time being pinout 3 send the signal to 5 bypassing the other arduino i will not connect the other until i have this one working
I don't understand to 100% but likely You got the idea. Enable the controller "all times" and debug it. Then You can disable it, by the test cable, and check for proper action.
I have it working for the biggest part. A few things that came out:
a LDR has a higher resistance when dark, so you either have to check if it is lower (not higher) or invert the results (~ or ! ) Also set the value to 600 (for my LDR's)
1b. AnalogRead gives a value of 0-1024 if not scaled..
The Countdowncounter is repeatedly reset in the if(isSensorBlocked(StartingLinePin)&& digitalRead(ARDUINOInput) == HIGH) .. Adding && (CountdownStart==0) solves this..
adding a delay and reset all counters and leds in a routine (RestartRace()) makes it continuous.
Yes, there are some more glitches (reactiontime is not implemented right), but i think you can solve them yourself..
I think the 3 orange lights an the green startinglight are the same for both lanes, so you drive them from one output.. This makes that you can do both lanes in one arduino UnoR3 with 6 analoge inputs and 10 outputs (skipping D0 and D1 to avoid problems with programming.. You can use these for switches, for example to manualy restart the program.
Adding an I2C Oled can display the results..
Good luck..
Here your code, changed to make it work..
// Pin values for Lights
const byte LEDWhite1 = 2;
const byte LEDWhite2 = 4;
const byte ARDUINOoutput = 3;
const byte ARDUINOInput = 5;
const byte LEDYellow1 = 6;
const byte LEDYellow2 = 8;
const byte LEDYellow3 = 10;
const byte LEDGreen = 11;
const byte LEDRed = 12;
// Pin values for Lasers
const byte StagingLinePin = A0;
const byte StartingLinePin = A1;
const byte FinishLinePin = A2;
// Delay between the countdoown lights
const unsigned InitialDelay = 7000; // between Stage and first Yellow
const unsigned YellowDelay = 500; // between each Yellow and Green
const int LaserInterruptSensitivity = 600; // sensitivity value to determine if sensor is blocked
// State Flags
bool RaceHasStarted = false; // Set 'true' when Green light is turned on
bool RaceHasFinished = false; // Set 'true' when Finish Line is crossed
// Timers for Race
unsigned long RaceStartTime = 0; // Set to micros() when Green light is turned on
unsigned long ReactionTime = 0; // Set to micros() when car finishes going past start line
unsigned long RaceFinishTime = 0; // Set to micros() when Finish Line is crossed
// Timer for Light Countdown
// Note: Doubles as a state flag: if != 0, countdown is in progress
unsigned long CountdownStart = 0; // Set to millis() when Start Line is crossed
// Checks if sensor on analog input 'pin' is blocked bool
bool isSensorBlocked(byte pin)
{
return (analogRead(pin) <= LaserInterruptSensitivity);
}
// Turns on Light on pin 'lightPin'
void turnOnLight(byte lightPin)
{
digitalWrite (lightPin, HIGH);
}
// Turns off Light on pin 'lightPin'
void turnOffLight(byte lightPin)
{
digitalWrite (lightPin, LOW);
}
// turn on Red LED, turn off all others
void falseStart()
{
// turn on Red LED
turnOnLight(LEDRed);
// turn off all other LEDs
turnOffLight(LEDWhite1);
turnOffLight(LEDWhite2);
turnOffLight(LEDYellow1);
turnOffLight(LEDYellow2);
turnOffLight(LEDYellow3);
turnOffLight(LEDGreen);
}
void setup()
{
Serial.begin(9600);
pinMode(LEDWhite1, OUTPUT); // Pre-stage: Car is on Staging Line
pinMode(LEDWhite2, OUTPUT); // Staged: Car has reached Start Line
pinMode(ARDUINOoutput, OUTPUT); // Arduino sends out signal to other arduino saying car is staged
pinMode(ARDUINOInput, INPUT); // Arduino recieves signal from other arduino saying car is staged
pinMode(LEDYellow1, OUTPUT); // Turns on 'InitialDelay' milliseconds after White2
pinMode(LEDYellow2, OUTPUT); // Turns on 'YellowDelay' milliseconds after Yellow1
pinMode(LEDYellow3, OUTPUT); // Turns on 'YellowDelay' milliseconds after Yellow2
pinMode(LEDGreen, OUTPUT); // Start: Turns on 'YellowDelay' milliseconds after Yellow3
pinMode(LEDRed, OUTPUT); // Fault: Leaving Staging Line between White2 and Green
}
void RestartRace(){
CountdownStart=0;
RaceHasStarted = false;
RaceHasFinished = false;
RaceStartTime = 0;
ReactionTime = 0;
RaceFinishTime = 0;
turnOffLight(LEDRed);
turnOffLight(LEDWhite1);
turnOffLight(LEDWhite2);
turnOffLight(LEDYellow1);
turnOffLight(LEDYellow2);
turnOffLight(LEDYellow3);
turnOffLight(LEDGreen);
}
void loop()
{
// Get current time
unsigned long now = millis();
// Serial.println("millis");
// Serial.print(analogRead(StagingLinePin)); Serial.print(" ");Serial.println(LaserInterruptSensitivity);
/*
Everything below this line is AFTER race finished
*/
if (RaceHasFinished)
{ delay(10000);
RestartRace();
//restart program
}
/*
Everything below this line is for AFTER race starts
*/
else if (RaceHasStarted)
{
Serial.println("RaceHasStarted");
// The race has not finished but it has started to the
// race is in progress. Waiting for car to cross the finish line
if (isSensorBlocked(FinishLinePin))
{
Serial.println("RaceFinished");
// race has just finished
RaceFinishTime = micros(); // Time of Finish
RaceHasFinished = true;
Serial.print("Finishing time (sec): ");
Serial.println((RaceFinishTime - RaceStartTime) / 1000000.0, 6);
}
else
{
// race is still in progress
Serial.print("Reaction Time (sec): ");
Serial.println((micros() - RaceStartTime) / 1000000.0, 6);
}
}
/*
Everything below this line is BEFORE the RACE starts
*/
else if (CountdownStart != 0) // The contdown to the start is in progress
{
// The race has not started or finished but the countdown to start
// is in progress so we light up the lights in sequence
unsigned long elapsedCountdown = now - CountdownStart;
// The car has left the Starting Line before the countdown ended!
if (!isSensorBlocked(StartingLinePin))
{
falseStart();
// DO NOT CONTINUE COUNTDOWN!
CountdownStart = 0;
return;
}
if (elapsedCountdown >= (InitialDelay))
turnOnLight(LEDYellow1);
if (elapsedCountdown >= (InitialDelay + YellowDelay))
turnOnLight(LEDYellow2);
if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay))
turnOnLight(LEDYellow3);
if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay + YellowDelay))
{
// START!
turnOnLight(LEDGreen);
RaceHasStarted = true;
RaceStartTime = micros(); // Measure race in microseconds
CountdownStart = 0; // Countdown is done
}
}
/*
Everything below this line is Pre-Stage or Stage
*/
else
{
// The race is not finished or running and the countdown isn't in
// progress so we are waiting for pre-staging and staging.
while (analogRead(StagingLinePin) > LaserInterruptSensitivity) {//waiting for Stage laser to be blocked
Serial.println(" staging ");
}
// White led 1 is ON when Staging Line sensor is blocked
if (isSensorBlocked(StagingLinePin))
{
turnOnLight(LEDWhite1);
}
else
{
turnOffLight(LEDWhite1);
}
while (analogRead(StartingLinePin) >= LaserInterruptSensitivity) {//waiting for Start laser to be blocked
Serial.println(" starting ");
}
// if Starting Line sensor is blocked turn on white led 2 and check other arduino for car stage
if (isSensorBlocked(StartingLinePin))
{
turnOnLight(LEDWhite2);
// digitalWrite (ARDUINOoutput, HIGH);
}
// while (digitalRead(ARDUINOInput) == LOW) { //waiting for high signal from other arduino
// }
}
if (isSensorBlocked(StartingLinePin)&& (CountdownStart==0) )// && digitalRead(ARDUINOInput) == HIGH)
{
CountdownStart = millis(); // Start the countdown to GREEN
Serial.print(" Countdown ");Serial.println(digitalRead(ARDUINOInput));
}
}
ignisgraecus:
im writing code for making a drag tree using two arduinos, I was helped by other people on this forum, when i run the code nothing happens
What do you expect to happen? You have to simulate a car at the staging line to get the staging lights to come on. Are you blocking the LDRs in the right order? Do you have the LaserInterruptSensitivity value set correctly? Is the comparison in "isSensorBlocked()" the right way around?
You know, this is not as easy as it seems. There's a lot of states this thing ends up going through. I figured I'd just whip this out and, wow! Its just blowing up on me! States within states. Maybe I'm just approaching it with the wrong mindset?
I rewrote some of the code I am testing it using switches for consistency and I cant get the LED's to work or the timers to start can someone help me go over the code and find the errors? I really need help with this
/*
_________ __ __
\_ ___ \ ____ ____ _______/ |______ _____/ |_ ______
/ \ \/ / _ \ / \ / ___/\ __\__ \ / \ __\/ ___/
\ \___( <_> ) | \\___ \ | | / __ \| | \ | \___ \
\______ /\____/|___| /____ > |__| (____ /___| /__| /____ >
\/ \/ \/ \/ \/ \/
*/
// Pin values for Lights
const byte LEDWhite1 = 2;
const byte LEDWhite2 = 4;
const byte ARDUINOoutput = 3;
const byte ARDUINOInput = 5;
const byte LEDYellow1 = 6;
const byte LEDYellow2 = 8;
const byte LEDYellow3 = 10;
const byte LEDGreen = 11;
const byte LEDRed = 12;
// Pin values for Lasers
const byte StagingLinePin = 7;
const byte StartingLinePin = 9;
const byte FinishLinePin = 13;
// Delay between the countdoown lights
const unsigned InitialDelay = 7000; // between Stage and first Yellow
const unsigned YellowDelay = 500; // between each Yellow and Green
const unsigned DebugDelay = 1000; // between each Yellow and Green
// sensitivity value to determine if sensor is blocked
const int LaserInterruptSensitivity = 0;
/*
____ ____ .__ ___. .__
\ \ / /____ _______|__|____ \_ |__ | | ____ ______
\ Y /\__ \\_ __ \ \__ \ | __ \| | _/ __ \ / ___/
\ / / __ \| | \/ |/ __ \| \_\ \ |_\ ___/ \___ \
\___/ (____ /__| |__(____ /___ /____/\___ >____ >
\/ \/ \/ \/ \/
*/
// State Flags
bool RaceHasStarted = false; // Set 'true' when Green light is turned on
bool RaceHasFinished = false; // Set 'true' when Finish Line is crossed
// Timers for Race
unsigned long RaceStartTime = 0; // Set to micros() when Green light is turned on
unsigned long RaceFinishTime = 0; // Set to micros() when Finish Line is crossed
// Timer for Light Countdown
unsigned long CountdownStart = 0; // Note: Doubles as a state flag: if != 0, countdown is in progress; Set to millis() when Start Line is crossed
// Time since start of program
unsigned long now;
/*
___________ __ .__
\_ _____/_ __ ____ _____/ |_|__| ____ ____ ______
| __)| | \/ \_/ ___\ __\ |/ _ \ / \ / ___/
| \ | | / | \ \___| | | ( <_> ) | \\___ \
\___ / |____/|___| /\___ >__| |__|\____/|___| /____ >
\/ \/ \/ \/ \/
*/
// Checks if sensor on analog input 'pin' is blocked
bool isSensorBlocked(byte pin)
{
int val = 0;
val = digitalRead(pin); // read input value
Serial.print("Sensor Value: ");
Serial.print(val);
Serial.print("\n");
return digitalRead(pin) == LaserInterruptSensitivity;
}
// Turns on Light on pin 'lightPin'
void turnOnLight(byte lightPin) {
digitalWrite (lightPin, HIGH);
}
// Turns off Light on pin 'lightPin'
void turnOffLight(byte lightPin) {
digitalWrite (lightPin, LOW);
}
// turn on Red LED, turn off all others
void falseStart() {
// turn on Red LED
turnOnLight(LEDRed);
// turn off all other LEDs
turnOffLight(LEDWhite1);
turnOffLight(LEDWhite2);
turnOffLight(LEDYellow1);
turnOffLight(LEDYellow2);
turnOffLight(LEDYellow3);
turnOffLight(LEDGreen);
}
void checkStagingPin(){
if (isSensorBlocked(StagingLinePin)) {
Serial.print("Staging Pin is blocked\n");
turnOnLight(LEDWhite1);
delay(DebugDelay);
}
else {
Serial.print("Staging Pin NOT blocked\n");
turnOffLight(LEDWhite1);
delay(DebugDelay);
}
}
// if Starting Line sensor is blocked turn on white led 2 and check other arduino for car stage
void checkStartingPin(){
if (isSensorBlocked(StartingLinePin)) {
Serial.print("Start Pin is blocked on THIS Arduino \n");
turnOnLight(LEDWhite2);
digitalWrite (ARDUINOoutput, HIGH);
delay(DebugDelay);
}
if (digitalRead(ARDUINOInput) == HIGH){
Serial.print("Start Pin is blocked on OTHER Arduino \n");
CountdownStart = millis(); // Start the countdown to GREEN
}
}
void RaceInPreStage() {
Serial.print("Pre-Stage\n");
checkStagingPin();
checkStartingPin();
}
void RaceInCountdown() {
unsigned long elapsedCountdown = now - CountdownStart;
// The car has left the Starting Line before the countdown ended!
if (!isSensorBlocked(StartingLinePin)) {
falseStart();
CountdownStart = 0; // DO NOT CONTINUE COUNTDOWN!
return;
}
if (elapsedCountdown >= (InitialDelay)) {
turnOnLight(LEDYellow1);
}
if (elapsedCountdown >= (InitialDelay + YellowDelay)) {
turnOnLight(LEDYellow2);
}
if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay)) {
turnOnLight(LEDYellow3);
}
if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay + YellowDelay)) {
// START!
turnOnLight(LEDGreen);
RaceHasStarted = true;
RaceStartTime = micros(); // Measure race in microseconds
CountdownStart = 0; // Countdown is done
}
}
void RaceInProgress() {
Serial.print("Race has started");
// Car has crossed Finish Line
if (isSensorBlocked(FinishLinePin)) {
RaceFinishTime = micros(); // Time of Finish
RaceHasFinished = true;
Serial.print("Finishing time (sec): ");
Serial.println((RaceFinishTime - RaceStartTime) / 1000000.0, 6);
}
// Car has NOT crossed finish line
else {
// race is still in progress
Serial.print("Reaction Time (sec): ");
Serial.println((micros() - RaceStartTime) / 1000000.0, 6);
}
}
void RaceDone(){
while(1);
}
/*
_____ .___ .__
/ _ \_______ __| _/_ __|__| ____ ____
/ /_\ \_ __ \/ __ | | \ |/ \ / _ \
/ | \ | \/ /_/ | | / | | ( <_> )
\____|__ /__| \____ |____/|__|___| /\____/
\/ \/ \/
*/
void setup() {
Serial.begin(9600);
// Initialize Lights On Drag Tree
pinMode(LEDWhite1, OUTPUT); // Pre-stage: Car is on Staging Line
pinMode(LEDWhite2, OUTPUT); // Staged: Car has reached Start Line
pinMode(LEDYellow1, OUTPUT); // Turns on 'InitialDelay' milliseconds after White2
pinMode(LEDYellow2, OUTPUT); // Turns on 'YellowDelay' milliseconds after Yellow1
pinMode(LEDYellow3, OUTPUT); // Turns on 'YellowDelay' milliseconds after Yellow2
pinMode(LEDGreen, OUTPUT); // Start: Turns on 'YellowDelay' milliseconds after Yellow3
pinMode(LEDRed, OUTPUT); // Fault: Leaving Staging Line between White2 and Green
// Initialize Input Lasers
pinMode(StagingLinePin, INPUT); //checks stage light laser
pinMode(StartingLinePin, INPUT); //checks stage light laser
pinMode(FinishLinePin, INPUT); //checks finish line light laser
// Initialize communications between the two arduinos
pinMode(ARDUINOoutput, OUTPUT); // Arduino sends out signal to other arduino saying car is staged
pinMode(ARDUINOInput, INPUT); // Arduino recieves signal from other arduino saying car is staged
}
void loop()
{
now = millis(); // Get current time
if (RaceHasFinished) {
RaceDone();
}
else if (RaceHasStarted) {
RaceInProgress();
}
else if (CountdownStart != 0) {
RaceInCountdown();
}
else {
RaceInPreStage();
}
}