Created
October 28, 2019 02:33
-
-
Save carneeki/c20760f9999ec1a150891ff613af2600 to your computer and use it in GitHub Desktop.
Arduino sketch to move a servo given some kind of trigger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Boney.ino | |
* Arduino sketch to move a servo given some kind of trigger | |
* (falling edge) | |
* | |
* Author: Adam Carmichael <carneeki@carneeki.net> | |
* License: Unlicensed. | |
* Warranties: None. This hasn't been tested yet. Don't poke your eye | |
* out. | |
*/ | |
#include <Servo.h> | |
#define POS_DOWN 0 // down position | |
#define POS_UP 180 // up position | |
#define HOLD_TIME 3000 // hold up for this many millis | |
#define PIN_SERVO 9 // servo pin (must be PWM capable) | |
#define PIN_TRIG 2 // only pins 2 or 3 for interrupts on uno | |
Servo srv; | |
boolean state = false; | |
void setup() { | |
pinMode(PIN_TRIG, INPUT_PULLUP); | |
pinMode(PIN_SERVO, OUTPUT); | |
srv.attach(PIN_SERVO); | |
} | |
void main() { | |
srv.write(state ? POS_UP : POS_DOWN); | |
if(state) { | |
delay(HOLD_TIME); | |
state = false; | |
} | |
sleep(50); | |
} | |
void raise() { | |
state = true; | |
} | |
// call ISR on falling edge | |
attachInterrupt(digitalPinToInterrupt(PIN_TRIG), raise, FALLING); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment