EmperorPhoenix

Drone Experimental V0.2

Jun 6th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. // ServoDecodeTest
  2.  
  3. #include <ServoDecode.h>
  4. #include <Servo.h>
  5. #include <Scheduler.h>
  6.  
  7.  
  8. Servo myservo[3];
  9.  
  10. char * stateStrings[] = {
  11.  "NOT_SYNCHED", "ACQUIRING", "READY", "in Failsafe"};
  12.  
  13. int pwm[] = {3, 5, 6, 9};
  14. int val[3];
  15. int realval[3];
  16. bool startup = true;
  17. int potval;
  18.  
  19. void setup()                    // run once, when the sketch starts
  20. {
  21.   //myservo.attach(6);
  22.  Serial.begin(9600);  
  23.  pinMode(12,OUTPUT);
  24.  pinMode(13,OUTPUT);  
  25.  ServoDecode.begin();
  26.  ServoDecode.setFailsafe(3,1234); // set channel 3 failsafe pulse  width
  27.  for (int i = 0; i < 4; i++) {
  28.   myservo[i].attach(pwm[i]);
  29.  }
  30.   Scheduler.startLoop(servoLoop);
  31. }
  32.  
  33. /*
  34. void loop()                     //use for testing and hopefully ruNning?
  35. {
  36.  int pulsewidth;
  37.  
  38.  // print the decoder state
  39.  if( ServoDecode.getState()!= READY_state) {
  40.    Serial.print("The decoder is ");
  41.    Serial.println(stateStrings[ServoDecode.getState()]);
  42.    for ( int i =0; i <=MAX_CHANNELS; i++ ){ // print the status of the first four channels
  43.      Serial.print("Cx"); // if you see this, the decoder does not have a valid signal
  44.      Serial.print(i);
  45.      Serial.print("= ");
  46.      pulsewidth = ServoDecode.GetChannelPulseWidth(i);
  47.      Serial.print(pulsewidth);
  48.      Serial.print("  ");
  49.    }
  50.    Serial.println("");
  51.  }
  52.  else {
  53.    // decoder is ready, print the channel pulse widths
  54.    for ( int i =1; i <=MAX_CHANNELS; i++ ){ // print the status of the first four channels
  55.      Serial.print("Ch");
  56.      Serial.print(i);
  57.      Serial.print("= ");
  58.      pulsewidth = ServoDecode.GetChannelPulseWidth(i);
  59.      Serial.print(pulsewidth);
  60.      Serial.print("  ");
  61.    }
  62.    Serial.println("");
  63.    digitalWrite(12,LOW);
  64.    digitalWrite(13,LOW);
  65.  }
  66.  delay(100); // update 2 times a second        
  67. }
  68. */
  69.  
  70. void loop() //use when actually using drone
  71. {
  72.  
  73. /*
  74.   while (startup) {
  75.     potval = map(analogRead(A0), 0, 1023, 0, 155);
  76.     Serial.println(potval);
  77.     myservo.write(potval);
  78.     delay(15);
  79.     if (potval > 150) {
  80.       startup = false;
  81.     }
  82.   }
  83.   for (int i = 1; i <=MAX_CHANNELS; i++) {
  84.     Serial.print("Ch");
  85.     Serial.print(i);
  86.     Serial.print("= ");
  87.     pulsewidth = ServoDecode.GetChannelPulseWidth(i);
  88.     Serial.print(pulsewidth);
  89.     Serial.print(" ");
  90.   }
  91. */
  92.  
  93.   for (int i = 0; i < 4; i++) {
  94.     val[i] = map(ServoDecode.GetChannelPulseWidth(3), 1513, 1099, 0, 155);
  95.   }
  96.   yield();
  97. }
  98.  
  99. void servoLoop(){
  100.   for(int i=0; i<4; i++){
  101.     if (val[i] > 50) {
  102.       Serial.print(val[i]);
  103.       Serial.print(" ");
  104.       Serial.print("SPIN");
  105.     }
  106.   }
  107.   for (int i = 0; i < 4; i++) {
  108.     myservo[i].write(val[i]);
  109.   }
  110.   yield();
  111. }
  112.  
  113. /*
  114. void loop() {
  115.   potval = analogRead(A0);            // reads the value of the potentiometer (value between 0 and 1023)
  116.   potval = map(potval, 0, 1023, 0, 155);     // scale it to use it with the servo (value between 0 and 180)
  117.   Serial.println(potval);
  118.   myservo.write(potval);                  // sets the servo position according to the scaled value
  119.   delay(15);                           // waits for the servo to get there
  120. }
  121. */
Advertisement
Add Comment
Please, Sign In to add comment