Advertisement
Guest User

CoffeeBotBluetooth.ino

a guest
Jan 22nd, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. /*
  2.   This program makes your robot move according to commands transmitted to it over bluetooth connection
  3.   bluetooth module should be connected to pins 10,11 (RX, TX)
  4.   reference bluetooth module:
  5.   http://dx.com/p/jy-mcu-arduino-bluetooth-wireless-serial-port-module-104299#.UuBS5rSxVL5
  6.   Controlling Android app: Bluetooth RC Controller, available at:
  7.   https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller&hl=en
  8. */
  9.  
  10. #include <SoftwareSerial.h>
  11.  
  12. SoftwareSerial bluetooth(10, 11); // RX, TX
  13.  
  14. void setup()
  15. {
  16.   // Set the mode of the digital pins to outputs to drive the motors, LED and horn
  17.   pinMode( 3, OUTPUT ); // motor
  18.   pinMode( 5, OUTPUT ); // motor
  19.   pinMode( 8, OUTPUT ); // speaker
  20.   pinMode( 9, OUTPUT ); // LED
  21.   Serial.begin(9600);
  22.   bluetooth.begin(9600);
  23.   //setup_bt();
  24. }
  25.  
  26. int current_speed = 0;
  27.  
  28. void loop()
  29. {
  30.   if (bluetooth.available()) {
  31.     char cmd = bluetooth.read();
  32.     switch(cmd) {
  33.       case '0':
  34.         Serial.println("Speed: 0");
  35.         current_speed = 0;
  36.         break;
  37.       case '1':
  38.         Serial.println("Speed: 1");
  39.         current_speed = 1;
  40.         break;
  41.       case '2':
  42.         Serial.println("Speed: 2");
  43.         current_speed = 2;
  44.         break;
  45.       case '3':
  46.         Serial.println("Speed: 3");
  47.         current_speed = 3;
  48.         break;
  49.       case '4':
  50.         Serial.println("Speed: 4");
  51.         current_speed = 4;
  52.         break;
  53.       case '5':
  54.         Serial.println("Speed: 5");
  55.         current_speed = 5;
  56.         break;
  57.       case '6':
  58.         Serial.println("Speed: 6");
  59.         current_speed = 6;
  60.         break;
  61.       case '7':
  62.         Serial.println("Speed: 7");
  63.         current_speed = 7;
  64.         break;
  65.       case '8':
  66.         Serial.println("Speed: 8");
  67.         current_speed = 8;
  68.         break;
  69.       case '9':
  70.         Serial.println("Speed: 9");
  71.         current_speed = 9;
  72.         break;
  73.       case 'F':
  74.         Serial.println("Forward");
  75.         // digitalWrite( 3, HIGH );
  76.         // digitalWrite( 5, HIGH );
  77.         analogWrite(3, map(current_speed, 0, 9, 0, 255));
  78.         analogWrite(5, map(current_speed, 0, 9, 0, 255));
  79.         break;
  80.       case 'S':
  81.         Serial.println("Stop");
  82.         digitalWrite( 3, LOW );
  83.         digitalWrite( 5, LOW );
  84.         break;
  85.       case 'R':
  86.         Serial.println("Right");
  87.         digitalWrite( 3, LOW );  
  88.         //digitalWrite( 5, HIGH );
  89.         analogWrite(5, map(current_speed, 0, 9, 0, 255));
  90.         break;
  91.       case 'L':
  92.         Serial.println("Left");
  93.         // digitalWrite( 3, HIGH );
  94.         analogWrite(3, map(current_speed, 0, 9, 0, 255));
  95.         digitalWrite( 5, LOW );
  96.         break;
  97.       case 'W':
  98.         Serial.println("Light On");
  99.         digitalWrite( 9, HIGH );
  100.         break;
  101.       case 'w':
  102.         Serial.println("Light Off");
  103.         digitalWrite( 9, LOW );
  104.         break;
  105.       case 'V':
  106.         Serial.println("Hooter On");
  107.         tone( 8, 440 );
  108.         break;
  109.       case 'v':
  110.         Serial.println("Hooter Off");
  111.         noTone( 8 );
  112.         break;
  113.       default:
  114.         Serial.print("Unsupported command: ");
  115.         Serial.println(cmd);
  116.     }
  117.   }
  118. }
  119.  
  120. void setup_bt() {
  121.   delay(1000);
  122.   bluetooth.print("AT");
  123.   delay(1000);
  124.   bluetooth.print("AT+VERSION");
  125.   delay(1000);
  126.   bluetooth.print("AT+PIN1234");
  127.   delay(1000);
  128.   bluetooth.print("AT+NAMEZvikaCoffeeBot");
  129.   delay(1000);
  130. //  bluetooth.print("AT+BAUD4");
  131. //  delay(1000);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement