Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <Servo.h>
  3.  
  4. String time;
  5. long temp;
  6. //Connect:
  7. //arduino >> bluetooth
  8. // D10 >> TX
  9. // D11 >> RX
  10.  
  11. Servo servo1, servo2;
  12. SoftwareSerial mySerial(10, 11); //RX, TX  
  13.  
  14. String readString; //main captured String
  15. String pitch; //data String
  16. String roll;
  17. String throttle;
  18.  
  19. int ind1; // , locations
  20. int ind2;
  21. int ind3;
  22.  
  23. int s1, s2;
  24.  
  25.  
  26. void setup() {
  27.   pinMode(9, OUTPUT);
  28.   servo1.attach(9); // Attaches pin D9 on nano to Servo! :)
  29.   pinMode(5, OUTPUT);
  30.   servo2.attach(5);
  31.   Serial.begin(9600);
  32.  
  33.   servo1.writeMicroseconds(700); // initialize servos
  34.   servo2.writeMicroseconds(700);
  35.    
  36.   mySerial.begin(9600);
  37.   mySerial.setTimeout(50);
  38.  
  39. }
  40.  
  41. void loop() {
  42.  
  43. // Denne setupen forventer en string på formen: x,y,z*  -- Viktig at det er kommadelt og en stjerne på slutten! :-)
  44. // Variable1(pitch),variable2(roll),variable3(throttle)*
  45.  
  46.   if (mySerial.available())  {
  47.     char c = mySerial.read();  //gets one byte from serial buffer
  48.     if (c == '*') {
  49.       //do stuff
  50.       ind1 = readString.indexOf(',');  //finds location of first ,
  51.       roll = readString.substring(0, ind1);   //captures first data String
  52.       ind2 = readString.indexOf(',', ind1+1 );   //finds location of second ,
  53.       pitch = readString.substring(ind1+1, ind2);   //captures second data String
  54.       ind3 = readString.indexOf(',', ind2+1 );
  55.       throttle = readString.substring(ind2+1, ind3); // ind3+1
  56.      
  57.       Serial.print("Roll: ");
  58.       Serial.println(roll);
  59.       Serial.print("Pitch: ");
  60.       Serial.println(pitch);
  61.       Serial.print("Throttle: ");
  62.       Serial.println(throttle);
  63.       Serial.println("");
  64.      
  65.       s1 = pitch.toInt(); // Casts the string back to int or float, whatever we choose to use.
  66.       s2 = roll.toInt();
  67.      
  68.       servo1.writeMicroseconds(s1);
  69.       servo2.writeMicroseconds(s2);
  70.      
  71.       readString=""; //clears variable for new input
  72.       pitch="";
  73.       roll="";
  74.       throttle="";
  75.       mySerial.write("u"); // Sends signal back to python
  76.     }  
  77.     else {    
  78.       readString += c; //makes the string readString
  79.     }
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement