/*
ROBOTRONIX.CO.IL
Arduino UNO - UART Electric Gate Controller
UART: 115200 Baud
Commands: OPEN, CLOSE, STATUS
Servo: D9
Green LED: D7
Red LED: D8
Gate Closed: 0 degrees
Gate Open: 90 degrees
Open warning: 2 seconds
Close warning: 23 seconds
*/
#include <Servo.h>
const byte SERVO_PIN = 9;
const byte GREEN_LED = 7;
const byte RED_LED = 8;
const unsigned long OPEN_WAIT = 2000;
const unsigned long CLOSE_WAIT = 23000;
const unsigned long STEP_TIME = 20;
const unsigned long BLINK_TIME = 250;
Servo gateServo;
enum GateState {
CLOSED,
WAIT_OPEN,
OPENING,
OPEN,
WAIT_CLOSE,
CLOSING
};
GateState gateState = CLOSED;
int angle = 0;
unsigned long stateTime = 0;
unsigned long lastStep = 0;
char rxBuffer[24];
byte rxIndex = 0;
// Print current gate status over UART
void printStatus() {
switch (gateState) {
case CLOSED:
Serial.println("GATE CLOSED");
break;
case WAIT_OPEN:
Serial.println("WAITING TO OPEN");
break;
case OPENING:
Serial.println("GATE OPENING");
break;
case OPEN:
Serial.println("GATE OPEN");
break;
case WAIT_CLOSE:
Serial.println("WAITING TO CLOSE");
break;
case CLOSING:
Serial.println("GATE CLOSING");
break;
}
}
// Process incoming UART commands
void processCommand(const char* cmd) {
if (strcmp(cmd, "OPEN") == 0) {
if (gateState == OPEN ||
gateState == OPENING ||
gateState == WAIT_OPEN) {
printStatus();
} else {
gateState = WAIT_OPEN;
stateTime = millis();
Serial.println("OPEN ACCEPTED - WAIT 2 SEC");
}
} else if (strcmp(cmd, "CLOSE") == 0) {
if (gateState == CLOSED ||
gateState == CLOSING ||
gateState == WAIT_CLOSE) {
printStatus();
} else {
gateState = WAIT_CLOSE;
stateTime = millis();
Serial.println("CLOSE ACCEPTED - WAIT 23 SEC");
}
} else if (strcmp(cmd, "STATUS") == 0) {
printStatus();
} else {
Serial.println("ERROR: UNKNOWN COMMAND");
}
}
void setup() {
Serial.begin(115200);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
gateServo.attach(SERVO_PIN);
gateServo.write(0);
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, HIGH);
Serial.println("ROBOTRONIX.CO.IL");
Serial.println("UART GATE CONTROLLER READY");
Serial.println("BAUD RATE: 115200");
Serial.println("COMMANDS: OPEN, CLOSE, STATUS");
printStatus();
}
void loop() {
unsigned long now = millis();
// Read UART without blocking gate movement
while (Serial.available() > 0) {
char c = Serial.read();
if (c == '\n' || c == '\r') {
if (rxIndex > 0) {
rxBuffer[rxIndex] = '\0';
processCommand(rxBuffer);
rxIndex = 0;
}
} else if (c != ' ' && c != '\t') {
// Convert lowercase to uppercase
if (c >= 'a' && c <= 'z') {
c -= 32;
}
if (rxIndex < sizeof(rxBuffer) - 1) {
rxBuffer[rxIndex++] = c;
} else {
rxIndex = 0;
Serial.println("ERROR: COMMAND TOO LONG");
}
}
}
now = millis();
// Wait 2 seconds before opening
if (gateState == WAIT_OPEN &&
now - stateTime >= OPEN_WAIT) {
gateState = OPENING;
lastStep = now;
Serial.println("GATE OPENING");
}
// Wait 23 seconds before closing
if (gateState == WAIT_CLOSE &&
now - stateTime >= CLOSE_WAIT) {
gateState = CLOSING;
lastStep = now;
Serial.println("GATE CLOSING");
}
// Open gate gradually
if (gateState == OPENING &&
now - lastStep >= STEP_TIME) {
lastStep = now;
if (angle < 90) {
angle++;
gateServo.write(angle);
}
if (angle >= 90) {
gateState = OPEN;
Serial.println("GATE OPEN");
}
}
// Close gate gradually
if (gateState == CLOSING &&
now - lastStep >= STEP_TIME) {
lastStep = now;
if (angle > 0) {
angle--;
gateServo.write(angle);
}
if (angle <= 0) {
gateState = CLOSED;
Serial.println("GATE CLOSED");
}
}
// LED status indication
if (gateState == OPEN) {
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
} else {
digitalWrite(GREEN_LED, LOW);
// Red LED blinking in every other state
bool blinkState = (now / BLINK_TIME) % 2 == 0;
digitalWrite(RED_LED, blinkState);
}
}