Okay, I have no idea why it works now, whatever, so here’s my code (basically just changed the slowspeed, dunno what fastspeed is for, but this is basically the code you posted, after i fixed the SERVO1PIN to servoPIN like I posted above)
/*******************************************************************
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 #1
Connection: Servo+ - Servo1 Switch
*******************************************************************/
#include <Adafruit_SoftServo.h> // SoftwareServo (works on non PWM pins)
#define servoPIN 2 //Servo control line (orange) on Trinket Pin #0
#define SWitchPIN 3 //Momentary switch to ground when pushed to open/close door
#define posOpenCMD 130 //Define Clockwise Servo Limit in deg
#define posCloseCMD 0 //Define CounterClockwise Servo Limit in deg
#define fastTime 8000 //time to quickly open/close door, min of 3600 msec
#define slowTime 4000 //time to normally open/close door
boolean doorOPEN = false; //initialize desired door state to closed
Adafruit_SoftServo myServo1; //create a servo object
void setup() {
// Set up the interrupt that will refresh the servo for us automagically
OCR0A = 0xAF; // any number is OK
TIMSK |= _BV(OCIE0A); // Turn on the compare interrupt (below!)
//setup the IO pins
pinMode(servoPIN, OUTPUT);
pinMode(SWitchPIN, INPUT_PULLUP);
myServo1.attach(servoPIN); // Attach the servo to pin 0 on Trinket
}
void loop() {
if (digitalRead(SWitchPIN) == LOW) { //see if switch is being pushed
doorOPEN = !doorOPEN; //reverse desired door state
moveDoor(slowTime); //call function to command the servo
}
delay(50); // waits 50 ms btw reads of switch
}
// 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;
myServo1.refresh();
}
}
// Function called whenever door is to be moved at a speed slower
// than normal servo speed. Interrupts work during this function but
//button pushes will be ignored until motion is complete
void moveDoor(unsigned long time) {
//compute the time needed to go 1 deg
unsigned long Dlay = time / (posOpenCMD - posCloseCMD);
//figure out if door is to be opened or closed
if (doorOPEN == true) {
//tell servo to go to open position, 1 deg at a time
for (int pos = posCloseCMD; pos < posOpenCMD; pos++) {
myServo1.write(pos);
delay(Dlay);
}
}
else {
//tell servo to go to closed position, 1 deg at a time
for (int pos = posOpenCMD; pos > posCloseCMD; pos--) {
myServo1.write(pos);
delay(Dlay);
}
}
}
The only problem is that when it’s uploaded, or when I unplug/replug USB, the servo quickly swings all the way 180* (let’s say closed ~10*, open ~150) and just buzzes there (i think it’s pressing up against my PSU). I hit the switch, and it swings all the way closed very quickly.
As in, if it was attached to the door, it would probably bring the hinges off quickly (which has happened MULTIPLE times, not in a while because I make sure before unplugging, replugging, uploading, etc).
Now, I plan to run off rechargeable battery and ideally the power will never shut off. BUT, should I put it into long term storage or something, or switch off the battery, it would do this. And uhh that’d be a nasty surprise, I probably wouldn’t even remember that it does that after such a storage.
I’m not sure why it’s doing that but I gotta fix that in the code. I’m pretty sure when the arduino micro was worked out where everything was all together, it didn’t do that.