Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <Servo.h>
  3. Servo myServo;
  4.  
  5. SoftwareSerial hc06(2,3);
  6. char cmd[100];
  7. int cmdIndex;
  8.  
  9. void setup(){
  10. //Initialize Serial Monitor
  11. Serial.begin(9600);
  12. //Initialize Bluetooth Serial Port
  13. hc06.begin(9600);
  14. cmdIndex = 0;
  15.  
  16. myServo.attach(4);
  17. }
  18.  
  19. void loop(){
  20. //Write data from HC06 to Serial Monitor
  21. if (hc06.available()){
  22. char c = hc06.read();
  23.  
  24.  
  25. if(c=='\n') {
  26. cmd[cmdIndex] = 0;
  27. exeCmd(); // execute the command
  28. cmdIndex = 0; // reset the cmdIndex
  29. } else {
  30. cmd[cmdIndex] = c;
  31. if(cmdIndex<99) cmdIndex++;
  32. }
  33. }
  34.  
  35. }
  36.  
  37. void exeCmd() {
  38.  
  39. // "servo" is the servo motor id
  40.  
  41. if(cmd[0]=='0' &&
  42. cmd[1]==' ') {
  43.  
  44. int val = 0;
  45. for(int i=2; cmd[i]!=0; i++) {
  46. val = val*10 + (cmd[i]-'0');
  47. }
  48. Serial.println(val);
  49. myServo.write(val);
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement