Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. //Include arduPi library
  2. #include "arduPi.h"
  3.  
  4. #define SERVO_PIN 9
  5. #define HIGH 1
  6. #define LOW 0
  7. #define dag 1
  8. #define nacht 0
  9.  
  10.  
  11. int switchState = 0 ;
  12.  
  13.  
  14. void setup()
  15. {
  16.  
  17. pinMode(SERVO_PIN, OUTPUT);
  18. pinMode(2, INPUT);
  19. }
  20.  
  21. int lenMicroSecondsOfPeriod = 20 * 1000; // 25 milliseconds (ms)
  22. int lenMicroSecondsOfPulse = 1 * 1000; // 1 ms is 0 degrees
  23. int first = 0.7 * 1000; //0.5ms is 0 degrees
  24. int end = 2.2 * 1000;
  25. int increment = 0.01 * 1000;
  26.  
  27. void loop()
  28. {
  29. switchState = digitalRead(2);
  30. if(switchState == HIGH ) {
  31. int current = 0;
  32. for(current = first; current <end; current+=increment){
  33. // Servos work by sending a 25 ms pulse.
  34. // 0.7 ms at the start of the pulse will turn the servo to the 0 degree position
  35. // 2.2 ms at the start of the pulse will turn the servo to the 90 degree position
  36. // 3.7 ms at the start of the pulse will turn the servo to the 180 degree position
  37. // Turn voltage high to start the period and pulse
  38. digitalWrite(SERVO_PIN, HIGH);
  39.  
  40. // Delay for the length of the pulse
  41. delayMicroseconds(current);
  42.  
  43. // Turn the voltage low for the remainder of the pulse
  44. digitalWrite(SERVO_PIN, LOW);
  45.  
  46. // Delay this loop for the remainder of the period so we don't
  47. // send the next signal too soon or too late
  48. delayMicroseconds(lenMicroSecondsOfPeriod - current);
  49.  
  50. }
  51. delay(2000);
  52. for(current = end; current >first; current-=increment){
  53. // Servos work by sending a 20 ms pulse.
  54. // 0.7 ms at the start of the pulse will turn the servo to the 0 degree position
  55. // 2.2 ms at the start of the pulse will turn the servo to the 90 degree position
  56. // 3.7 ms at the start of the pulse will turn the servo to the 180 degree position
  57. // Turn voltage high to start the period and pulse
  58. digitalWrite(SERVO_PIN, HIGH);
  59.  
  60. // Delay for the length of the pulse
  61. delayMicroseconds(current);
  62.  
  63. // Turn the voltage low for the remainder of the pulse
  64. digitalWrite(SERVO_PIN, LOW);
  65.  
  66. // Delay this loop for the remainder of the period so we don't
  67. // send the next signal too soon or too late
  68. delayMicroseconds(lenMicroSecondsOfPeriod - current);
  69. }
  70. }
  71. }
  72. int main (){
  73. setup();
  74. while(1){
  75. loop();
  76. }
  77. return (0);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement