#include <Servo.h>
#include <LiquidCrystal_I2C.h>
Servo crossingBarServo;
int servoPin = 9;
const int lamp = 2;
// Parameters for the function
const float T = 5.0; // Time when the bar reaches near-vertical in seconds
const float A = 10.0; // Amplitude of the oscillation
const float b = 0.8; // Damping coefficient
const float c = 10.0; // Frequency of oscillation
unsigned long lastToggleTime = 0; // To keep track of the last toggle time of the LED
const int ledToggleInterval = 500; // Interval at which the LED should toggle (in milliseconds)
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
crossingBarServo.attach(9);
pinMode(lamp, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop() {
openCrossRoadBar(); // Call function to open the railroad crossing bar
digitalWrite(lamp, LOW);
delay(10000); // Wait 10 seconds before next operation (for demonstration purposes)
}
void openCrossRoadBar() {
unsigned long startTime = millis();
float t, angle;
bool ledState = LOW;
while(true) {
t = (millis() - startTime) / 1000.0; // Current time in seconds since start
if (t < T) {
// Initial smooth rise
angle = 90 * sin(PI / 2 * t / T);
} else if (t >= T) {
// Damped oscillation after reaching near-vertical
angle = 90 - A * exp(-b * (t - T)) * abs(sin(c * (t - T)));
}
// Check if the movement should continue
if (t > T + 2) {
break; // End the function after a sufficient time has passed
}
// Update the servo position
crossingBarServo.write((int)angle);
delay(20); // Short delay for smoother motion
// Optional: output current angle to the display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(t, 2);
lcd.setCursor(0, 1);
lcd.print("Angle: ");
lcd.print(angle, 1);
if (millis() - lastToggleTime >= ledToggleInterval) {
lastToggleTime = millis(); // Update the last toggle time
ledState = !ledState; // Toggle the LED state
digitalWrite(lamp, ledState); // Apply the new LED state
}
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
servo1:GND
servo1:V+
servo1:PWM
led1:A
led1:C