Currently, I have two loops in my Arduino code.
Certain cases take me from loop 1 to loop 2
I also have an ISR
When my ISR condition is met the code is executed and part of the ISR code is supposed to go to loop 1. Which it does but...
Then I close the ISR accordingly, but the service routine goes back to loop 2.
Any ideas why?
I've been surfing the web for the past couple hours and haven't caught a wave..
Thanks all.
//Physical variables
int eStop = 3; // the number of the input pin
int eStopLED = 9; // the number of the output pin
int dispenseButton = 6; // the number of the input pin
int dispenseLED = 8; // the number of the output pin
// have 789 for colors and 3456 for input only 3 ND 6 NEEDS TO BE USED
int eStopZone = HIGH; // the current eStopZone of the output pin
int eStopScan; // the current eStopScan from the input pin
int eStopPrior = LOW; // the eStopPrior eStopScan from the input pin
int dispenseZone = HIGH; // the current eStopZone of the output pin
int dispenseScan; // the current eStopScan from the input pin
int dispensePrior = LOW; // the eStopPrior eStopScan from the input pin
//Other Variables
volatile int vControl = 0;
int eVar;
int i; //Normal counting integer
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void control(void);
void emergency(void);
//another coid for char ring
void setup()
{
pinMode(eStop, INPUT);
pinMode(eStopLED, OUTPUT);
pinMode(dispenseButton, INPUT);
pinMode(dispenseLED, OUTPUT);
eVar = 0;
Serial.begin(9600); //Start Serial
attachInterrupt(digitalPinToInterrupt(eStop), emergency, CHANGE);
}
void loop()
{
eStopScan = digitalRead(eStop);
dispenseScan = digitalRead(dispenseButton);
//Serial.println(eStopScan);
/*if (eStopScan == HIGH && eStopPrior == LOW && millis() - time > debounce) { //eStop control conditions
switch (eVar) {
case 0:
vControl = 0;
control();
break;
case 1:
vControl = 1;
control();
break;
}
time = millis();
}
*/
if (dispenseScan == HIGH && dispensePrior == LOW && millis() - time > debounce) { //dispense control conditions
Serial.println("in this loop");
switch (eVar) {
case 0:
vControl = 2;
control();
break;
case 1:
vControl = 3;
control();
break;
}
time = millis();
}
eStopPrior = eStopScan;
dispensePrior = dispenseScan;
}
void control () {
switch (vControl) {
case 0: //estop case put in another variable for showing last estop state
eStopZone = HIGH;
dispenseZone = LOW;
digitalWrite(dispenseLED, dispenseZone);
digitalWrite(eStopLED, eStopZone);
eStopScan = LOW;
dispenseScan = LOW;
Serial.println("Case 0");
//eVar = 1;
break;
case 1:
for (int i = 0; i <= 2; i++) {
eStopZone = LOW;
digitalWrite(eStopLED, eStopZone);
delay(345);
eStopZone = HIGH;
digitalWrite(eStopLED, eStopZone);
delay(345);
Serial.println("Case 1");
//Serial.println(i);
}
break;
case 2:
dispenseZone = HIGH;
eStopZone = HIGH;
digitalWrite(eStopLED, eStopZone);
digitalWrite(dispenseLED, dispenseZone);
Serial.println("here 1");
delay(1000);
Serial.println("here?");
eStopZone = LOW;
digitalWrite(eStopLED, eStopZone);
Serial.println("Case 2");
break;
case 3:
dispenseZone = HIGH;
eStopZone = LOW;
digitalWrite(eStopLED, eStopZone);
digitalWrite(dispenseLED, dispenseZone);
eVar = 0;
Serial.println("Case 3");
// make led go green
break;
}
time = millis();
loop();
}
void emergency () {
Serial.println("interrupt");
vControl = 0;
control();
}
Com window from debugging
in this loop
here 1
here?
Case 2
in this loop
here 1
here?
Case 2
in this loop
here 1
interrupt
interrupt
interrupt
here?
Case 2