Hi there, I am currently working on a university assessment that involves a boomgate (the servo) operated by a PIR sensor along with a button that controls an led which represents a pedestrian crossing. upon button press if the gate is closed, the light will turn blue indicating that it is safe to cross, then the boom gate operation can resume. However, I am encountering a problem wherein the pedestrian crossing and blue lighting sequence can only occur during the initial startup of the code. Upon button press at any time after the boom gate is operated, the light never turns blue which I am assuming means the program is not getting to that stage of the void loop. Does anyone have any ideas on how to fix this? Also, if anyone could provide some insight on how to make the servo open gradually rather than instantly, while simultaneously making a light blink as the servo opens, that would be a great help. I can provide videos of the system if needed. Thanks
// C++ code
//
#include <Servo.h>
const int button = 8;
const int rgbRed = 5;
const int rgbBlue = 6;
const int rgbGreen = 11;
const int irSensor = 2;
const int ldrPin = 0;
int buttonState = 0;
int servo = 9;
const int moveDuration = 2000;
const int blinkInterval = 200;
Servo myservo;
unsigned long last_servo_time;
unsigned long servo_wait;
unsigned long previousTime;
unsigned long currentTime = 0;
unsigned long buttonTime = 0;
unsigned long startTime = millis();
unsigned long lastDetect = 0;
unsigned long buttonPressTime = 0;
int current_servo_angle = 90;
int delta_angle = 1;
int pos = 0;
String status1 = "Gate closed | Crossing not in use | Car not detected";
String status2 = "Gate open | Crossing not in use | Car detected";
String status3 = "Gate open | Pedestrians waiting | Car detected";
String status4 = "Gate closed | Crossing in use| Car not detected";
String status5 = "Gate closed | Crossing in use| Car detected";
bool buttonPressed = false;
bool crossingInUse = false;
bool carDetected = false;
bool gateOpen = false;
bool gateActive = false;
int irRead() {
int statusSensor = digitalRead(irSensor);
if (statusSensor == 1) {
return 1;
} else {
return 0;
}
}
int buttonRead() {
buttonState = digitalRead(button);
if (buttonState == HIGH) {
buttonTime = millis();
buttonPressed = true;
return 1;
} else {
return 0;
}
}
void setRgb(int r, int g, int b){
analogWrite(rgbRed, r);
analogWrite(rgbGreen, g);
analogWrite(rgbBlue, b);
}
void setup() {
// initial state - boom gate begins as closed, RGB is solid red, no one is at the crossing, no cars are at the boom gate.
myservo.attach(servo);
pinMode(button, INPUT);
pinMode(irSensor, INPUT);
pinMode(rgbRed, OUTPUT);
pinMode(rgbGreen, OUTPUT);
pinMode(rgbBlue, OUTPUT);
Serial.begin(9600);
int ldrValue = analogRead(ldrPin);
int brightness = map(ldrValue, 0, 1023, 0, 255) / 4;
buttonPressed = false;
crossingInUse = false;
carDetected = false;
startTime = millis();
setRgb(255, 0, 0);
myservo.write(0);
}
void gateUp() {
for (pos = 0; pos <= 90; pos += 1) { // in gate open sequence, needs to blink red at 5Hz then solid green.
myservo.write(pos);
if (pos = 90) {
gateOpen = true;
setRgb(0, 255, 0);
break;
}
}
}
void gateDown() {
for (pos = 90; pos >= 0; pos -= 1) {
myservo.write(pos);
if (pos = 0){
gateOpen = false;
break;
}
}
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - previousTime >= 2000) {
previousTime = currentTime;
if (crossingInUse == false && carDetected == false && gateOpen == false) {
Serial.println("crossing not in use, no cars, no pedestrians");
} else if (gateOpen == true && crossingInUse == false && carDetected == true) {
Serial.println("gate open, no pedestrians, car detected");
} else if (gateOpen == true && crossingInUse == false && carDetected == false) {
Serial.println("gate open, no pedestrians, waiting to close");
} else if (gateOpen == false && crossingInUse == true && carDetected == false) {
Serial.println("gate closed, pedestrians crossing, no car detected");
} else if (!gateOpen && crossingInUse && carDetected) {
Serial.println("gate closed, pedestrians crossing, car waiting to cross");
}
}
if (irRead() == 1) {
if (!carDetected) {
carDetected = true;
buttonPressTime = currentTime;
} else if (currentTime - buttonPressTime >= 2000 && !crossingInUse) {
gateUp();
setRgb(255, 0, 0);
crossingInUse = true;
lastDetect = currentTime;
}
} else {
if (carDetected && currentTime - lastDetect >= 2000) {
gateDown();
setRgb(0, 255, 0);
carDetected = false;
crossingInUse = false;
lastDetect = currentTime;
previousTime = currentTime;
}
}
if (buttonRead() == 1 && gateOpen == false && !crossingInUse) {
if (buttonPressTime == 0) {
buttonPressTime = currentTime; // Record the initial press time
crossingInUse = true;
setRgb(0, 0, 255);
previousTime = currentTime;
}
}
if (crossingInUse && currentTime - buttonPressTime >= 5000) {
crossingInUse = false;
buttonPressTime = 0; // Reset the buttonPressTime
setRgb(255, 0, 0);
}
}