Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Include arduPi library
- #include "arduPi.h"
- #define SERVO_PIN 9
- #define HIGH 1
- #define LOW 0
- #define dag 1
- #define nacht 0
- int switchState = 0 ;
- void setup()
- {
- pinMode(SERVO_PIN, OUTPUT);
- pinMode(2, INPUT);
- }
- int lenMicroSecondsOfPeriod = 20 * 1000; // 25 milliseconds (ms)
- int lenMicroSecondsOfPulse = 1 * 1000; // 1 ms is 0 degrees
- int first = 0.7 * 1000; //0.5ms is 0 degrees
- int end = 2.2 * 1000;
- int increment = 0.01 * 1000;
- void loop()
- {
- switchState = digitalRead(2);
- if(switchState == HIGH ) {
- int current = 0;
- for(current = first; current <end; current+=increment){
- // Servos work by sending a 25 ms pulse.
- // 0.7 ms at the start of the pulse will turn the servo to the 0 degree position
- // 2.2 ms at the start of the pulse will turn the servo to the 90 degree position
- // 3.7 ms at the start of the pulse will turn the servo to the 180 degree position
- // Turn voltage high to start the period and pulse
- digitalWrite(SERVO_PIN, HIGH);
- // Delay for the length of the pulse
- delayMicroseconds(current);
- // Turn the voltage low for the remainder of the pulse
- digitalWrite(SERVO_PIN, LOW);
- // Delay this loop for the remainder of the period so we don't
- // send the next signal too soon or too late
- delayMicroseconds(lenMicroSecondsOfPeriod - current);
- }
- delay(2000);
- for(current = end; current >first; current-=increment){
- // Servos work by sending a 20 ms pulse.
- // 0.7 ms at the start of the pulse will turn the servo to the 0 degree position
- // 2.2 ms at the start of the pulse will turn the servo to the 90 degree position
- // 3.7 ms at the start of the pulse will turn the servo to the 180 degree position
- // Turn voltage high to start the period and pulse
- digitalWrite(SERVO_PIN, HIGH);
- // Delay for the length of the pulse
- delayMicroseconds(current);
- // Turn the voltage low for the remainder of the pulse
- digitalWrite(SERVO_PIN, LOW);
- // Delay this loop for the remainder of the period so we don't
- // send the next signal too soon or too late
- delayMicroseconds(lenMicroSecondsOfPeriod - current);
- }
- }
- }
- int main (){
- setup();
- while(1){
- loop();
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement