Skip to content

Instantly share code, notes, and snippets.

@carneeki
Created October 28, 2019 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carneeki/c20760f9999ec1a150891ff613af2600 to your computer and use it in GitHub Desktop.
Save carneeki/c20760f9999ec1a150891ff613af2600 to your computer and use it in GitHub Desktop.
Arduino sketch to move a servo given some kind of trigger
/*
* 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