I want it to do two things, randomly rotate the neck (which I've commented out for the time being) and most importantly, open the mouth when a button is pressed, close it when it's not. What I see happen is mostly nothing. I can attach the button press to the onboard LED successfully, but when I try using either of the servos, nothing happens, though they seem to stutter whenever the servo interrupt refresh Adafruit calls out is included (https://learn.adafruit.com/trinket-gemm ... ntrol/code).
I'm not 100% sure I didn't mess something up on the board connections, but I can't help but think it's code. Would someone mind taking a look to see if I've made an error?
Code: Select all
#include <Adafruit_SoftServo.h> // SoftwareServo (works on non PWM pins)
#define mouthButton 4
//#define LED 1
#define MOUTHPIN 3
#define HEADPIN 2
Adafruit_SoftServo neckServo, mouthServo;
int neckTime = 2000;
int currTime = 0;
int defaultDelay = 15;
void setup()
{
// initialize the LED pin as an output.
//pinMode(LED, OUTPUT);
// initialize the SWITCH pin as an input.
pinMode(mouthButton, INPUT);
// ...with a pullup
digitalWrite(mouthButton, HIGH);
// Set up the interrupt that will refresh the servo for us automagically
OCR0A = 0xAE;
TIMSK |= _BV(OCIE0A);
mouthServo.attach(MOUTHPIN); // attaches the mouth servo
mouthServo.write(89);
//neckServo.attach(HEADPIN); //attaches the head servo
//neckServo.write(90);
}
void loop(){
if (digitalRead(mouthButton) == HIGH) {
mouthServo.write(89);
//digitalWrite(LED, LOW);
} else {
mouthServo.write(160);
//digitalWrite(LED, HIGH);
}
/*
if(currTime>=neckTime){
neckServo.write(random(20,170));
currTime = 0;
neckTime = (random(250,3000));
}else{
currTime = currTime+defaultDelay;
}/*
delay(defaultDelay);
*/
delay(15);
}
// 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_COPA_vect) {
if (counter >= 20) {
counter = 0;
mouthServo.refresh();
}
}
I left the LED stuff in there to show what I had working but ONLY when the interrupt code was commented out. I'll snap a photo of the actual circuit if it'll help.