Changing a program from buttons pressed to buttons held

G'day Mates,

I've got a machine I'm building.

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.

Also when looking for button activity try this:

if (start1 == LOW && start2 == LOW) {
//perform action
}

If (start1 == HIGH || start2 == HIGH) {
//turn everything LOW
}

That might be worth a try anyway, still might have to use the Millis function for timing so the Arduino can be looking for that last if statement.

THanks but I don't think what you said will help.

It is good to use millis()

but delay(250) then make a check - while long, will work almost as well
I don't know why I get that error message using break?

What I need is my process running only WHILE the buttons are HELD DOWN meaning (start1value == 0 && start2value == 0)

The if statement example I think will just work as it already dose now ( pressing buttons once for a full action)

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);
}


}

Well break did not work because it was an if statement, break only works in for and while loops.

I used return instead which took me back to the begining of the program.

My main error was not continuously updating with.
start1value = digitalRead(start1);
start2value = digitalRead(start2);

I previously only did this at the start of the code

THanks

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.