Hello everyone, I'm new, but I have developed a program with then has been tweaked by others. I am having some difficulty with one are of the software code below. This code is for a BMX practice gate and where I am having problems is in the "Loop". When I depress my start button the following should happen:
1 - delay of 2 seconds for the BMX rider to get setup
2 - an audio file starts the cautionary cadence which last for 5.3 seconds
3 - after the 5.3 seconds there is a random delay generation between .1 sec and 2.7 seconds.
This all works fine, but if during the cautionary cadence (within the 5.3 seconds) I press the start button again, I need the loop to stop and immediately jump to the abort sequence. My problem is that I believe the delay within the attachinterrupt won't allow this. Can someone look at the code I have and tell me a way around this. I have highlighted it in BOLD Green text.
Any help you can provide would be great and very much appreciated!!!
I am loading all the code so you can see how this bad boy works:
int outPin = 1; // communication line to the MP3 Trigger
int startpbVal = 0; // variable for reading the Start PB state
int startpbState=0; // variable for holding the start PB state
volatile int val = 0; // initiate dummy variable for use as interrupt loop delay
volatile int abt=0; // initialize abort counter at 0
const int startpbPin = 2; // Start pushbutton
const int gateupPin = 3; // Initialization/Reset sequence pushbutton
const int grnledPin = 5; // Green LED on the light tree
const int yelled2Pin = 6; // Yellow #2 LED on the light tree
const int yelled1Pin = 7; // Yellow #1 LED on the light tree
const int redledPin = 8; // Red LED on the light tree
const int upsolPin = 9; // Solenoid that holds the gate up
const int dwsolPin = 10; // Solenoid that holds the gate down
long randNumber; // sets variable rand "random" to a long integer.
void setup() {
Serial.begin(9600); //Initialize serial communications
pinMode(outPin, OUTPUT); //transmit info to MP3 Trigger to play specified track
pinMode(redledPin, OUTPUT); //Output to Red LED transistor for LED on light tree
pinMode(yelled1Pin, OUTPUT); //Output to Yellow1 LED transistor for LED on light tree
pinMode(yelled2Pin, OUTPUT); //Output to Yellow2 LED transistor for LED on light tree
pinMode(grnledPin, OUTPUT); //Output to Green LED transistor for LED on light tree
pinMode(upsolPin, OUTPUT); //Output to Gate Up Solenoid transistor
pinMode(dwsolPin, OUTPUT); //Output to Gate Down Solenoid transistor
pinMode(startpbPin, INPUT); //start and abort pushbutton (abort not funtioning yet)
pinMode(gateupPin, INPUT); //gate up PB which functions only once for now.
digitalWrite(upsolPin, LOW); //Part of the command to drive the solenoid down so that the gate doesn't go up out of control
digitalWrite(dwsolPin, HIGH); //part of the command to drive the solenoid down so that the gate doesn't go up out of control
}
void loop(){
digitalWrite(redledPin, LOW); //reset red light from abort function call
abt=0; //reset abort counter
if (digitalRead(gateupPin) == HIGH) { //check to see if the gate up button is pressed.
** gateup(); //go to gateup sub-routine **
**} **
startpbVal = digitalRead(startpbPin); //read input value and store it in startpbVal
if (startpbVal != startpbState){ //the button state has changed!
** delay(0500); //delay half a second to allow the user to get their finger off the button**
** attachInterrupt(0,abort, RISING); //when input goes from low to high, perform the abort function**
** Serial.begin(38400); //if analog input pin 0 is unconnected, random analog**
** randomSeed(analogRead(0)); //noise will cause the call to randomSeed() to generate**
** //different seed numbers each time the sketch runs.**
** //randomSeed() will then shuffle the random function. **
** digitalWrite(redledPin, LOW); //sets Red LED to off**
** digitalWrite(yelled1Pin, LOW); //sets Yellow LED #1 to off**
** digitalWrite(yelled2Pin, LOW); //sets Yellow LED #2 to off**
** digitalWrite(grnledPin, LOW); //sets Green LED #1 to off**
** //Beginning of the cadence section **
** delay(2000); //two second delay to allow the ride a chance to get settled.**
** Serial.write('t'); //lowercase "t" must be in single quotes…**
** Serial.write(1); //commands MP3 trigger to play track001 - UCI cadence**
** delay(5350); //starts the 5.3 seconds delay for the cadence to complete**
** detachInterrupt(0); //removes the interupt function from the start pin**
** if(abt>2){ //if abt=1, then**
** Serial.write('t'); //command to mp3 trigger to initialize**
** Serial.write(4); //command to mp3 trigger to play track 4**
** digitalWrite(redledPin, HIGH); //while playing abort tone, hold red light on**
** delay(1000);**
** loop(); //exit start sequence**
** }**
else{ //if pressed, then perform start sequence
randNumber = random(100, 2700); //Generates a random number between 100 and 2700 (.1 sec to 2.7 sec)
delay(randNumber); //sets random number into delay for start circuit
Serial.write('t'); //lowercase "t" must be in single quotes…
Serial.write(2); //causes the MP3 trigger to play track002
delay(0060);
digitalWrite(redledPin, HIGH); //sets the RED LED to on
delay(0120); //sets delay for .120 seconds
digitalWrite(yelled1Pin, HIGH); //sets the Yellow LED #1 to on
delay(0120); //sets delay for .126 seconds
digitalWrite(yelled2Pin, HIGH); //sets the Yellow led #2 to on
delay(0120); //sets delay for .126 seconds
digitalWrite(grnledPin, HIGH); //sets the Green LED to on
digitalWrite(upsolPin, LOW); //command to drop the gate
digitalWrite(dwsolPin, HIGH); //command to drop the gate
delay(2250); //required delay for tone and lights
digitalWrite(redledPin, LOW); //sets Red LED to off
digitalWrite(yelled1Pin, LOW); //sets Yellow LED #1 to off
digitalWrite(yelled2Pin, LOW); //sets Yellow LED #2 to off
digitalWrite(grnledPin, LOW); //sets Green LED #1 to off
delay(3000); //three second delay before the gate automatically goes up
gateup(); //calls the reinitialization sequence
}
}
}
void gateup(){
Serial.begin(38400); //if analog input pin 0 is unconnected, random analog
Serial.write('t'); //command to mp3 trigger to initialize
Serial.write(3); //command to mp3 triggger to play track 3
digitalWrite(grnledPin, HIGH); //sets the Green LED to on
delay(2250); //delay to allow initialization tone to complete
digitalWrite(upsolPin, HIGH); //actuate the upward driving solenoid
digitalWrite(dwsolPin, LOW); //deactivate the downward driving solenoid
digitalWrite(grnledPin, LOW); //signal completion of the initialization sequence
return; //return from initialization function
}
void abort(){
val=0; //implement a delay funtion (inside an interrupt function
while(val<5000){ //delay calls do not function, )
val++;
}
abt=abt++; //increment button press counter
return; //return from abort function
}