Hello,
I'm trying to create a function that I can call inside my loop that will 'pause' the program and wait (indefinitely) until it receives an input from a limit switch before continuing. I'm using the following function, with the switch sending its input to pin 5.
void drillWait(int drillPin){
int drillState = 0;
while(1){
drillState = digitalRead(drillPin);
if (drillState == LOW) {
return;
}
}
}
that I found here: arduino uno - Pause code untill a button is pressed - Arduino Stack Exchange, but I can't seem to get it to work and I don't quite understand what it's doing well enough to modify it to fit my needs.
I've added my entire code in case it's something somewhere else messing with it (sorry it's messy, I don't know entirely what I'm doing). I've rewired my entire system twice and just can't find the problem.
i
nt mot1d = 12; //motor 1 direction
int mot1s = 3; //motor 1 speed
int mot1b = 9; //motor 1 brake
int pusher = 7; //Pusher air cylinder
int gripper = 4; //gripper
int drill = 6; //Drill air cylinder
int tdoor = 2; //Trap door air cylinder
int IR1 = 10; //Part present IR switch
int i = 1;
int estop = A5; //emergency stop
int mode = A2; //mode switch
int foots = A3; //foot pedal
int x;
int y;
int ks = 0;
int mmode = 0;
//Function for drill sensor; drill until sensor is activated.
void drillWait(int drillPin){
int drillState = 0;
while(1){
drillState = digitalRead(drillPin);
if (drillState == LOW) {
return;
}
}
}
void setup() {
pinMode(mot1d, OUTPUT);
pinMode(mot1s, OUTPUT);
pinMode(mot1b, OUTPUT);
pinMode(pusher, OUTPUT);
pinMode(gripper, OUTPUT);
pinMode(drill, OUTPUT);
pinMode(tdoor, OUTPUT);
pinMode(IR1, INPUT);
pinMode(mode, INPUT);
pinMode(foots, INPUT);
Serial.begin(9600);
}
void loop() {
//motors on
//digitalWrite(mot1d, HIGH);
//analogWrite(mot1s, 255);
//Read e stop pin & mode pin
mmode = digitalRead(A2);
ks = digitalRead(A5);
if (ks == 0) {
ks = digitalRead(A5);
//manual mode program
while (mmode == 1) {
digitalWrite(mot1b, HIGH);
digitalWrite(tdoor, LOW);
digitalWrite(pusher, LOW);
digitalWrite(gripper,LOW);
digitalWrite(drill, LOW);
digitalRead(A5);
//If manual mode is selected, wait for signal from foot switch. After foot switch is activated, close grippers, short delay, drill until drill sensor activates, retract drill and release gripper then wait for foot switch again.
if (digitalRead(foots) == HIGH) {
digitalWrite(gripper,HIGH);
delay(1000);
digitalWrite(drill, HIGH);
delay(500);
drillWait(5);
digitalWrite(drill, LOW);
delay(2000);
digitalWrite(gripper, LOW);
delay(1000);
digitalWrite(tdoor, HIGH);
delay(2000);
digitalWrite(tdoor, LOW);
}
mmode = digitalRead(A2);
}
if (i == 0) {
x = millis();
while ((x - y) <= 1000) {
digitalWrite(mot1d, HIGH);
analogWrite(mot1s, 255);
i = 1;
x = millis();
}
//if stick is present, stop motors and activate pusher. Delay 1 sec, then activate gripper and push down solenoid. Delay 1 sec and activate drill. Time based drilling then exit loop. Need to add sensor for drill position.
}
if (digitalRead(IR1) == LOW) {
digitalWrite(mot1b, HIGH);
digitalWrite(pusher, HIGH);
delay(1000);
digitalWrite(gripper,HIGH);
delay(1000);
digitalWrite(drill, HIGH);
delay(100);
drillWait(5);
digitalWrite(drill, LOW);
digitalWrite(pusher, LOW);
delay(2000);
digitalWrite(gripper, LOW);
delay(1000);
digitalWrite(tdoor, HIGH);
delay(2000);
digitalWrite(tdoor, LOW);
digitalWrite(mot1b, LOW);
digitalRead(IR1);
}
else{
i = 0;
y = millis();
}
}
// estop program
//if (ks == 1) {
// digitalWrite(mot1b, HIGH);
// digitalWrite(pusher, LOW);
// digitalWrite(gripper,LOW);
// digitalWrite(tdoor, LOW);
// digitalWrite(drill, LOW);
// delay (2000);
// ks = digitalRead(A5);
// while (ks == 0) {
// ks = digitalRead(A5);
// }
// delay (1000);
// ks = digitalRead(A5);
// digitalWrite(mot1b, LOW);
}
Basically what's happening is the program runs fine but once the switch is hit the program stays paused. I wasn't sure if maybe I'm telling it to look for the wrong thing (my switch sends a LOW signal when activated), so I tried changing the line in the function to if (drillState == HIGH) just to see what would happen and the program doesn't pause at all, it just continues through the loop.
Any help would be greatly appreciated!