Make push button as input the servo motor

Hello,

Can anyone help me to modified this code?
This code will make a servo motor move continuously from 80 degrees to 35 degrees.
So how can I make a push button as an input to move the servo motor?
There are two push button sw 1 and sw 2. When I push the sw 1, the servo motor will move 2x only. That means the servo motor can go from
80 degrees -->35 degrees --> 80 degrees --35 degrees -->80 degrees.
Then, when I push sw 2, the servo motor will move 10x.
Is this possible to make?
Hope can help me.
Thank for advance :slight_smile:

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
 
  myservo.write(80);                  // sets the servo position according to the scaled value 
  delay(200);                         // waits for the servo to get there 
  myservo.write(35);                  // sets the servo position according to the scaled value 
  delay(200);                          // waits for the servo to get there 
}

There are two push button sw 1 and sw 2.

No, there aren't. I looked at your code.

Reading the state of a pin that a properly wired switch is attached to is trivial.

Using an if statement hardly needs more than a few functioning brain cells.

You try something. If it doesn't work, post the code, explain what it actually does, and how that differs from what you want.

Use the buttons to set a variable - call it buttonState. Button 1 sets it to 1 and button 2 sets it to 2.

Then modify your servo code so it does A if buttonState == 1 and B if buttonState == 2.

Have a look at planning and implementing a program

...R

Thank a lot for pauls and robin2.

I got this code:

#include <Servo.h>
Servo servoMain;
int led = 13;
long int x = 0;

void setup() {         
    Serial.begin(9600);
  pinMode(led, OUTPUT);
   servoMain.attach(5); // servo on digital pin 5
}

void loop()

{servoMain.write(120);
   delay(10);
   while (Serial.available() > 0) {
    int x = Serial.parseInt();
   while (x>0) {
   servoMain.write(40);  // Turn Servo Left to 45 degrees
   delay(170);          // Wait 1 second
   servoMain.write(120);   // Turn Servo Left to 0 degrees
   delay(170);          // Wait 1 second
x--;  // wait for a second
}
}
}

Using serial input. just put the number that we want and the servo will move.

How can I control my servo using this code?

120
121
1
120
121
3
120
20
121

code above is a result from:

void setup()
{
 Serial.begin(9600);
}

void loop()
{
 if( Serial.available() > 0)
 {    
   Serial.println(Serial.read()); // prints the result to Serial Monitor  
 }
}

I insert 1 MYR,5 MYR AND 10 MYR. I set to my validator to accept 1 MYR and 5 MYR only. The others note will rejected from validator.

code for 1 MYR = 1
5 MYR = 3
Validator Busy = 120
Validator Not Busy = 121
Note Not Recognised = 20

You can see the code from the manual with SIO interface. Page 20 until 22
here the link: https:

//www.google.com.my/url?sa=t&rct=j&q=&esrc=s&source=web&cd=11&cad=rja&uact=8&ved=0CEwQFjAK&url=http%3A%2F%2Fwww.vendapin.com%2FAdobe%2520files%2FBV20%2520Manual%2520Issue-1.pdf&ei=d5YoVerILcvguQTDvIDADA&usg=AFQjCNESXeKCDpxSEK4x9spFvMCtBdNXnQ&sig2=rVa3xqlr9XTRIn1oWUhccg

How to make if I insert 1MYR the servo will move 2 times and I insert 5MYR the servo will move 10 times?

I don't know how to use serial as input.
I hope someone can give a little bit idea and help me.
Thanks

mohdfaiz:
I don't know how to use serial as input.

Have a look at the examples in serial input basics. The second or third example combined with the parse example should do what you want.

...R

mohdfaiz:

void loop() 

{

myservo.write(80);                  // sets the servo position according to the scaled value
  delay(200);                        // waits for the servo to get there
  myservo.write(35);                  // sets the servo position according to the scaled value
  delay(200);                          // waits for the servo to get there
}

Ok, the code in the loop moves the servo 1x. Let's put that out into a function.

void move_servo_1_time() {
 
  myservo.write(80);                  // sets the servo position according to the scaled value 
  delay(200);                         // waits for the servo to get there 
  myservo.write(35);                  // sets the servo position according to the scaled value 
  delay(200);                          // waits for the servo to get there 
}

Now lets add a function to move servo multiple times

void move_servo_n_times(int n) {
  for(int i = 0; i< n; i++) {
    move_servo_1_time();
  }
}

Then you need to add code in your loop to read the push buttons. When push button 2 is pressed, you call the function with an appropriate value:

  if(push_button_2_is_pressed) {
    move_servo_n_times(10);
  }

So all you have to write now is some code to sense the push button.

If you don't know how to do that, then you need to work your way through the examples and make an effort to understand them.

Open the arduino IDE, and select "File>Examples>02.Digital>Button". If you don't know how to detect a button push, then forget about the servo until you have worked through that example.

Hello, thank a lot guys.

this code have a error

#include <Servo.h>
Servo servoMain;
const int buttonPin1 = 2;   
const int buttonPin2 = 3; 
const int ledPin =  13;      


int buttonState1 = 0;       
int buttonState2 = 0;    
void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin1, INPUT);  
  pinMode(buttonPin2, INPUT);  
}

void loop(){
 
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
}

void move_servo_1_time() {
 
  servoMain.write(80);                  // sets the servo position according to the scaled value 
  delay(200);                         // waits for the servo to get there 
  servoMain.write(35);                  // sets the servo position according to the scaled value 
  delay(200);                          // waits for the servo to get there 
}
void move_servo_n_times(int n) {
  for(int i = 0; i< n; i++) {
    move_servo_1_time();
  }
}

  if (buttonState1 == HIGH) {     
      move_servo_n_times(2);
  } 
 if else (buttonState2 == HIGH) {     
       move_servo_n_times(10); 
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }

the error show this

Button:37: error: expected unqualified-id before 'if'
Button:40: error: expected unqualified-id before 'if'
Button:43: error: expected unqualified-id before 'else'