1 push button with multiple functions?

I am using Arduino Uno R3 and Motor Shield to test solenoids. 2-state solenoids state is chanced when voltage is added. When you chance polarity and add voltage state chances again. 100ms pulse is used to control solenoid.

Everything is working now but I want to use push button to chance state. In addition I would like to use same button to run endurance test which chances state eg. 100 times. I was thinking that state would chance with quick push of the button. Endurance test could be selected with longer push(2 seconds or so). What do you think? I am not so familiar with programming and loops.

Tester code with endurance test(never ending loop) below. Feel free to help :slight_smile:

Solenoid tester 2023
/*************************************************************
Original version:
Motor Shield 1-Channel DC Motor Demo
by Randy Sarafan
*************************************************************/

void setup() {
  
  //Setup Channel A & B
  pinMode(12, OUTPUT);      //Initiates Channel A pin
  pinMode(13, OUTPUT);      //Initiates Channel B pin
  
}

void loop(){
  
  //positive pulse
  digitalWrite(12, HIGH ); //Positive polarity @ Channel A
  digitalWrite(13, HIGH ); //Positive polairty @ Channel B
  analogWrite(3, 255);     //Channel A voltage 12V
  analogWrite(11, 255);    //Channel B voltage 12V
  delay(100);		   //Pulse length 100ms
  analogWrite(3, 0);       //Channel A voltage 0V
  analogWrite(11, 0);      //Channel A voltage 0V
  delay(1000);
  
  //negative pulse 
  digitalWrite(12, LOW);   //Negative polarity @ Channel A
  digitalWrite(13, LOW);   //Negative polarity @ Channel B
  analogWrite(3, 255);     //Channel A voltage 12V
  analogWrite(11, 255);    //Channel B voltage 12V
  delay(100);		           //Pulse length 100ms
  analogWrite(3, 0);        //Channel A voltage 0V
  analogWrite(11, 0);      //Channel B voltage 0V
  delay(1000);
  
  
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

This library will help you to sense state changes (single click), double click and long press.

capture a timestamp when a button is pressed and monitor for when some time period from that timestamp is exceeded.

recognize a "long" press if the timer expires and a "short" press if the button is released before the time expires

do some google searches

Thank you all for your help, i will test those tips.

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