Facebook
From Thanat, 1 Month ago, written in Actionscript.
This paste is a reply to servo model from Thanat - view diff
Embed
Download Paste or View Raw
Hits: 34
  1. /* Sweep
  2.   by BARRAGAN <http://barraganstudio.com>
  3.   This example code is in the public domain.
  4.  
  5.   modified 8 Nov 2013
  6.   by Scott Fitzgerald
  7.   http://www.arduino.cc/en/Tutorial/Sweep
  8. */
  9.  
  10. #include <Servo.h>
  11. unsigned long duration,temp_duration = 0;
  12. Servo servo1;  // create servo object to control a servo
  13. Servo servo2;
  14. Servo servo3;
  15. Servo servo4;
  16. // twelve servo objects can be created on most boards
  17.  
  18. int pos = 0;    // variable to store the servo position
  19.  
  20. void setup() {
  21.     Serial.begin(9600);
  22.   servo1.attach(9, 0, 180); // attaches the servo on pin 9 to the servo object
  23.   servo2.attach(8, 0, 180);
  24.   servo3.attach(7, 0, 180);
  25.   servo4.attach(6, 0, 180);
  26.   pinMode(10, INPUT);
  27.  
  28.  
  29. }
  30.  
  31. void extend ()
  32. {
  33.   for (pos = 100; pos <= 180; pos += 1) {
  34.     // in steps of 1 degree
  35.     servo1.write(pos);
  36.     servo2.write(pos);
  37.     servo3.write(pos);
  38.     servo4.write(pos);
  39.     delay(10);
  40.   }
  41. }
  42. void retract ()
  43. {
  44.   for (pos = 180; pos >= 100; pos -= 1) {
  45.     servo1.write(pos);
  46.     servo2.write(pos);
  47.     servo3.write(pos);
  48.     servo4.write(pos);
  49.     delay(10);
  50.   }
  51. }
  52.  
  53.  
  54.  
  55. void loop() {
  56.  
  57. temp_duration = duration;  
  58. duration = pulseIn(10, HIGH);
  59.  
  60. if ((temp_duration/100) != (duration/100) and (duration - temp_duration > 50  )
  61. {
  62.   Serial.println(duration);
  63.   if(((duration/100)==12) or ((duration/100)==13) )
  64.   {
  65.     extend();
  66.   }
  67.   else
  68.   {
  69.     retract();
  70.   }
  71. }
  72.  
  73. //Serial.println(duration);
  74.   //extend();
  75.   //retract();
  76.  
  77. }