Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This program makes your robot move according to commands transmitted to it over bluetooth connection
- bluetooth module should be connected to pins 10,11 (RX, TX)
- reference bluetooth module:
- http://dx.com/p/jy-mcu-arduino-bluetooth-wireless-serial-port-module-104299#.UuBS5rSxVL5
- Controlling Android app: Bluetooth RC Controller, available at:
- https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller&hl=en
- */
- #include <SoftwareSerial.h>
- SoftwareSerial bluetooth(10, 11); // RX, TX
- void setup()
- {
- // Set the mode of the digital pins to outputs to drive the motors, LED and horn
- pinMode( 3, OUTPUT ); // motor
- pinMode( 5, OUTPUT ); // motor
- pinMode( 8, OUTPUT ); // speaker
- pinMode( 9, OUTPUT ); // LED
- Serial.begin(9600);
- bluetooth.begin(9600);
- //setup_bt();
- }
- int current_speed = 0;
- void loop()
- {
- if (bluetooth.available()) {
- char cmd = bluetooth.read();
- switch(cmd) {
- case '0':
- Serial.println("Speed: 0");
- current_speed = 0;
- break;
- case '1':
- Serial.println("Speed: 1");
- current_speed = 1;
- break;
- case '2':
- Serial.println("Speed: 2");
- current_speed = 2;
- break;
- case '3':
- Serial.println("Speed: 3");
- current_speed = 3;
- break;
- case '4':
- Serial.println("Speed: 4");
- current_speed = 4;
- break;
- case '5':
- Serial.println("Speed: 5");
- current_speed = 5;
- break;
- case '6':
- Serial.println("Speed: 6");
- current_speed = 6;
- break;
- case '7':
- Serial.println("Speed: 7");
- current_speed = 7;
- break;
- case '8':
- Serial.println("Speed: 8");
- current_speed = 8;
- break;
- case '9':
- Serial.println("Speed: 9");
- current_speed = 9;
- break;
- case 'F':
- Serial.println("Forward");
- // digitalWrite( 3, HIGH );
- // digitalWrite( 5, HIGH );
- analogWrite(3, map(current_speed, 0, 9, 0, 255));
- analogWrite(5, map(current_speed, 0, 9, 0, 255));
- break;
- case 'S':
- Serial.println("Stop");
- digitalWrite( 3, LOW );
- digitalWrite( 5, LOW );
- break;
- case 'R':
- Serial.println("Right");
- digitalWrite( 3, LOW );
- //digitalWrite( 5, HIGH );
- analogWrite(5, map(current_speed, 0, 9, 0, 255));
- break;
- case 'L':
- Serial.println("Left");
- // digitalWrite( 3, HIGH );
- analogWrite(3, map(current_speed, 0, 9, 0, 255));
- digitalWrite( 5, LOW );
- break;
- case 'W':
- Serial.println("Light On");
- digitalWrite( 9, HIGH );
- break;
- case 'w':
- Serial.println("Light Off");
- digitalWrite( 9, LOW );
- break;
- case 'V':
- Serial.println("Hooter On");
- tone( 8, 440 );
- break;
- case 'v':
- Serial.println("Hooter Off");
- noTone( 8 );
- break;
- default:
- Serial.print("Unsupported command: ");
- Serial.println(cmd);
- }
- }
- }
- void setup_bt() {
- delay(1000);
- bluetooth.print("AT");
- delay(1000);
- bluetooth.print("AT+VERSION");
- delay(1000);
- bluetooth.print("AT+PIN1234");
- delay(1000);
- bluetooth.print("AT+NAMEZvikaCoffeeBot");
- delay(1000);
- // bluetooth.print("AT+BAUD4");
- // delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement