i run my code and when it gets to the void checkStartingPin() it calls a function that turns on a light but it only does it 80% of the time is it skipping the part of the code for some reason?
/*
_________ __ __
\_ ___ \ ____ ____ _______/ |______ _____/ |_ ______
/ \ \/ / _ \ / \ / ___/\ __\__ \ / \ __\/ ___/
\ \___( <_> ) | \\___ \ | | / __ \| | \ | \___ \
\______ /\____/|___| /____ > |__| (____ /___| /__| /____ >
\/ \/ \/ \/ \/ \/
*/
// Pin values for Lights
const byte LEDWhite1 = 2;
const byte LEDWhite2 = 5;
const byte ARDUINOoutput = 50;
const byte ARDUINOInput = 51;
const byte LEDYellow1 = 6;
const byte LEDYellow2 = 8;
const byte LEDYellow3 = 10;
const byte LEDGreen = 11;
const byte LEDRed = 12;
const byte WinnerOut = 7;
const byte WinnerIn = 9;
// Pin values for Lasers
const byte StagingLinePin = 22;
const byte StartingLinePin = 23;
const byte EightMileLinePin = 28;
const byte FinishLinePin = 24;
//handicap timer
unsigned long Handicapvalue = 0;
// Delay between the countdoown lights
const unsigned InitialDelay = 1000; // between Stage and first Yellow
const unsigned YellowDelay = 500; // between each Yellow and Green
const unsigned HandicapDelay = Handicapvalue; // Handicap
/*
____ ____ .__ ___. .__
\ \ / /____ _______|__|____ \_ |__ | | ____ ______
\ 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
unsigned long RaceEightMile = 0; // Set to micros() when 1/8 Finish Line is crossed
unsigned long ReactionTimer = 0; // This will contain the reaction time
unsigned long HandicapTimer = 0;
// 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;
/*
___________ __ .__
\_ _____/_ __ ____ _____/ |_|__| ____ ____ ______
| __)| | \/ \_/ ___\ __\ |/ _ \ / \ / ___/
| \ | | / | \ \___| | | ( <_> ) | \\___ \
\___ / |____/|___| /\___ >__| |__|\____/|___| /____ >
\/ \/ \/ \/ \/
*/
// 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 (digitalRead(StagingLinePin) == HIGH) {
turnOnLight(LEDWhite1);
}
else if (digitalRead(StagingLinePin) == LOW) {
turnOffLight(LEDWhite1);
}
}
void checkStartingPin() {
if (digitalRead(StartingLinePin) == HIGH) {
turnOffLight(LEDRed); // Turn off the red light again. We are back at the start.
//digitalWrite(LEDWhite2, HIGH);
turnOnLight(LEDWhite2);
//Serial.print("this is wheer light s supposed to go on");
digitalWrite (ARDUINOoutput, HIGH);
} else if (StartingLinePin == LOW) {
turnOffLight(LEDWhite2);
digitalWrite(ARDUINOoutput, LOW);
}
if ((digitalRead(ARDUINOInput) == HIGH) && (digitalRead(StartingLinePin) == HIGH)) { // if Starting Line sensor is blocked turn on white led 2 and check other arduino for car stage
CountdownStart = millis(); // Start the countdown to GREEN
}
}
void RaceInPreStage() {
checkStagingPin();
checkStartingPin();
}
void RaceInCountdown() {
unsigned long elapsedCountdown = now - CountdownStart;
checkStagingPin();
// The car has left the Starting Line before the countdown ended!
if (digitalRead(StartingLinePin) == LOW) {
falseStart();
CountdownStart = 0; // DO NOT CONTINUE COUNTDOWN!
return;
}
if (elapsedCountdown >= (InitialDelay + HandicapDelay)) {
turnOnLight(LEDYellow1);
}
if (elapsedCountdown >= (InitialDelay + YellowDelay + HandicapDelay)) {
turnOnLight(LEDYellow2);
}
if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay + HandicapDelay)) {
turnOnLight(LEDYellow3);
}
if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay + YellowDelay + HandicapDelay)) {
turnOnLight(LEDGreen);
turnOffLight(LEDWhite1);
turnOffLight(LEDWhite2);
turnOffLight(LEDYellow1);
turnOffLight(LEDYellow2);
turnOffLight(LEDYellow3);
digitalWrite(WinnerOut, LOW);
RaceHasStarted = true;
RaceStartTime = micros(); // Measure race in microseconds
CountdownStart = 0; // Countdown is done
}
}
void RaceInProgress() {
//HandicapTimer = (Handicapvalue * 1000);
if ( ReactionTimer == 0 && (digitalRead(StartingLinePin) == LOW)) {
ReactionTimer = micros() - RaceStartTime; // When the car leaves the starting line after we turned green, capture the reaction time
Serial.print("Reaction Time: ");
Serial.println(ReactionTimer / 1000000.0, 4);
}
if (digitalRead(FinishLinePin) == HIGH ) {
RaceFinishTime = micros(); // Time of Finish
RaceHasFinished = true;
Serial.print("Finish time: ");
Serial.println((RaceFinishTime - RaceStartTime - ReactionTimer) / 1000000.0, 4);
Serial.print("Total time: ");
Serial.println(((RaceFinishTime - RaceStartTime) ) / 1000000.0, 4);
}
}
void RaceDone() {
digitalWrite(WinnerOut, HIGH);
if (digitalRead(WinnerIn) == HIGH) {
Serial.print("Done Second ");
}
else if (digitalRead(WinnerIn) == LOW) {
Serial.print("Done First");
}
while (1);
}
/*
_____ .___ .__
/ _ \_______ __| _/_ __|__| ____ ____
/ /_\ \_ __ \/ __ | | \ |/ \ / _ \
/ | \ | \/ /_/ | | / | | ( <_> )
\____|__ /__| \____ |____/|__|___| /\____/
\/ \/ \/
*/
void setup() {
Serial.begin(9600);
// Initialize Lights On Drag Tree
pinMode(LEDWhite1, OUTPUT); // Pre-stage light
pinMode(LEDWhite2, OUTPUT); // Stage light
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 pins that tells who finished first
pinMode(WinnerOut, OUTPUT);
pinMode(WinnerIn, OUTPUT);
// 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
pinMode(EightMileLinePin, INPUT); //checks 1/8 mile line pin
// 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();
}
}
the application is a drag tree so when the car goes over the first sensor the first led comes on, then the second laser (this is where is the glitch is it only does it 80% of the time) the second light should come on, i am using two due's because the drag race is two lanes and wanted each due to manage each of the lanes
The funny thing is the part of the code where the problem is
if (digitalRead(StartingLinePin) == HIGH) {
turnOffLight(LEDRed); // Turn off the red light again. We are back at the start.
turnOnLight(LEDWhite2);
Serial.print("this is where the light is supposed to go on");
digitalWrite (ARDUINOoutput, HIGH);
} else if (StartingLinePin == LOW) {
turnOffLight(LEDWhite2);
digitalWrite(ARDUINOoutput, LOW);
}
if ((digitalRead(ARDUINOInput) == HIGH) && (digitalRead(StartingLinePin) == HIGH)) { // if Starting Line sensor is blocked turn on white led 2 and check other arduino for car stage
CountdownStart = millis(); // Start the countdown to GREEN
}
}
It doesnt do anything inside the first if statement but the second if statement where it reads rhe arduino input and the starting line pin high it keeps going
I rewrote part of the code to and im still getting the same issue
switch (startvalue) {
case true:
turnOnLight(LEDWhite2);
Serial.print("this is where light s supposed to go on");
digitalWrite (ARDUINOoutput, HIGH);
turnOffLight(LEDRed); // Turn off the red light again. We are back at the start.
break;
case false:
turnOffLight(LEDWhite2);
digitalWrite(ARDUINOoutput, LOW);
break;
}
Exactly what I pointed out in #1 already, but OP is indeed just racing along it seems...
ignisgraecus:
I rewrote part of the code to and im still getting the same issue
Obviously the issue is in the part you didn't post.
I just had a look at the schematic; the code mentions a StartingLinePin at pin 23, there is nothing connected to that pin in the schematic. That's an obvious issue.
There's some connection to pin 28 in the schematic that's marked Arduino Nano - which you didn't mention at all in your previous messages. Pin 28 is called "EightMileLinePin" in the code. What is that about?
I don't know if there are more issues, but that's what I found so far.
A complete, detailed description of what your code is supposed to do (beginning to end of the procedures) and where it differs from what it should do, will be helpful.
It working sometimes but not all the times is very often the result of a poor electrical connection.
Hi,
How have you wired the switch?
If it is between the input and Vcc have you got a 10K pull_down resistor?
If it is between the input and gnd have you got a 10K pull_up resistor?
You cannot get reliable operation of your input if you leave it open circuit when the switch is open.
Open circuit input does not mean it is LOW or at gnd.
PLEASE a complete circuit, you don't seem to realize that you have your complete project in front of you, WE DON"T.
the schematic is the complete circuit for testing reasons where it says nano is just the signal going in im using nanos because im using NRFl01 transmitters from the finish line to the start, on the schematic since its using the laser sensor modules that have built in resistors that is why you cant see them there ( I have already tested with those modules and get the same result), using the switches i have tried a pullup between input and vcc and a pulldown between input and ground, always getting the same end result, the different pins on the schematic was me trying different pins on the code thinking it might have been the pins that were the problem, the eightmile pin is just if i want to add another sensor in the future i cant paste the whole code and explanation becuause it exceeds the character amounts sorry
Hi,
It looks like you are looking for your input state to be high to trigger an event.
You should be looking for a transition from HIGH to LOW or LOW to HIGH to trigger the event.
You are declaring inputmodes as INPUT, you need a pullup resistor if you switch input to gnd or a pulldown resistor if you are switching input to Vcc.
You cannot get reliable digital input if one of your states is an open circuit input.
If you suspect bounce, write some code JUST to evaluate the possibility.
Did you write your code in stages, if so you must have some code where you tested your input circuit and strategy.