Changing different mode on servo motor with a push button

Hi everyone,
Currently I’m working on a project and have been stuck on programming for days and I desperately need help. I think its a simple program but can someone please help me. PS I’m not good in programming.

I need my servo to rotate clockwise and counter-clockwise in a certain speed and angle. A switch button is included to change to mode with every press.

Example:
Button switch is pressed Once, servo rotate with speed “A” and angle of 10 degrees
Twice, servo rotate with speed “B” and angle of 60 degrees
Thrice, servo rotate with speed “C” and angle of 50 degrees
Fourth, everything starts from the beginning, with speed “A” and angle of 10 degrees

Here’s a copy of my codes

#include<Servo.h>

Servo myservo;

int switchState = 0;
int pos = 0;
int val;

int lastswitchState = 0; // previous state of the button
int switchcount = 0;

void setup()
{
myservo.attach(9);
Serial.begin(9600);

pinMode(2,INPUT);
}

void loop()
{
switchState = digitalRead(2);

if (switchState != lastswitchState)
{

if (switchState == HIGH)
{
switchcount++;
for(pos = 0; pos < 10; pos += val) // goes from 0 degrees to 10 degrees
{
val = map(val, 0, 1023, 1, 25);
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(30); // time for the servo to reach the position
}

for(pos = 10; pos>=1; pos -= val) // goes from 10 degrees to 0 degrees
{
val = map(val, 0, 1023, 1, 25);
myservo.write(pos);
delay(30);
}
}
}

if (switchState == HIGH)
{
switchcount++;
for(pos = 0; pos < 60; pos += val) // goes from 0 degrees to 60 degrees
{
val = map(val, 0, 1023, 1, 25);
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // time for the servo to reach the position
}

for(pos = 60; pos>=1; pos -= val) // goes from 60 degrees to 0 degrees
{
val = map(val, 0, 1023, 1, 25);
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}

if (switchState == HIGH)
{
switchcount++;
for(pos = 0; pos < 50; pos += val) // goes from 0 degrees to 50 degrees
{
val = map(val, 0, 1023, 1, 25);
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(100); // time for the servo to reach the position
}

for(pos = 50; pos>=1; pos -= val) // goes from 50 degrees to 0 degrees
{
val = map(val, 0, 1023, 1, 25);
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(100); // waits 15ms for the servo to reach the position
}
}
if(switchState != lastswitchState)
{
if(switchState == 1)
{
switchcount = switchcount +1;
}
}
lastswitchState = switchState;

delay(200);
}

here’s an attached file of the program
Thanks everyone for the time and kind help :slight_smile:

BETA_testing_Heart_Beat_Simulation.ino (2.82 KB)

Removing all the code that diddles with the servo, this is what your loop() function looks like - properly formatted:

void loop()
{
  switchState = digitalRead(2);
  if (switchState != lastswitchState)
  {
    if (switchState == HIGH)
    {
      switchcount++;
    }    
  }

  if (switchState == HIGH)
  {
    switchcount++;
  }

  if (switchState == HIGH)
  {
    switchcount++;
  }
  if(switchState != lastswitchState)
  {
    if(switchState == 1)
    {
      switchcount = switchcount +1;
    }
  }
  lastswitchState = switchState;

  delay(200);
}

I think, then, that you can see that you are diddling with switchcount far too often. It should be obvious, too, that you are NOT counting switches, so the variable name should not indicate that you are. Use a more meaningful name, like gfgdASgasdhadbbvsdfbha.

It should also be obvious that you care about the switch state AND that you care about the number of times that the switch is pressed, but that you care about those ON THE SAME LEVEL.

So, put the code that cares about the switch state, and incrementing the counter, in one function.
Put the code that cares about what the value in the counter is in another function. Call the two functions from loop. This way, it will be impossible to structure the code the way that you have.

okay, so I have modify the code. But it isn’t working too.
please help. I’m really bad at programming.

#include<Servo.h>

Servo myservo;

int switchState = 0;
int pos = 0;
int val;

int lastswitchState  = 0;     // previous state of the button
int switchcount = 0;

void setup() 
{ 
  myservo.attach(9); 
  Serial.begin(9600);
  
  pinMode(2,INPUT);
} 

void loop()
{
  switchState = digitalRead(2);
  
  if (switchState != lastswitchState) 
 {

  if (switchState == HIGH)
    {
          switchcount++;
          for(pos = 0; pos < 10; pos += val)  // goes from 0 degrees to 10 degrees 
            {  
              val = map(val, 0, 1023, 1, 25);
              myservo.write(pos);              // tell servo to go to position in variable 'pos' 
              delay(30);                       // time for the servo to reach the position 
            }
 
         for(pos = 10; pos>=1; pos -= val)     // goes from 10 degrees to 0 degrees 
            {   
              val = map(val, 0, 1023, 1, 25);  
              myservo.write(pos);
              delay(30); 
            } 
    }    
 }
    
    if (switchState == HIGH)
      {
            switchcount++;
            for(pos = 0; pos < 60; pos += val)  // goes from 0 degrees to 60 degrees 
              {  
                val = map(val, 0, 1023, 1, 25);
                myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                delay(50);                       // time for the servo to reach the position 
              }
 
            for(pos = 60; pos>=1; pos -= val)     // goes from 60 degrees to 0 degrees 
              {  
                val = map(val, 0, 1023, 1, 25);  
                myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                delay(50);                       // waits 15ms for the servo to reach the position 
              } 
      }
      
          if (switchState == HIGH)
      {
            switchcount++;
            for(pos = 0; pos < 50; pos += val)  // goes from 0 degrees to 50 degrees 
              {  
                val = map(val, 0, 1023, 1, 25);
                myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                delay(100);                       // time for the servo to reach the position 
              }
 
            for(pos = 50; pos>=1; pos -= val)     // goes from 50 degrees to 0 degrees 
              {  
                val = map(val, 0, 1023, 1, 25);  
                myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                delay(100);                       // waits 15ms for the servo to reach the position 
              } 
      }
            if(switchState != lastswitchState)
            {
              if(switchState == 1)
              {
                switchcount = switchcount +1;
              }
            }
      lastswitchState = switchState;
      
      delay(200);
}

hi I had just recode the program another time. this is how it looks.
I had managed to get the program to run the servo. but it ran all the 3 different mode in 1 press of the push button instead of 3 separate presses.

#include<Servo.h>

Servo myservo;

int switchState = 0;
int pos = 0;
int val;

int lastswitchState  = 0;     // previous state of the button
int switchcount = 1;

void setup() 
{ 
  myservo.attach(9); 
  Serial.begin(9600);
  
  pinMode(2,INPUT);
} 

void loop()
{
  switchState = digitalRead(2);
  
  if (switchState != lastswitchState) 
 {
    
  if (switchState == HIGH && switchcount == 1)
    {
          switchcount++;
          for(pos = 0; pos < 10; pos += val)  // goes from 0 degrees to 10 degrees 
            {  
              val = map(val, 0, 1023, 1, 25);
              myservo.write(pos);              // tell servo to go to position in variable 'pos' 
              delay(30);                       // time for the servo to reach the position 
            }
 
         for(pos = 10; pos>=1; pos -= val)     // goes from 10 degrees to 0 degrees 
            {   
              val = map(val, 0, 1023, 1, 25);  
              myservo.write(pos);
              delay(30); 
            } 
    }    
 }
    
    if (switchState == HIGH && switchcount == 2)
      {
            switchcount++;
            for(pos = 0; pos < 60; pos += val)  // goes from 0 degrees to 60 degrees 
              {  
                val = map(val, 0, 1023, 1, 25);
                myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                delay(20);                       // time for the servo to reach the position 
              }
 
            for(pos = 60; pos>=1; pos -= val)     // goes from 60 degrees to 0 degrees 
              {  
                val = map(val, 0, 1023, 1, 25);  
                myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                delay(20);                       // waits 15ms for the servo to reach the position 
              } 
      }
      
                
          if (switchState == HIGH && switchcount == 3)
      {
           switchcount++;
            for(pos = 0; pos < 50; pos += val)  // goes from 0 degrees to 50 degrees 
              {  
                val = map(val, 0, 1023, 1, 25);
                myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                delay(50);                       // time for the servo to reach the position 
              }
 
            for(pos = 50; pos>=1; pos -= val)     // goes from 50 degrees to 0 degrees 
              {  
                val = map(val, 0, 1023, 1, 25);  
                myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                delay(50);                       // waits 15ms for the servo to reach the position 
              } 
      }
            if(switchState != lastswitchState)
            {
              if(switchState == 1)
              {
                switchcount = switchcount +1;
              }
            }
      lastswitchState = switchState;
      
      delay(200);
}

I suggest you break your code into at least 2 separate functions so that loop() looks like this

void loop() {
   readSwitch();
   moveServo();
}

That will allow you to concentrate your mind (and ours) on two separate problems so that you can see whether the problem is in the switch code or the servo code.

The code in readSwitch() should update the variables switchCount and switchState and the code in moveServo() should do whatever needs to be done with the servos depending on the values of switchCount and switchState.

...R