I'm a newbie here, i got an problem combine the coding. i got a servo motor with button coding and want to combine with IR obstacle sensor with buzzer coding. the problem is, before the button pressed, the IR sensor and buzzer are working. But when the button pressed the IR sensor and buzzer is not working.
help me with this.
#include <Servo.h>
Servo myservo;
#define servoPin 9
#define buttonPin1 11
#define buttonPin2 12
#define buz 7
#define obstaclePin 1
int hasObstacle = HIGH;
unsigned long interval_10, interval_12;
void button1();
void button2();
void setup() {
myservo.attach(servoPin);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buz, OUTPUT);
pinMode(obstaclePin, INPUT);
digitalWrite(buttonPin1, HIGH);
digitalWrite(buttonPin2, HIGH);
interval_10 = 6000;
interval_12 = 12000;
myservo.write(180);
myservo.detach();
}
void loop() {
if(digitalRead(buttonPin1) == LOW)
{
digitalWrite(buttonPin1, LOW);
button1();
delay(interval_10);
loop;
}
if(digitalRead(buttonPin2) == LOW)
{
digitalWrite(buttonPin2, LOW);
button2();
delay(interval_12);
loop;
}
hasObstacle = digitalRead(obstaclePin);
if (hasObstacle == HIGH)
{
digitalWrite(buz, HIGH);
}
if (hasObstacle == LOW)
{
digitalWrite(buz, LOW);
}
delay(200);
loop;
}
void button1()
{
myservo.attach(servoPin);
myservo.write(0);
delay(500);
myservo.write(180);
delay(500);
myservo.write(0);
delay(500);
myservo.write(180);
delay(500);
myservo.detach();
}
void button2()
{
myservo.attach(servoPin);
myservo.write(0);
delay(500);
myservo.write(180);
delay(500);
myservo.write(0);
delay(500);
myservo.write(180);
delay(500);
myservo.detach();
}