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
#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
}
#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.
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:
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
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.
#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'