#include <SoftwareSerial.h>
SoftwareSerial HC06(2,3);
int redLED = 6;
int yellowLED = 7;
int greenLED = 8;
void setup() {
Serial.begin(9600); // Start serial communication with the Serial Monitor
HC06.begin(9600);
// Initialize LED pins as output
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
// Turn off all LEDs at the start
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
Serial.println("Bluetooth Controlled Traffic Light Ready");
}
void loop() {
// Check if data is available from the Bluetooth module
if (BTSerial.available()) {
char command = BTSerial.read(); // Read the incoming command
Serial.print("Command received: ");
Serial.println(command);
// Control the traffic lights based on the command received
if (command == 'r') {
// Turn on Red LED, turn off others
digitalWrite(redLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
}
else if (command == 'y') {
// Turn on Yellow LED, turn off others
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(greenLED, LOW);
}
else if (command == 'g') {
// Turn on Green LED, turn off others
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, HIGH);
}
else if (command == '0') {
// Turn off all LEDs
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
}
}
}
// HC-06 Bluetooth Module Connection: TX = 2, RX =3, and 5 V / GND
// In Arduino IDE Serial Monitor, set 'No Line Ending'
// Enter 'AT' to get 'OK' response, then
// Enter 'AT+NAMEBT1234' to set HC-06 Module Name to 'BT1234'
// Enter 'AT+PIN1234' to set HC-06 Module PIN to '1234'
// Recommended Smartphoe App for connection is
// Arduino Bluetooth Control by broxcode