Two servos with different functions

I’m running two microservos controlled by Arduino Uno and one momentary switch.
Microservos are fysicly attached to Volume and Gain potentiometres of my guitar amplifier.
There are two functions in my code both triggered by the same button:
Loud (servos turn up )
Quiet ( servos go back to zero positions)

This is my code:

#include <Servo.h>
Servo servoVolume;          
Servo servoGain;         

const int buttonPin2 = 2; 
int buttonState2 = 0;         
int lastButtonState2 = 0;    
int buttonPushCounter2 = 0;   
boolean currentButton2 = LOW;
const int ledPin7 =  7;     
   
void setup() {
 pinMode(ledPin7, OUTPUT);    
   servoVolume.attach(9);  
     servoGain.attach(10);  
      delay(15); 
    pinMode(buttonPin2, INPUT); 
  }
boolean debounce2(boolean last2)
{
  boolean current2 = digitalRead(buttonPin2);
  if (last2 != current2)
  {
    delay(5);
    current2 = digitalRead(buttonPin2);    
  }
  return current2;   
   }
 void loop()
{
   
 buttonState2 = debounce2(buttonPin2); 
  if (buttonState2 != lastButtonState2) 
{
      if (buttonState2 == HIGH) 
    {
      buttonPushCounter2++;
      } 
    else {
    lastButtonState2 = buttonState2; }
   }
  if (buttonPushCounter2 % 2 == 0) 
 {
 digitalWrite(ledPin7, LOW);  
    Loud();
   delay(15); 
  } else {
 
  digitalWrite(ledPin7, HIGH); 
   Quiet();
   delay(15); 
  } 
 }
void Loud() 
{
  servoVolume.write(90);
  servoGain.write(160);
}

void Quiet() {
  servoVolume.write(0);
  servoGain.write(0);
}

I want so to add another button and function to my code.
This new button should control servoVolume ONLY

I tried for a week to write a new code but in vain.

Would anyone be so kind and at least put me in the right direction?
When I add another button I really get lost in statment about it’s last state and button pushcounter

I want so to add another button and function to my code.

Sounds simple. Just clone and modify these:

const int buttonPin2 = 2; 
int buttonState2 = 0;         
int lastButtonState2 = 0;    
int buttonPushCounter2 = 0;   
boolean currentButton2 = LOW;

    pinMode(buttonPin2, INPUT); 

boolean debounce2(boolean last2)
{
  boolean current2 = digitalRead(buttonPin2);
  if (last2 != current2)
  {
    delay(5);
    current2 = digitalRead(buttonPin2);    
  }
  return current2;   
   }
 buttonState2 = debounce2(buttonPin2); 
  if (buttonState2 != lastButtonState2) 
{
      if (buttonState2 == HIGH) 
    {
      buttonPushCounter2++;
      } 
    else {
    lastButtonState2 = buttonState2; }
   }
  if (buttonPushCounter2 % 2 == 0) 
 {
 digitalWrite(ledPin7, LOW);  
    Loud();
   delay(15); 
  } else {

  digitalWrite(ledPin7, HIGH); 
   Quiet();
   delay(15); 
  } 
 }

and change what happens when the new counter is even.

Using Tools + Auto Format would stop your code from wandering drunkenly all over the page.

Thank you Paul,
Pleas confirm if I understood correctly the “cloning part”
Something like this?

#include <Servo.h>

Servo servoVolume;          
Servo servoGain;         

const int buttonPin2 = 2; 
int buttonState2 = 0;         
int lastButtonState2 = 0;    
int buttonPushCounter2 = 0;   
boolean currentButton2 = LOW;
const int ledPin7 =  7;     


const int buttonPin4 = 4; 
int buttonState4 = 0;         
int lastButtonState4 = 0;    
int buttonPushCounter4 = 0;   
boolean currentButton4 = LOW;
const int ledPin8 =  8;   



void setup() {

  pinMode(ledPin7, OUTPUT);  
  pinMode(ledPin8, OUTPUT);     

  servoVolume.attach(9);  
  servoGain.attach(10);  
  delay(15); 

  pinMode(buttonPin2, INPUT); 
  pinMode(buttonPin4, INPUT); 
}
boolean debounce2(boolean last2)
{
  boolean current2 = digitalRead(buttonPin2);
  if (last2 != current2)
  {
    delay(5);
    current2 = digitalRead(buttonPin2);
  }
  return current2;   
}
boolean debounce4(boolean last4)
{
  boolean current4 = digitalRead(buttonPin4);
  if (last4 != current4)
  {
    delay(5);
    current4 = digitalRead(buttonPin4);
  }
  return current4;  
}
void loop()
{
  buttonState2 = debounce2(buttonPin2); 
  if (buttonState2 != lastButtonState2) 
  {
    if (buttonState2 == HIGH) 
    {
      buttonPushCounter2++;
    } 
    else {
      lastButtonState2 = buttonState2;
    }
  }
  if (buttonPushCounter2 % 2 == 0) {
    digitalWrite(ledPin7, LOW);  
    Loud();
    delay(15); 
  } 
  else {
    digitalWrite(ledPin7, HIGH); 
    Quiet();
    delay(15); 
  } 
  buttonState4 = debounce4(buttonPin4); 
  if (buttonState4 != lastButtonState4) {

    if (buttonState4 == HIGH) 
    {

      buttonPushCounter4++;
    } 
    else {
      lastButtonState4 = buttonState4;
    }
  }
  if (buttonPushCounter4 % 2 == 0) {
    digitalWrite(ledPin8, LOW);  
    volumeup();
    delay(15); 
  } 
  else {
    digitalWrite(ledPin8, HIGH); 
    volumedown();
    delay(15); 
  } 
}
void Loud() 
{
  servoVolume.write(90);
  servoGain.write(160);
}
void Quiet() {
  servoVolume.write(0);
  servoGain.write(0);
}
void volumeup() {
  servoVolume.write(90);

}
void volumedown() {
  servoVolume.write(0);
}

Something like this?

Looks OK to me. But, then, I'm neither a compiler or an Arduino. What does the compiler tell you? What does the Arduino do?

It compiles fine, no issues at all. I'll upload it do test later when I get home. Thank you very much!

It didn't work! I'm guessing my ELSE statement is problem now. It worked fine when I had only one button. Now I got another button and it's obvious that ELSE can mean a lot of other events. Those "events" make my servos behave crazy.

Is it possible to resolve this code by using "IF"-conditions only?

Some servo toggle code that might be of use.

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
Servo servo1;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  servo1.attach(8); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      servo1.write(20);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      servo1.write(160);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}
//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

I'm guessing my ELSE statement is problem now.

Would that be this else:

    else {
      lastButtonState2 = buttonState2;
    }

or this else

  else {
    digitalWrite(ledPin7, HIGH); 
    Quiet();
    delay(15); 
  }

or this else:

    else {
      lastButtonState4 = buttonState4;
    }

or this else:

  else {
    digitalWrite(ledPin8, HIGH); 
    volumedown();
    delay(15); 
  }

Is it possible to resolve this code by using "IF"-conditions only?

if(youWantTo)

I'm not sure but probably all my ELSE statements are wrong because I got another button and when I press it it's something ELSE isn't it? I guess it's affecting the counter. It happens that servos go correctly when I press the button several times . After that they go crazy and so on. I'll try tomorrow again using only "IF".

It happens that servos go correctly when I press the button several times . After that they go crazy and so on.

Perhaps you need to take the servos to a mental health specialist. Or perhaps you need to use terms that mean something in the context of software and hardware.

You might also consider adding Serial.print() statements to prove that the switches are wired/being read correctly.

By the way, the lastButtonStateN assignments should NOT be in else blocks. That should happen unconditionally.

Thank you guys! I really appreciate your help.

@zoomcat Do your exsamples reffer to latching buttons? I can use momentary only...

@PaulS Sorry for my silly description of servo behaviour! I'll try to work more arround my code and get back for more help. It's obvious that I don't know most basic things about programming but I this time I really try hard before asking for help. First of all I'll try to move "lastbuttonState" out from ELSE block. My buttons are properly wired. I tested them by using my old "one button code".

sikter

Do your exsamples reffer to latching buttons? I can use momentary only…

I haven’t tried the below with a servo, but it may do what you want.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

zoomkat:

Do your exsamples reffer to latching buttons? I can use momentary only…

I haven’t tried the below with a servo, but it may do what you want.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

I’ll test it.
Thank you

Back to my initial code (for two buttons and two servos)
I found out that problem could be in my functions.
One button is supposed to run both servos and another button only one servo.
It might cause some conflict (I think)

I tried to run one servo with first button and the other servo with second button and it worked.