Using Actuator to Automate Open/Close of Computer Side Panel

Belial88:
However, I still am unable to figure out your code. Trying to google-fu button inhibition.

Well post the pieces you don't get and I can explain.

You said no peeking lol. I’ve googled ‘button inhibition’ but not sure still. I only saw one arduino article on it.

I still wish I could do a battery backup thing… I see thee duracell usb chargers, but they are very expensive (relative to this mod) at $30+ and are only usb voltage, ie 4.8-5v. Hopefully that’ll be all I need but still looking for something else.

Theres usb chargers on ebay but I’m just not sure if they can charge and output electrcity at the same time. I know some usb chargers cant, like they have a switch for chargin vs being charged.

Heyo, I got the code to work on the arduino micro. Code as follows:

#include <Servo.h> // SoftwareServo (works on non PWM pins)

//declare the constants to be used
#define LEDPIN 13            //pin attached to led
#define servoPIN 9          //Servo control line (orange) on Trinket Pin #2
#define SWitchPIN 2         //input from N.O. momentary switch

#define posOpenCMD 180      //command in deg to servo to open the door
#define posCloseCMD 0      //command in deg to servo to close the door
#define DBdelay 100         //delay in ms btw readings of the switch
#define doorTime 5000       //time in ms btw opening and closing of door
#define MAXcnt 3            //need this many same consecutive readings of button pressed

//declare the variables used
boolean inMotion = false;   //door is moving, or not
boolean doorOPEN = false;   //desired door state set to closed
boolean SWstate = true;     //state of switch, open = TRUE = not pushed
byte SWcnt = 0;             //counter of same consecutive switch readings w/button = pressed
int STEPcnt = 0;            //counter for stepping door command from open - closed
int STEPsize = 0;           //size of step for door command
int posCMD = posCloseCMD;   //servo position command in deg

Servo myServo; //create a servo object

void setup() {

  //set the pins to be ins or outs
  pinMode(LEDPIN, OUTPUT);
  digitalWrite(LEDPIN, LOW);  //turn LED off
  pinMode(servoPIN, OUTPUT);
  pinMode(SWitchPIN, INPUT_PULLUP);
  myServo.attach(servoPIN,600,2400);   // Attach the servo on Trinket

  //compute the step size used by the servo to open/close the door
  STEPsize = max(1,(posOpenCMD - posCloseCMD)/(doorTime/DBdelay));
}

void loop() {
  //read the switch if the door is not moving
  if(digitalRead(SWitchPIN) == LOW && !inMotion){   //see if switch is being pushed
    SWcnt ++;                //SW reads low = pushed, increment debounce counter
    if(SWcnt >= MAXcnt){     //test if switch is really being pushed AND done bouncing
      SWcnt = 0;             //done bouncing, reset counter for next push
      doorOPEN = !doorOPEN;  //reverse desired door state
      inMotion = true;       //set door in motion flag
      STEPcnt = 0;           //zero out position step counter
    }
  }
  else {
    SWcnt = 0;              //switch is released or bouncing
  }
  //now compute servo command based on desired door state
  if(doorOPEN == true){
    digitalWrite(LEDPIN, HIGH);       //turn LED on
    posCMD = max(posCloseCMD,(posCMD + STEPcnt*STEPsize));   //compute position, prevent rollover
    posCMD = min(posOpenCMD, posCMD); //limit max command to door open value
    myServo.write(posCMD);            //tell servo to go to open position, step by step
    if(posOpenCMD - posCMD < 2){      //close enough to done
      inMotion = false;               //reset door in motion flag
    }
  }
  else {
    digitalWrite(LEDPIN, LOW);         //turn LED off
    posCMD = min(posOpenCMD,(posCMD - STEPcnt*STEPsize));   //compute position, prevent rollunder
    posCMD = max(posCloseCMD, posCMD); //limit min command to door closed value
    myServo.write(posCMD);             //tell servo to go to closed position, step by step
    if(posCMD - posCloseCMD < 2){      //close enough to done
      inMotion = false;                //reset door in motion flag
    }
  }
  STEPcnt = min(10000, STEPcnt++);   //increment step count but prevent rollover
  delay(DBdelay);           //wait for next read 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;
  }
}

I replaced the top #include <Adafruit_SoftServo.h> to #include <Servo.h> because I understand libraries (essentially it’s a way to call upon multiple ‘things’, usually settings, so you don’t have to type it all out every time)! I also changed the pin-outs. I noticed in the setup section, you had the servo pin assignment as servoPIN and that’s cool, I see that that’s a feature of ‘good code’.

I then removed the

  // 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!)

and

 myServo.refresh();
  }
}

because it wouldn’t compile/upload with those lines, and I don’t know what they mean and it seems to work fine without them. I’ve been reading a ton up on arduino coding and stuff every day. A far, far possible end goal to all this is custom pcb’s and massive Belial’s Custom Side Panel mod kits or building it to sell for a corporation, but that’s a long ways away.

I’m also confused on DBdelay and doortime. It seems DBDelay is how long you have to hold the switch in order for it to register an open/close command, but it also affects the speed of door/servo. I have no idea what doortime is. It sometimes seems to affect door open speed/servo speed, but it’s not like raising it slows it down, or lowering speeds it up. It’s really weird. Like 50 and 850 are really fast, but both 10 and 100000 is slow.

One thing that is a bummer, is that servo moves kind of jerky. Is there a way to not make it move so jerky?

What I basically want in the code, is, controlling throw and speed of the turning, to have it where you have to hold the button for X-controllable time in order to open/close. I actually don’t think I mind interrupting the process (oh my god dont open it was a mistake my coffee is right there! but thanks to you both codes are available) anymore. The LED turning on when on Open is cool too.

I will power it via that usb battery, and I’m going to put an SPST switch inline on the USB cable from the battery to arduino. I’m going to use a 1-position small slide DIP switch because those switches aren’t really meant to be switched often (wish I could find it in black, not only in red, but maybe i paint it). You know, so if the case needs to be put away for a month or something insane like that, you can turn it off and the battery doesn’t die. ie 9mah for idle on the micro on a 6000mah battery = 27 days.

There’s actually a power button on the Powergen usb battery I have, but it’s on the actual battery itself, so that doesn’t really do me good… unless I made the battery accessible when the side panel is open and then I just unhooked the servo for said long term storage, or made it accessible from the other side panel in which case you’d have to reattach the side panel or something… yea maybe I don’t need a power button after all… whatever, power button is cooler.

Oh, and to bring it back to the servo thread, you said this in the other thread:

The tutorials are sometimes wrong. Sometimes you can get away with a marginal situation, wondering why your servo twitches occasionally. Proper engineering means you’re sure that’ll work.

FWIW my original concerns re: the mechanics of the door mechanism remain. Get it working, post vid to prove me wrong. :mrgreen:

So… does that mean I can, or can’t, power a ~.9amp servo off an arduino micro?

edit: okay I’m really trying to code this stuff myself, i heard of something caled VarSpeedServo which is another servo library. I don’t understand it. I got it compile (change library, what you put in before you add servo object),but can’t really make it work. myServo.slowmove(newpos, speed). I have no idea what newposition is for, i just set 600 for whatever. But speed, 1 to 255 makes no difference.

Belial88:
I replaced the top #include <Adafruit_SoftServo.h> to #include <Servo.h> because I understand libraries (essentially it’s a way to call upon multiple ‘things’, usually settings, so you don’t have to type it all out every time)!

I then removed the

  // 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!)




and




myServo.refresh();
}
}




because it wouldn't compile/upload with those lines, and I don't know what they mean and it seems to work fine without them.

That’s correct. You can now use the “real” servo library since you’re not constrained by the Trinket.

Belial88:
I’m also confused on DBdelay and doortime. It seems DBDelay is how long you have to hold the switch in order for it to register an open/close command, but it also affects the speed of door/servo. I have no idea what doortime is. It sometimes seems to affect door open speed/servo speed, but it’s not like raising it slows it down, or lowering speeds it up. It’s really weird. Like 50 and 850 are really fast, but both 10 and 100000 is slow.

One thing that is a bummer, is that servo moves kind of jerky. Is there a way to not make it move so jerky?

What I basically want in the code, is, controlling throw and speed of the turning, to have it where you have to hold the button for X-controllable time in order to open/close.

edit: okay I’m really trying to code this stuff myself, i heard of something caled VarSpeedServo which is another servo library. I don’t understand it. I got it compile (change library, what you put in before you add servo object),but can’t really make it work. myServo.slowmove(newpos, speed). I have no idea what newposition is for, i just set 600 for whatever. But speed, 1 to 255 makes no difference.

One thing to recall is that we modified the Adafruit Trinket servo library to make different pulsewidths (xtra short and wide) for the 0 and 180 deg commands. IIRC there was some odd twitching prior to that. With the real library you can command the PW directly. If the VarSpeedServo takes usecs as an input argument as well as speed, you may have a simple answer to the twitch, speed control and servo throw issues. I'll have to find and look at the VarSpeedServo function.

Belial88:
So… does that mean I can, or can’t, power a ~.9amp servo off an arduino micro?

It depends. If the Micro gets it's power off a normal USB 2.0 port, I doubt it. Perhaps, if from a USB3 port. If your battery will source full current w/o the Micro having to request it (I think this is likely), then yes when on that power.

Have a look at the revision of the VarSpeedServo library here.

https://github.com/netlabtoolkit/VarSpeedServo

It takes 400-2400 usec as an position input, a speed and a flag to tell the operation (slow servo commands) to complete before doing the next position. Read the readme file. You could just about go back to the 1’st working code I posted and use that as the base door control code.

Here’s that earlier, simple code modified to use the above library. You had mentioned something about controlling/setting the door speed via a button … this doesn’t do that, you need to change the value of slowSPD. I’ll leave the fancy feature to you. I note the following from the library ;

          // speed=0 is identical to write, speed=1 slowest and speed=255 fastest.
          // On the RC-Servos tested, speeds differences above 127 can't be noticed,
          // because of the mechanical limits of the servo.

One other thing to note is the wait flag. It stops all other processing until the door is done moving. I’m not so sure that’ll play well with the LCDSmartie code that you’ll need to combine with this code. LCDSmartie may not tolerate messages sent to it but lost due to the door being in motion.

#include <VarSpeedServo.h>  //variable speed servo library

//declare the constants to be used
#define LEDPIN 1            //pin attached to led
#define servoPIN 2          //Servo control line (orange) on Trinket Pin #2
#define SWitchPIN 3         //input from N.O. momentary switch

#define posOpenCMD 2400     //command in usec to servo to open the door
#define posCloseCMD 600     //command in usec to servo to close the door
#define DBdelay 50          //delay in ms btw readings of the switch
#define MAXcnt 3            //need this many same consecutive readings of button pressed
#define fastSPD 255         //speed setting; 1 = slowest, 255 is fastest 
#define slowSPD 50          //normal speed setting

//declare the variables used
boolean wait = true;        //wait for motion to complete, or don't
boolean doorOPEN = false;   //desired door state set to closed
boolean SWstate = true;     //state of switch, open = TRUE = not pushed
byte SWcnt = 0;             //counter of same consecutive switch readings w/button = pressed

VarSpeedServo myServo; //create a servo object

void setup() {
  //set the pins to be ins or outs
  pinMode(LEDPIN, OUTPUT);
  digitalWrite(LEDPIN, LOW);  //turn LED off
  pinMode(servoPIN, OUTPUT);
  pinMode(SWitchPIN, INPUT_PULLUP);

  //CMD the servo to close the door
  myServo.attach(servoPIN);   // Attach the servo on Trinket
  myServo.write(posCloseCMD, fastSPD, wait); // Tell servo to go to closed position
}

void loop() {
  //read the switch
  if(digitalRead(SWitchPIN) == LOW){   //see if switch is being pushed
    SWcnt ++;                //SW reads low = pushed, increment debounce counter
    if(SWcnt >= MAXcnt){     //test if switch is really being pushed AND done bouncing
      SWcnt = 0;             //done bouncing, reset counter for next push
      doorOPEN = !doorOPEN;  //reverse desired door state
      if(doorOPEN == true){
        digitalWrite(LEDPIN, HIGH);   //turn LED on
        myServo.write(posOpenCMD, slowSPD, wait);    //tell servo to go to open position
      } 
      else {
        digitalWrite(LEDPIN, LOW);    //turn LED off
        myServo.write(posCloseCMD, slowSPD, wait);   //tell servo to go to closed position
      }
    }
  } 
  else {
    SWcnt = 0;              //switch is released or bouncing
  }
  delay(DBdelay);           //wait for next read of switch
}

Interesting, so my problem was that I had to define stuff?

Here’s the code I have. I changed the descriptions on the #defines to my understanding. I changed ‘myServo.write’ to ‘myServo.slowmove’, and I removed the ‘wait’ commands attached to those because it wouldn’t compile with them.

I don’t mind the LCD going off momentarily while door open/closes. What is the point of the ‘wait’?

#include <VarSpeedServo.h>  //variable speed servo library

//declare the constants to be used
#define LEDPIN 13            //pin attached to led
#define servoPIN 9          //Servo control line (orange) on Trinket Pin #2
#define SWitchPIN 2         //input from N.O. momentary switch

#define posOpenCMD 2400     //Define Clockwise Servo Limit in usec
#define posCloseCMD 600     //Define CounterClockwise Servo Limit in usec
#define DBdelay 200          //Duration of each button press in ms
#define MAXcnt 3            //Count of Button Presses needed for action
#define fastSPD 100         //speed setting; 1 = slowest, 255 is fastest 
#define slowSPD 30          //Speed Servo/Door opens and closes

//declare the variables used
boolean wait = true;        //wait for motion to complete, or don't
boolean doorOPEN = false;   //desired door state set to closed
boolean SWstate = true;     //state of switch, open = TRUE = not pushed
byte SWcnt = 0;             //counter of same consecutive switch readings w/button = pressed

VarSpeedServo myServo; //create a servo object

void setup() {
  //set the pins to be ins or outs
  pinMode(LEDPIN, OUTPUT);
  digitalWrite(LEDPIN, LOW);  //turn LED off
  pinMode(servoPIN, OUTPUT);
  pinMode(SWitchPIN, INPUT_PULLUP);

  //CMD the servo to close the door
  myServo.attach(servoPIN);   // Attach the servo
  myServo.slowmove(posCloseCMD, fastSPD); // Tell servo to go to closed position
}

void loop() {
  //read the switch
  if(digitalRead(SWitchPIN) == LOW){   //see if switch is being pushed
    SWcnt ++;                //SW reads low = pushed, increment debounce counter
    if(SWcnt >= MAXcnt){     //test if switch is really being pushed AND done bouncing
      SWcnt = 0;             //done bouncing, reset counter for next push
      doorOPEN = !doorOPEN;  //reverse desired door state
      if(doorOPEN == true){
        digitalWrite(LEDPIN, HIGH);   //turn LED on
        myServo.slowmove(posOpenCMD, slowSPD);    //tell servo to go to open position
      } 
      else {
        digitalWrite(LEDPIN, LOW);    //turn LED off
        myServo.slowmove(posCloseCMD, slowSPD);   //tell servo to go to closed position
      }
    }
  } 
  else {
    SWcnt = 0;              //switch is released or bouncing
  }
  delay(DBdelay);           //wait for next read of switch
}

I mean this seems pretty finalized to me, save for some little tweaking when it’s actually installed, but as far as I see the code I’d recommend if I were to write a guide on this or something.

Belial88:
Here’s the code I have. I changed the descriptions on the #defines to my understanding. I changed ‘myServo.write’ to ‘myServo.slowmove’, and I removed the ‘wait’ commands attached to those because it wouldn’t compile with them.

I don’t mind the LCD going off momentarily while door open/closes. What is the point of the ‘wait’?

The newer library I linked to used the .write() command to do the slowmove (if a speed was specified) and the wait flag stopped other processing, like reading the switch, until the door finishes it's movement. Otherwise the code above should function the same as what I posted.

The only caution might be that the original variable speed servo library was written prior to Arduino 1.0. There’s some minor change needed to update the library (if it hasn’t been done). But since it compiled … I’ll assume it was updated.

Ah, I see. I downloaded the linked, updated varspeedservo you posted. I don’t think I’ll use the wait command, I’l make it so you have to hold the button for a sec though.

The servo mod works. I’m going to wrap up my other mods before finalizing it (like putting my acrylic window back in). It feels a bit flimsy, that’s probably because my side panel is ~90% gone with such a huge window, should be much sturdier with the acrylic in it. It moves like 20* before actually opening so there’s a lot of give here and there. I think the ball link is probably a bad idea and an ez connector is the best way to go.

It does this annoying thing where, when plugging in, it slightly opens/closes the panel shut really fast. I guess it wont matter when it’s on a UPS and got a heavier frame though.

Something like this:

#include <VarSpeedServo.h>  //variable speed servo library

//declare the constants to be used
#define LEDPIN 13            //pin attached to led
#define servoPIN 10          //Servo control line 
#define SWitchPIN 12         //input from N.O. momentary switch

#define posOpenCMD 1900     //Define Clockwise Servo Limit in usec
#define posCloseCMD 600     //Define CounterClockwise Servo Limit in usec
#define DBdelay 200          //Duration of each button press in ms
#define MAXcnt 3            //Count of Button Presses needed for action
#define fastSPD 100         //speed setting; 1 = slowest, 255 is fastest (???)
#define slowSPD 15          //Speed Servo/Door opens and closes (15 is good average)

//declare the variables used
boolean wait = true;        //wait for motion to complete, or don't
boolean doorOPEN = false;   //desired door state set to closed
boolean SWstate = true;     //state of switch, open = TRUE = not pushed
byte SWcnt = 0;             //counter of same consecutive switch readings w/button = pressed

VarSpeedServo myServo; //create a servo object

void setup() {
  //set the pins to be ins or outs
  pinMode(LEDPIN, OUTPUT);
  digitalWrite(LEDPIN, LOW);  //turn LED off
  pinMode(servoPIN, OUTPUT);
  pinMode(SWitchPIN, INPUT_PULLUP);

  //CMD the servo to close the door
  myServo.attach(servoPIN);   // Attach the servo
  myServo.slowmove(posCloseCMD, fastSPD); // Tell servo to go to closed position
}

void loop() {
  //read the switch
  if(digitalRead(SWitchPIN) == LOW){   //see if switch is being pushed
    SWcnt ++;                //SW reads low = pushed, increment debounce counter
    if(SWcnt >= MAXcnt){     //test if switch is really being pushed AND done bouncing
      SWcnt = 0;             //done bouncing, reset counter for next push
      doorOPEN = !doorOPEN;  //reverse desired door state
      if(doorOPEN == true){
        digitalWrite(LEDPIN, HIGH);   //turn LED on
        myServo.slowmove(posOpenCMD, slowSPD);    //tell servo to go to open position
      } 
      else {
        digitalWrite(LEDPIN, LOW);    //turn LED off
        myServo.slowmove(posCloseCMD, slowSPD);   //tell servo to go to closed position
      }
    }
  } 
  else {
    SWcnt = 0;              //switch is released or bouncing
  }
  delay(DBdelay);           //wait for next read of switch
}

I think I’m going to have the arduino micro visible, with sleeved cables going to it. I’ll have to get some dupont connectors.

The code you linked above doesnt work, I tried it because normal adafruit servo control sucks and is jittery and too fast and I remember varspeed was really nice control.

I changed all myServo.write(…) to myServo.slowmove because I think that’s what varspeed uses instead of write, but then after fixing(?) those I get:

C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In function 'void handle_interrupts(timer16_Sequence_t, volatile uint16_t*, volatile uint16_t*)':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:91: error: 'LOW' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:91: error: 'digitalWrite' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:122: error: 'HIGH' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:122: error: 'digitalWrite' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:126: error: 'clockCyclesPerMicrosecond' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In function 'void __vector_3()':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:139: error: cannot convert 'volatile uint8_t*' to 'volatile uint16_t*' for argument '2' to 'void handle_interrupts(timer16_Sequence_t, volatile uint16_t*, volatile uint16_t*)'
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In function 'void initISR(timer16_Sequence_t)':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:185: error: 'TCCR1A' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:186: error: 'TCCR1B' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:193: error: 'TIFR1' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:194: error: 'TIMSK1' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: At global scope:
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:266: error: 'boolean' does not name a type
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In constructor 'VarSpeedServo::VarSpeedServo()':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:283: error: 'clockCyclesPerMicrosecond' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'uint8_t VarSpeedServo::attach(int, int, int)':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:297: error: 'OUTPUT' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:297: error: 'pinMode' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:304: error: 'isTimerActive' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::detach()':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:315: error: 'isTimerActive' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::write(int)':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:326: error: 'map' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::writeMicroseconds(int)':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:334: error: 'byte' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:334: error: expected `;' before 'channel'
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:335: error: 'channel' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:343: error: 'clockCyclesPerMicrosecond' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::slowmove(int, uint8_t)':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:377: error: 'map' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:380: error: 'byte' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:380: error: expected `;' before 'channel'
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:381: error: 'channel' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:388: error: 'clockCyclesPerMicrosecond' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'int VarSpeedServo::read()':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:407: error: 'map' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'int VarSpeedServo::readMicroseconds()':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:414: error: 'clockCyclesPerMicrosecond' was not declared in this scope

Looks like the Arduino IDE can’t find the VarSpeedServo files. Make sure they’re in a file named “VarSpeedServo” (spelling and cap sensitive) and that folder is in your libraries folder.

I have no idea what to do. I can’t get it to work. So the trinket tutorial on adafruit said to download like this special IDE they did, I followed all their steps, and I can’t get varspeed servo working.

So I check documents → arduino → libraries, and add varspeedservo folder there. I also go to downloads (where the trinket-arduino-ide is), windows->libraries and add it there too, where like a bunch of stock libraries are (adafruit neopixel, adafruit softservo, esplora, ethernet, gsm, liquidcrystal, servo, etc).

C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In function 'void __vector_3()':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:160: error: cannot convert 'volatile uint8_t*' to 'volatile uint16_t*' for argument '2' to 'void handle_interrupts(timer16_Sequence_t, volatile uint16_t*, volatile uint16_t*)'
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp: In function 'void initISR(timer16_Sequence_t)':
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:206: error: 'TCCR1A' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:207: error: 'TCCR1B' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:214: error: 'TIFR1' was not declared in this scope
C:\Users\Evan\Documents\Arduino\libraries\VarSpeedServo\VarSpeedServo.cpp:215: error: 'TIMSK1' was not declared in this scope

This is using the varspeedservo you posted.

Could be that the VarSpeedServo library uses a timer that doesn’t exist in a Tiny85 based Trinket. I’d have to look at the code but the eror messages certainly suggest that. You can slowdown the servo movement w/o using that servo library, I think I even posted a version that did that way back when. Given the Trinket is doing little else but moving the door, I don’t see why it wouldn’t work.

Instead of simply doing the servo command with position and speed arguments, use the regular servo command* inside of a loop that increments the command actually sent to the servo 1 deg every XYZ msecs until the servo command equals the desired position.

*IIRC there was some other mod to the code needed to make servos run on a Trinket. See this;

https://learn.adafruit.com/trinket-gemma-servo-control

BTW while I think I understand the reason behind the split of functions in separate Micro and Trinket MCUs, wouldn’t it have been easier to add a hardware reset to the (working) Micro and code so that whenever the PC was re-powered it also rebooted the Micro ?

It’s just the trinket uses a different servo library than normal arduinos.

wouldn’t it have been easier to add a hardware reset to the (working) Micro and code so that whenever the PC was re-powered it also rebooted the Micro ?

The issue was basically the battery implementation since the computer/usb wouldn’t recognize it without it also supplying the power. We talked about attaching the cell phone battery to VIN but for some reason or another I think that wasn’t going to work out smoothly, ie the arduino switching from usb power to battery power. I think it had to do with voltage regulation?

Someone suggested I used a powered usb hub, which would hook up to the pc and be powered by the usb battery, but well it became simpler, cheaper, and easier just to use a second MCU especially since I already had the trinket… or so I thought.

Belial88:
… or so I thought.

I think it not that big a deal to halve the functions and Trinket-ize the door open/closing. I have that code 50% done now. I have to tend to mending my Mini's supercharger in the AM so watch this space in the PM tomorrow.

See if this will compile for a Trinket, after you’ve installed the Adafruit software servo library. I took the Adafruit code from the Trinket URL above and left the timer setup and ISR parts. Then I added code from the prior servo build. You’ll need to fine tune the time(s) and the open and close position commands, now in degrees. I think there’s a way to tune the pulsewidths assigned to those commands if needed. Check/change the pins used if needed.

/*******************************************************************
  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 SERVO1PIN 0        //Servo control line (orange) on Trinket Pin #0
#define SWitchPIN 1        //Momentary switch to ground when pushed to open/close door
#define posOpenCMD 170     //Define Clockwise Servo Limit in deg
#define posCloseCMD 0      //Define CounterClockwise Servo Limit in deg
#define fastTime 6800      //time to quickly open/close door, min of 3600 msec
#define slowTime 15000     //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(SERVO1PIN);   // 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);
    }
  }
}