// wokwi resluts: ok
// ARDUINO TEST: ok
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int stepV=0;
int delayV=0;
int choice;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600); //اعداد سرعة الاتصال مع شاشة العرض
Serial.setTimeout(5000);
Serial.println("enter your choice:");
Serial.println("choice 0: testing position values");
Serial.println("choice 1: testing sweep function");
while (Serial.available() == 0) {
}
choice = Serial.parseInt(); // read INTEGER values
Serial.print("your choice is: ");
Serial.println(choice);
switch (choice)
{
case 0:
Serial.print("inter position value: ");
while (Serial.available() == 0) {
}
pos = Serial.parseInt(); // read INTEGER values
Serial.println (pos); // print the inserted value
break;
case 1:
Serial.print("enter the step value: ");
while (Serial.available() == 0) {
}
stepV = Serial.parseInt(); // read INTEGER values
Serial.println (stepV); // print the inserted value
Serial.print("enter the delay value: ");
while (Serial.available() == 0) {
}
delayV = Serial.parseInt(); // read INTEGER values
Serial.println (delayV); // print the inserted value
break;
default: Serial.println("Invalid choice!");
}
}
void loop() {
if (choice==0){
myservo.write(pos); // tell servo to go to position in variable 'pos'
} else if (choice==1){
for (pos = 0; pos <= 180; pos += stepV) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(delayV); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= stepV) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(delayV); // waits 15ms for the servo to reach the position
}
}
}