#include <Servo.h> #define pinForward 8 #define pinBack 7 #define pinSpeedForwardBack 6 #define pinFrontLights 2 #define pinBackLights 3 #define pinFrontSteering 10 // Pin definitions for L293D motor driver #define ENA 6 // Enable pin for motor A (connected to pin 6 for speed control) #define IN1 8 // Input 1 for motor A (connected to pin 8 for direction control) #define IN2 7 // Input 2 for motor A (connected to pin 7 for direction control) // Pin definitions for turn indicator LEDs #define pinLeftTurnLED 4 #define pinRightTurnLED 5 // Pin definition for Bluetooth status LED #define pinBluetoothStatusLED 9 Servo leftRight; byte commands[4] = {0x00, 0x00, 0x00, 0x00}; byte prevCommands[4] = {0x01, 0x01, 0x01, 0x01}; unsigned long timer0 = 2000; // Stores the time (in millis since execution started) unsigned long timer1 = 0; // Stores the time when the last sensor reading was sent to the phone unsigned long timer2 = 0; // Stores the time when the last command was received from the phone unsigned long leftBlinkTimer = 0; unsigned long rightBlinkTimer = 0; unsigned long blinkInterval = 500; // Interval at which to blink the LEDs (in milliseconds) bool leftLEDState = LOW; bool rightLEDState = LOW; float stepSize = 9.04; void setup() { Serial.begin(115200); pinMode(pinFrontLights, OUTPUT); pinMode(pinBackLights, OUTPUT); pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(pinLeftTurnLED, OUTPUT); pinMode(pinRightTurnLED, OUTPUT); pinMode(pinBluetoothStatusLED, OUTPUT); leftRight.attach(pinFrontSteering); } void loop() { unsigned long currentMillis = millis(); if (Serial.available() == 4) { timer2 = currentMillis; // Store the time when the last command was received memcpy(prevCommands, commands, 4); // Storing the received commands commands[0] = Serial.read(); // Direction commands[1] = Serial.read(); // Speed commands[2] = Serial.read(); // Angle commands[3] = Serial.read(); // Lights and buttons states digitalWrite(pinBluetoothStatusLED, HIGH); // Turn on Bluetooth status LED when command is received if ((commands[2] <= 0xb4) && ((commands[0] <= 0xf5) && (commands[0] >= 0xf1))) { if (commands[0] <= 0xf3) { if (commands[0] == 0xf1) { // Check if the move forward command was received if (prevCommands[0] != 0xf1) { // Change pin state to move forward only if previous state was not move forward moveForward(commands[1]); } } else if (commands[0] == 0xf2) { // Check if the move back command was received if (prevCommands[0] != 0xf2) { // Change pin state to move back only if previous state was not move back moveBackward(commands[1]); } } else { // Check if the stop command was received if (prevCommands[0] != 0xf3) { // Change pin state to stop only if previous state was not stop stopMotor(); } } if (prevCommands[1] != commands[1]) { setMotorSpeed(commands[1]); // Change speed only if new speed is not equal to the previous speed } if (prevCommands[2] != commands[2]) { leftRight.write(commands[2]); // Steer front wheels only if the new angle is not equal to the previous angle // Check for left or right turn if (commands[2] < 90) { // Assuming <90 is a left turn leftBlinkTimer = currentMillis; } else if (commands[2] > 90) { // Assuming >90 is a right turn rightBlinkTimer = currentMillis; } } } else if (commands[0] == 0xf5) { if (prevCommands[0] != 0xf5) { stopMotor(); digitalWrite(pinFrontLights, LOW); digitalWrite(pinBackLights, LOW); } } // Check the front/back lights and other toggles if (prevCommands[3] != commands[3]) { // Change the light/button states if ((bitRead(prevCommands[3], 7)) != (bitRead(commands[3], 7))) { digitalWrite(pinFrontLights, bitRead(commands[3], 7)); } if ((bitRead(prevCommands[3], 6)) != (bitRead(commands[3], 6))) { digitalWrite(pinBackLights, bitRead(commands[3], 6)); } if ((bitRead(prevCommands[3], 5)) != (bitRead(commands[3], 5))) { digitalWrite(pinFrontLights, bitRead(commands[3], 5)); } } } else { Serial.begin(115200); } } else { timer0 = currentMillis; if ((timer0 - timer2) > 400) { stopMotor(); digitalWrite(pinFrontLights, LOW); digitalWrite(pinBackLights, LOW); digitalWrite(pinBluetoothStatusLED, LOW); // Turn off Bluetooth status LED if no command received for 400ms } } // Handle LED blinking for left turn if (currentMillis - leftBlinkTimer < 1000) { // LED blinks for 1 second after turning left if (currentMillis - leftBlinkTimer >= blinkInterval) { leftLEDState = !leftLEDState; // Toggle LED state digitalWrite(pinLeftTurnLED, leftLEDState); leftBlinkTimer = currentMillis; } } else { digitalWrite(pinLeftTurnLED, LOW); // Turn off the LED after 1 second } // Handle LED blinking for right turn if (currentMillis - rightBlinkTimer < 1000) { // LED blinks for 1 second after turning right if (currentMillis - rightBlinkTimer >= blinkInterval) { rightLEDState = !rightLEDState; // Toggle LED state digitalWrite(pinRightTurnLED, rightLEDState); rightBlinkTimer = currentMillis; } } else { digitalWrite(pinRightTurnLED, LOW); // Turn off the LED after 1 second } } void moveForward(int speed) { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(ENA, speed); } void moveBackward(int speed) { digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(ENA, speed); } void stopMotor() { digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); analogWrite(ENA, 0); } void setMotorSpeed(int speed) { analogWrite(ENA, speed); }
Standard input is empty
#include <Servo.h>
#define pinForward 8
#define pinBack 7
#define pinSpeedForwardBack 6
#define pinFrontLights 2
#define pinBackLights 3
#define pinFrontSteering 10
// Pin definitions for L293D motor driver
#define ENA 6 // Enable pin for motor A (connected to pin 6 for speed control)
#define IN1 8 // Input 1 for motor A (connected to pin 8 for direction control)
#define IN2 7 // Input 2 for motor A (connected to pin 7 for direction control)
// Pin definitions for turn indicator LEDs
#define pinLeftTurnLED 4
#define pinRightTurnLED 5
// Pin definition for Bluetooth status LED
#define pinBluetoothStatusLED 9
Servo leftRight;
byte commands[4] = {0x00, 0x00, 0x00, 0x00};
byte prevCommands[4] = {0x01, 0x01, 0x01, 0x01};
unsigned long timer0 = 2000; // Stores the time (in millis since execution started)
unsigned long timer1 = 0; // Stores the time when the last sensor reading was sent to the phone
unsigned long timer2 = 0; // Stores the time when the last command was received from the phone
unsigned long leftBlinkTimer = 0;
unsigned long rightBlinkTimer = 0;
unsigned long blinkInterval = 500; // Interval at which to blink the LEDs (in milliseconds)
bool leftLEDState = LOW;
bool rightLEDState = LOW;
float stepSize = 9.04;
void setup() {
Serial.begin(115200);
pinMode(pinFrontLights, OUTPUT);
pinMode(pinBackLights, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(pinLeftTurnLED, OUTPUT);
pinMode(pinRightTurnLED, OUTPUT);
pinMode(pinBluetoothStatusLED, OUTPUT);
leftRight.attach(pinFrontSteering);
}
void loop() {
unsigned long currentMillis = millis();
if (Serial.available() == 4) {
timer2 = currentMillis; // Store the time when the last command was received
memcpy(prevCommands, commands, 4); // Storing the received commands
commands[0] = Serial.read(); // Direction
commands[1] = Serial.read(); // Speed
commands[2] = Serial.read(); // Angle
commands[3] = Serial.read(); // Lights and buttons states
digitalWrite(pinBluetoothStatusLED, HIGH); // Turn on Bluetooth status LED when command is received
if ((commands[2] <= 0xb4) && ((commands[0] <= 0xf5) && (commands[0] >= 0xf1))) {
if (commands[0] <= 0xf3) {
if (commands[0] == 0xf1) { // Check if the move forward command was received
if (prevCommands[0] != 0xf1) { // Change pin state to move forward only if previous state was not move forward
moveForward(commands[1]);
}
} else if (commands[0] == 0xf2) { // Check if the move back command was received
if (prevCommands[0] != 0xf2) { // Change pin state to move back only if previous state was not move back
moveBackward(commands[1]);
}
} else { // Check if the stop command was received
if (prevCommands[0] != 0xf3) { // Change pin state to stop only if previous state was not stop
stopMotor();
}
}
if (prevCommands[1] != commands[1]) {
setMotorSpeed(commands[1]); // Change speed only if new speed is not equal to the previous speed
}
if (prevCommands[2] != commands[2]) {
leftRight.write(commands[2]); // Steer front wheels only if the new angle is not equal to the previous angle
// Check for left or right turn
if (commands[2] < 90) { // Assuming <90 is a left turn
leftBlinkTimer = currentMillis;
} else if (commands[2] > 90) { // Assuming >90 is a right turn
rightBlinkTimer = currentMillis;
}
}
} else if (commands[0] == 0xf5) {
if (prevCommands[0] != 0xf5) {
stopMotor();
digitalWrite(pinFrontLights, LOW);
digitalWrite(pinBackLights, LOW);
}
}
// Check the front/back lights and other toggles
if (prevCommands[3] != commands[3]) {
// Change the light/button states
if ((bitRead(prevCommands[3], 7)) != (bitRead(commands[3], 7))) {
digitalWrite(pinFrontLights, bitRead(commands[3], 7));
}
if ((bitRead(prevCommands[3], 6)) != (bitRead(commands[3], 6))) {
digitalWrite(pinBackLights, bitRead(commands[3], 6));
}
if ((bitRead(prevCommands[3], 5)) != (bitRead(commands[3], 5))) {
digitalWrite(pinFrontLights, bitRead(commands[3], 5));
}
}
} else {
Serial.end();
Serial.begin(115200);
}
} else {
timer0 = currentMillis;
if ((timer0 - timer2) > 400) {
stopMotor();
digitalWrite(pinFrontLights, LOW);
digitalWrite(pinBackLights, LOW);
digitalWrite(pinBluetoothStatusLED, LOW); // Turn off Bluetooth status LED if no command received for 400ms
}
}
// Handle LED blinking for left turn
if (currentMillis - leftBlinkTimer < 1000) { // LED blinks for 1 second after turning left
if (currentMillis - leftBlinkTimer >= blinkInterval) {
leftLEDState = !leftLEDState; // Toggle LED state
digitalWrite(pinLeftTurnLED, leftLEDState);
leftBlinkTimer = currentMillis;
}
} else {
digitalWrite(pinLeftTurnLED, LOW); // Turn off the LED after 1 second
}
// Handle LED blinking for right turn
if (currentMillis - rightBlinkTimer < 1000) { // LED blinks for 1 second after turning right
if (currentMillis - rightBlinkTimer >= blinkInterval) {
rightLEDState = !rightLEDState; // Toggle LED state
digitalWrite(pinRightTurnLED, rightLEDState);
rightBlinkTimer = currentMillis;
}
} else {
digitalWrite(pinRightTurnLED, LOW); // Turn off the LED after 1 second
}
}
void moveForward(int speed) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, speed);
}
void moveBackward(int speed) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, speed);
}
void stopMotor() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0);
}
void setMotorSpeed(int speed) {
analogWrite(ENA, speed);
}