fork download
  1. #include <Servo.h>
  2.  
  3. #define pinForward 8
  4. #define pinBack 7
  5. #define pinSpeedForwardBack 6
  6. #define pinFrontLights 2
  7. #define pinBackLights 3
  8. #define pinFrontSteering 10
  9.  
  10. // Pin definitions for L293D motor driver
  11. #define ENA 6 // Enable pin for motor A (connected to pin 6 for speed control)
  12. #define IN1 8 // Input 1 for motor A (connected to pin 8 for direction control)
  13. #define IN2 7 // Input 2 for motor A (connected to pin 7 for direction control)
  14.  
  15. // Pin definitions for turn indicator LEDs
  16. #define pinLeftTurnLED 4
  17. #define pinRightTurnLED 5
  18.  
  19. // Pin definition for Bluetooth status LED
  20. #define pinBluetoothStatusLED 9
  21.  
  22. Servo leftRight;
  23. byte commands[4] = {0x00, 0x00, 0x00, 0x00};
  24. byte prevCommands[4] = {0x01, 0x01, 0x01, 0x01};
  25.  
  26. unsigned long timer0 = 2000; // Stores the time (in millis since execution started)
  27. unsigned long timer1 = 0; // Stores the time when the last sensor reading was sent to the phone
  28. unsigned long timer2 = 0; // Stores the time when the last command was received from the phone
  29. unsigned long leftBlinkTimer = 0;
  30. unsigned long rightBlinkTimer = 0;
  31. unsigned long blinkInterval = 500; // Interval at which to blink the LEDs (in milliseconds)
  32. bool leftLEDState = LOW;
  33. bool rightLEDState = LOW;
  34.  
  35. float stepSize = 9.04;
  36.  
  37. void setup() {
  38. Serial.begin(115200);
  39. pinMode(pinFrontLights, OUTPUT);
  40. pinMode(pinBackLights, OUTPUT);
  41. pinMode(ENA, OUTPUT);
  42. pinMode(IN1, OUTPUT);
  43. pinMode(IN2, OUTPUT);
  44. pinMode(pinLeftTurnLED, OUTPUT);
  45. pinMode(pinRightTurnLED, OUTPUT);
  46. pinMode(pinBluetoothStatusLED, OUTPUT);
  47. leftRight.attach(pinFrontSteering);
  48. }
  49.  
  50. void loop() {
  51. unsigned long currentMillis = millis();
  52.  
  53. if (Serial.available() == 4) {
  54. timer2 = currentMillis; // Store the time when the last command was received
  55. memcpy(prevCommands, commands, 4); // Storing the received commands
  56. commands[0] = Serial.read(); // Direction
  57. commands[1] = Serial.read(); // Speed
  58. commands[2] = Serial.read(); // Angle
  59. commands[3] = Serial.read(); // Lights and buttons states
  60.  
  61. digitalWrite(pinBluetoothStatusLED, HIGH); // Turn on Bluetooth status LED when command is received
  62.  
  63. if ((commands[2] <= 0xb4) && ((commands[0] <= 0xf5) && (commands[0] >= 0xf1))) {
  64. if (commands[0] <= 0xf3) {
  65. if (commands[0] == 0xf1) { // Check if the move forward command was received
  66. if (prevCommands[0] != 0xf1) { // Change pin state to move forward only if previous state was not move forward
  67. moveForward(commands[1]);
  68. }
  69. } else if (commands[0] == 0xf2) { // Check if the move back command was received
  70. if (prevCommands[0] != 0xf2) { // Change pin state to move back only if previous state was not move back
  71. moveBackward(commands[1]);
  72. }
  73. } else { // Check if the stop command was received
  74. if (prevCommands[0] != 0xf3) { // Change pin state to stop only if previous state was not stop
  75. stopMotor();
  76. }
  77. }
  78. if (prevCommands[1] != commands[1]) {
  79. setMotorSpeed(commands[1]); // Change speed only if new speed is not equal to the previous speed
  80. }
  81. if (prevCommands[2] != commands[2]) {
  82. leftRight.write(commands[2]); // Steer front wheels only if the new angle is not equal to the previous angle
  83.  
  84. // Check for left or right turn
  85. if (commands[2] < 90) { // Assuming <90 is a left turn
  86. leftBlinkTimer = currentMillis;
  87. } else if (commands[2] > 90) { // Assuming >90 is a right turn
  88. rightBlinkTimer = currentMillis;
  89. }
  90. }
  91. } else if (commands[0] == 0xf5) {
  92. if (prevCommands[0] != 0xf5) {
  93. stopMotor();
  94. digitalWrite(pinFrontLights, LOW);
  95. digitalWrite(pinBackLights, LOW);
  96. }
  97. }
  98. // Check the front/back lights and other toggles
  99. if (prevCommands[3] != commands[3]) {
  100. // Change the light/button states
  101. if ((bitRead(prevCommands[3], 7)) != (bitRead(commands[3], 7))) {
  102. digitalWrite(pinFrontLights, bitRead(commands[3], 7));
  103. }
  104. if ((bitRead(prevCommands[3], 6)) != (bitRead(commands[3], 6))) {
  105. digitalWrite(pinBackLights, bitRead(commands[3], 6));
  106. }
  107. if ((bitRead(prevCommands[3], 5)) != (bitRead(commands[3], 5))) {
  108. digitalWrite(pinFrontLights, bitRead(commands[3], 5));
  109. }
  110. }
  111. } else {
  112. Serial.end();
  113. Serial.begin(115200);
  114. }
  115. } else {
  116. timer0 = currentMillis;
  117. if ((timer0 - timer2) > 400) {
  118. stopMotor();
  119. digitalWrite(pinFrontLights, LOW);
  120. digitalWrite(pinBackLights, LOW);
  121. digitalWrite(pinBluetoothStatusLED, LOW); // Turn off Bluetooth status LED if no command received for 400ms
  122. }
  123. }
  124.  
  125. // Handle LED blinking for left turn
  126. if (currentMillis - leftBlinkTimer < 1000) { // LED blinks for 1 second after turning left
  127. if (currentMillis - leftBlinkTimer >= blinkInterval) {
  128. leftLEDState = !leftLEDState; // Toggle LED state
  129. digitalWrite(pinLeftTurnLED, leftLEDState);
  130. leftBlinkTimer = currentMillis;
  131. }
  132. } else {
  133. digitalWrite(pinLeftTurnLED, LOW); // Turn off the LED after 1 second
  134. }
  135.  
  136. // Handle LED blinking for right turn
  137. if (currentMillis - rightBlinkTimer < 1000) { // LED blinks for 1 second after turning right
  138. if (currentMillis - rightBlinkTimer >= blinkInterval) {
  139. rightLEDState = !rightLEDState; // Toggle LED state
  140. digitalWrite(pinRightTurnLED, rightLEDState);
  141. rightBlinkTimer = currentMillis;
  142. }
  143. } else {
  144. digitalWrite(pinRightTurnLED, LOW); // Turn off the LED after 1 second
  145. }
  146. }
  147.  
  148. void moveForward(int speed) {
  149. digitalWrite(IN1, HIGH);
  150. digitalWrite(IN2, LOW);
  151. analogWrite(ENA, speed);
  152. }
  153.  
  154. void moveBackward(int speed) {
  155. digitalWrite(IN1, LOW);
  156. digitalWrite(IN2, HIGH);
  157. analogWrite(ENA, speed);
  158. }
  159.  
  160. void stopMotor() {
  161. digitalWrite(IN1, LOW);
  162. digitalWrite(IN2, LOW);
  163. analogWrite(ENA, 0);
  164. }
  165.  
  166. void setMotorSpeed(int speed) {
  167. analogWrite(ENA, speed);
  168. }
Success #stdin #stdout 0.02s 26100KB
stdin
Standard input is empty
stdout
#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);
}