Skip to content

Instantly share code, notes, and snippets.

@chriswoodle
Created February 17, 2019 06:55
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 chriswoodle/07cc9ec5fbb94f370a93a52f5e02187a to your computer and use it in GitHub Desktop.
Save chriswoodle/07cc9ec5fbb94f370a93a52f5e02187a to your computer and use it in GitHub Desktop.
hacknyu2019-arduino-turnstile
// Chris Woodle, HackNYU2019
// Send an 'S' to toggle a servo open and closed
// Refrences:
// https://www.arduino.cc/en/tutorial/sweep
// https://www.arduino.cc/en/serial/read
const char SIGNAL = 'S';
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(0); // Set to 0 position
myservo.detach(); // disable servo
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void openTurnstile() {
myservo.attach(9);
int pos = 0; // variable to store the servo position
for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
myservo.detach();
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
int incomingByte = Serial.read(); // read the incoming byte:
Serial.print("Recieved: ");
Serial.println(incomingByte, DEC);
if (incomingByte == SIGNAL) {
openTurnstile();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment