Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define INT_A 2 // control the left motor direction pin D2
- #define INT_B 4 // control the right motor direction pin D4
- #define left_A 9 // define the left motor speed control pin as D9
- #define right_B 5 // define the right motor speed control pin as D5
- String str;
- int blueToothVal;
- void setup()
- {
- Serial.begin(9600);
- pinMode(INT_A, OUTPUT); // set the motor control pin as OUTPUT
- pinMode(INT_B, OUTPUT);
- pinMode(left_A, OUTPUT);
- pinMode(right_B, OUTPUT);
- }
- void loop() {
- bluetooth();
- }//end of loop
- void bluetooth()
- {
- while (Serial.available()){
- {
- str = Serial.readStringUntil('\n'); // str is the temporary variable for storing the last sring sent over bluetooth from Android device
- //Serial.print(str);
- }
- blueToothVal = (str.toInt()); // convert the string 'str' into an integer and assign it to blueToothVal
- switch (blueToothVal)
- {
- case 1:
- FRONT();
- delay(10);
- break;
- case 2:
- BACKWARDS();
- delay(10);
- break;
- case 3:
- LEFTT();
- delay(10);
- //STOP();
- break;
- case 4:
- RIGHTT();
- delay(10);
- //STOP();
- break;
- case 5:
- STOP();
- break;
- } // end of switch case
- } // end of while loop Serial1 read
- }
- // go forward
- void FRONT()
- {
- digitalWrite(INT_A, LOW); // control the left motor turn forward
- digitalWrite(INT_B, LOW); // control the right motor turn forward
- analogWrite(left_A, 200); // set the two motors’ speed(PWM=200)
- analogWrite(right_B, 200);
- }
- // backward
- void BACKWARDS()
- {
- digitalWrite(INT_A, HIGH); // control the left motor turn backward
- digitalWrite(INT_B, HIGH); // control the right motor turn backward
- analogWrite(left_A, 200); // set the two motors’ speed (PWM=200)
- analogWrite(right_B, 200);
- }
- // turn left
- void LEFTT()
- {
- digitalWrite(INT_A, HIGH); // control the left motor turn backward
- digitalWrite(INT_B, LOW); // control the right motor turn forward
- analogWrite(left_A, 100); // set the two motors’ speed(PWM为100)
- analogWrite(right_B, 100);
- }
- // turn right
- void RIGHTT()
- {
- digitalWrite(INT_A, LOW); // control the left motor turn forward
- digitalWrite(INT_B, HIGH); // control the right motor turn backward
- analogWrite(left_A, 100); // two motors’ speed(PWM为100)
- analogWrite(right_B, 100);
- }
- // stop
- void STOP()
- {
- digitalWrite(INT_A, LOW);
- digitalWrite(INT_B, LOW);
- analogWrite(left_A, 0); // both PWM are 0
- analogWrite(right_B, 0);
- }
Add Comment
Please, Sign In to add comment