#include <ESP32Servo.h>
int servoPin = 27;
int button = 26;
int ledRed1 = 13;
int ledRed2 = 14;
int ledYellow = 12;
unsigned long previousTime;
unsigned long currentTime;
unsigned long currentTimeYellow;
unsigned long previousTimeYellow;
unsigned long servoTime;
bool slagboomUp = false;
bool buttonPush = false;
bool goingDown = false;
int degree = 90;
Servo myservo;
void setup()
{
myservo.attach(servoPin);
servoTime = currentTime;
pinMode(button, INPUT);
pinMode(ledRed1, OUTPUT);
pinMode(ledRed2, OUTPUT);
pinMode(ledYellow, OUTPUT);
}
//LEDS ---------------------------------------------------------------------------
void Redleds(){
currentTime = millis();
if(currentTime - previousTime <= 500)
{
digitalWrite(ledRed1, HIGH);
digitalWrite(ledRed2, LOW);
}
if(currentTime - previousTime > 500 && currentTime - previousTime <= 1000)
{
digitalWrite(ledRed1, LOW);
digitalWrite(ledRed2, HIGH);
}
if(currentTime - previousTime > 1000)
{
previousTime = currentTime;
}
}
//------------------------------------------------------------------------------------
//Servo with red leds and pause -------------------------------------------------------
// void ServoDown(){
// for (deg = 90; deg <= 180; deg++)
// {
// Redleds();
// myservo.write(deg);
// //delay(50);
// while(deg == 180 && millis() < 5000){
// Redleds();
// millis();
// }
// }
// }
// void ServoUp(){
// for (deg = 180; deg >= 90; deg--)
// {
// Redleds();
// myservo.write(deg);
// //elay(50);
// }
// }
//-------------------------------------------------------------------------------------
//Yellow led --------------------------------------------------------------------------
void Yellowled(){
currentTimeYellow = millis();
if (currentTimeYellow - previousTimeYellow <= 500){
digitalWrite(ledYellow, HIGH);
}
if(currentTimeYellow - previousTimeYellow > 500 && currentTimeYellow - previousTimeYellow <= 1000){
digitalWrite(ledYellow, LOW);
}
if(currentTimeYellow - previousTimeYellow > 1000){
previousTimeYellow = currentTimeYellow;
}
}
//----------------------------------------------------------------------------------------
// void pausewithleds(){
// currentPause = millis();
// if(currentPause - previousPause < 8000){
// redleds();
// }
// if(currentPause - previousPause > 8000){
// previousPause = currentPause;
// false;
// }
// }
// void loop()
// {
// //if button pushed
// if(digitalRead(button) == HIGH){
// //yellowled blinking all the time
// Yellowled();
// }
// else{
// //yellow led goes off
// digitalWrite(ledYellow, LOW);
// //servo kicks in
// //ServoDown();
// //place for pause if not done within servodown();
// //ServoUp();
// //after servo 2 red leds go off so that yello can blink again (solo)
// digitalWrite(ledRed1, LOW);
// digitalWrite(ledRed2, LOW);
// }
// }
void loop(){
if(slagboomUp == false && buttonPush==true) {
if(currentTime - servoTime > 15) {
degree++;
myservo.write(degree);
servoTime = currentTime;
if(degree==180) {
slagboomUp = true;
buttonPush = false;
}
}
}
if(slagboomUp == true && buttonPush==true) {
goingDown = true;
if(currentTime - servoTime > 15) {
degree--;
myservo.write(degree);
servoTime = currentTime;
if(degree==90) {
slagboomUp = false;
buttonPush = false;
goingDown = false;
}
}
}
if(digitalRead(button) == LOW){
buttonPush = true;
}
}