Created
September 12, 2016 00:01
-
-
Save DeathMetalCoder/57c10203a8f654d494709a0763fa9235 to your computer and use it in GitHub Desktop.
cosplay_servo_wings
This file contains hidden or 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
| /******************************************************************* | |
| SoftServo sketch for Adafruit Trinket. Turn the potentiometer knob | |
| to set the corresponding position on the servo | |
| (0 = zero degrees, full = 180 degrees) | |
| Required library is the Adafruit_SoftServo library | |
| available at https://github.com/adafruit/Adafruit_SoftServo | |
| The standard Arduino IDE servo library will not work with 8 bit | |
| AVR microcontrollers like Trinket and Gemma due to differences | |
| in available timer hardware and programming. We simply refresh | |
| by piggy-backing on the timer0 millis() counter | |
| Required hardware includes an Adafruit Trinket microcontroller | |
| a servo motor, and a potentiometer (nominally 1Kohm to 100Kohm | |
| As written, this is specifically for the Trinket although it should | |
| be Gemma or other boards (Arduino Uno, etc.) with proper pin mappings | |
| Trinket: USB+ Gnd Pin #0 Pin #2 A1 | |
| Connection: Servo+ - Servo1 Potentiometer wiper | |
| *******************************************************************/ | |
| #include <Adafruit_SoftServo.h> // SoftwareServo (works on non PWM pins) | |
| // We demonstrate two servos! | |
| #define SERVO1PIN 0 // Servo control line (orange) on Trinket Pin #0 | |
| #define SERVO2PIN 1 | |
| Adafruit_SoftServo myServo0, myServo1; //create TWO servo objects | |
| bool dirUp = false; | |
| int pos = 0; | |
| void setup() { | |
| // Set up the interrupt that will refresh the servo for us automagically | |
| OCR0A = 0xAF; // any number is OK | |
| TIMSK |= _BV(OCIE0A); | |
| myServo0.attach(SERVO1PIN); // Attach the servo to pin 0 on Trinket | |
| myServo0.write(0); | |
| delay(15); | |
| myServo1.attach(SERVO2PIN); // Attach the servo to pin 0 on Trinket | |
| myServo1.write(0); // Tell servo to go to position per quirk | |
| delay(15); // Wait 15ms for the servo to reach the position | |
| } | |
| void loop() { | |
| if( pos > 180 ) { dirUp = true; } | |
| if ( pos < 0 ) { dirUp = false; } | |
| dirUp == true ? --pos : ++pos; | |
| myServo0.write(pos); | |
| myServo1.write(180-pos); // Tell servo to go to position per quirk | |
| delay(15); // Wait 15ms for the servo to reach the position | |
| } | |
| // We'll take advantage of the built in millis() timer that goes off | |
| // to keep track of time, and refresh the servo every 20 milliseconds | |
| // The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be | |
| // Called by the microcontroller every 2 milliseconds | |
| volatile uint8_t counter = 0; | |
| SIGNAL(TIMER0_COMPA_vect) { | |
| // this gets called every 2 milliseconds | |
| counter += 2; | |
| // every 20 milliseconds, refresh the servos! | |
| if (counter >= 20) { | |
| counter = 0; | |
| myServo0.refresh(); | |
| myServo1.refresh(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment