The process fires a bunch of pneumatic actuators after 2 buttons are pressed simultaneously.
I decided I want the process carried out only while the buttons are held down. If either or both buttons are released, all pneumatics go to their retracted state (Low signals)
I tried replacing my main "if" loop with "while" - the process just fired on repeat.
I decided its easy enough to replace all delay(2000) with 8 x
if (start1value == 1 || start2value == 1){
break;
}
delay(250);
I get an error telling me the break is not in a loop (break statement not within loop or switch)
Any help would be much appreciated!
Thank you!
Hurricane
const int s1_clamping = 12;
const int s2_dowel = 11;
const int s3_press = 10;
const int start1 = 3;
const int start2 = 4;
int start1value = 0;
int start2value = 0;
void setup() {
// put your setup code here, to run once:
pinMode(s1_clamping, OUTPUT);
pinMode(s2_dowel, OUTPUT);
pinMode(s3_press, OUTPUT);
pinMode(start1, INPUT_PULLUP);
pinMode(start2, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int start1value = 0;
int start2value = 0;
start1value = digitalRead(start1);
start2value = digitalRead(start2);
digitalWrite(s1_clamping, LOW);
digitalWrite(s2_dowel, LOW);
digitalWrite(s3_press, LOW);
if (start1value == 0 && start2value == 0){
Serial.print(start1value);
Serial.print("fire");
digitalWrite(s1_clamping, HIGH);
if (start1value == 1 || start2value == 1){
break;
}
delay(250);
digitalWrite(s2_dowel, HIGH);
delay(2000);
digitalWrite(s3_press, HIGH);
delay(2000);
digitalWrite(s3_press, LOW);
delay(2000);
digitalWrite(s2_dowel, LOW);
delay(2000);
digitalWrite(s1_clamping, LOW);
delay(2000);
}else
Serial.print("dont fire");
digitalWrite(s1_clamping, LOW);
digitalWrite(s2_dowel, LOW);
digitalWrite(s3_press, LOW);
delay(1000);
}
This is for sure a job for using Millis. Seeing how you have timers set up and you want the buttons activating only while pressed you will need to use the Millis so the timing can be running in background and the Arduino is still looking for the button release.
Looks like right now, once you start the process with two buttons, it will run that course until finished and then check for the buttons being released.
Also, I wouldn’t think you need a break; where you have it.
The while statement should work. However the delay would cause it to take a pause in reading meaning it will not be looking for button release because it’s busy in delay. If you use millis you shouldn’t need the break; I have ran a similar code before as ran into same issues. Once I used millis, problem went away.
Also adding debounce to the buttons might help since buttons tend to bounce around. Won’t need much debounce in it though.
THanks
This is what I have now, but it doesn't work.
What happens is a press the 2 buttons and only s1_clamping fires - then it freezes there and removing my fingers from the buttons doesn't do anything either
const int s1_clamping = 12;
const int s2_dowel = 11;
const int s3_press = 10;
const int start1 = 3;
const int start2 = 4;
int start1value = 0;
int start2value = 0;
const unsigned long interval2000 = 2000;
unsigned long previoustime = 0;
void setup() {
// put your setup code here, to run once:
pinMode(s1_clamping, OUTPUT);
pinMode(s2_dowel, OUTPUT);
pinMode(s3_press, OUTPUT);
pinMode(start1, INPUT_PULLUP);
pinMode(start2, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int start1value = 0;
int start2value = 0;
start1value = digitalRead(start1);
start2value = digitalRead(start2);
digitalWrite(s1_clamping, LOW);
digitalWrite(s2_dowel, LOW);
digitalWrite(s3_press, LOW);
unsigned long currenttime = millis();
if (start1value == 0 && start2value == 0){
while (start1value == 0 && start2value == 0){
Serial.print(start1value);
Serial.print("fire");
digitalWrite(s1_clamping, HIGH);
previoustime=currenttime;
if (currenttime - previoustime >= interval2000){
digitalWrite(s2_dowel, HIGH);
previoustime=currenttime;
}
if (currenttime - previoustime >= interval2000){
digitalWrite(s3_press, HIGH);
previoustime=currenttime;
}
if (currenttime - previoustime >= interval2000){
digitalWrite(s3_press, LOW);
previoustime=currenttime;
}
if (currenttime - previoustime >= interval2000){
digitalWrite(s2_dowel, LOW);
previoustime=currenttime;
}
if (currenttime - previoustime >= interval2000){
digitalWrite(s1_clamping, LOW);
previoustime=currenttime;
}
if (currenttime - previoustime >= interval2000){
previoustime=currenttime;
return;
}
}
digitalWrite(s1_clamping, LOW);
digitalWrite(s2_dowel, LOW);
digitalWrite(s3_press, LOW);
}
else{
Serial.print("dont fire");
digitalWrite(s1_clamping, LOW);
digitalWrite(s2_dowel, LOW);
digitalWrite(s3_press, LOW);
}
}