#include <Servo.h>
#define LED1 2
#define LED2 3
#define BUTTON1 4
#define BUTTON2 5
int val1=0;
int val2=0;
Servo myServo; // Create a servo object
int rotationCount = 0;
#include <Buzzer.h>
#define buzzerPin 8 // Pin connected to the buzzer
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
myServo.attach(9); // Attach the servo to pin 9
Serial.begin(115200);
}
void buzzing(int freq,int dura){
tone(buzzerPin,freq,dura); // Generate a 500 Hz tone
delay(1000); // Wait for 1 second
}
void rotate(){
val1 = digitalRead(BUTTON1);
do {
val2 = digitalRead(BUTTON2);
//Serial.println(rotationCount);
//rotationCount++;
if((val1 == HIGH) && (val2 == LOW)){
digitalWrite(LED1, val1);
digitalWrite(LED2, val2);
buzzing(1000,500);
//if (rotationCount >= 1) {
// Return the servo to the center position
myServo.write(180); // 180 degrees (center position)
delay(1000); // Wait for 1 second
// Rotate the servo to the right (clockwise) position
myServo.write(0); // 0 degrees (rightmost position)
delay(1000); // Wait for 1 second
//}
}
}
while ((val1 == HIGH) && (val2 == LOW));//คำสั่งนี้เป็นจริงทำloopdoไปเรื่อยๆ ถ้าเช็คเเล้วว่าไม่เป็นจริงลงมาทำบรรทัดที51
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
//Rotate the servo to the right (clockwise) position
myServo.write(90); // 0 degrees (rightmost position)
delay(1000); // Wait for 1 second
}
void loop() {
rotate();
}