Adafruit Trinket, Please Help Me...

I'm not much of a programming or even electrical person. I'm making some Nike Back to the Future Mags and I'm using a 3V trinket to power a high torque micro servo.

I have the Adafruit library installed, but the tutorial they provided uses a potentiometer and I want to use buttons to turn the motor one way and the opposite direction. I also want to add these LED's I soldered into the mix and to only turn on when one of the buttons are pushed.

I have spent now a total of 3 days and about 20 hours on this dilemma. I think the wiring is correct but I'm not sure. Every time I upload the sketch the servo twitches about 20 degrees back and forth in a random pattern. Please, please help me. I included some nasty code that I've tried to piece together from random Adafruit tutorials and some pictures of what I'm working with.

Thanks

sketch_may28a.ino (2.83 KB)

IMG_2329 (1).jpg

IMG_2327 (1).jpg

IMG_2328 (1).jpg

IMG_2330 (1).jpg

The code you posted doesn't compile (doesn't even have a loop() function)... If you have code that makes the servos twitch, you've gotten further than that, and you should post that code (preferably using code tags.)

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

#define SERVO1PIN 0   // Servo control line (orange) on Trinket Pin #0

#define sw1_pin 1

#define sw2_pin 2

Adafruit_SoftServo myServo1, myServo2;  //create TWO servo objects


volatile boolean sw1 = false;
volatile boolean sw2 = false;

uint8_t sw1ButtonState = 0;
uint8_t sw2ButtonState = 0;

uint8_t lastsw1ButtonState = 0;
uint8_t lastsw2ButtonState = 0;


   
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!)
  
  myServo1.attach(SERVO1PIN);   // Attach the servo to pin 0 on Trinket
  myServo1.write(90);           // Tell servo to go to position per quirk
  delay(15);                    // Wait 15ms for the servo to reach the position
}

void checkIfSw1ButtonIsPressed()
{
    sw1ButtonState   = digitalRead(sw1_pin);
  
    if (sw1ButtonState != lastsw1ButtonState)
  {
    if ( sw1ButtonState == 0)
    {
      sw1=true;
    }
    delay(50);
  }
   lastsw1ButtonState = sw1ButtonState;
 }

void checkIfSw2ButtonIsPressed()
{
    sw2ButtonState   = digitalRead(sw2_pin);
  
    if (sw2ButtonState != lastsw2ButtonState)
  {
    if ( sw2ButtonState == 0)
    {
      sw2=true;
    }
    delay(50);
  }

// 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();
  }
}

Sorry, I've made a lot more progress now. That code was straight garbage. Here's something a bit better. I can get the LED to turn on while the button is pressed but I can't get the servo to move at all now.

#include <Button.h>       //  Is this being used?
int BUTTON = 2;           //  Button info in pin 2
int pos = 0;              //  Variable to store the servo position
int light = 1;           //  LED in pin 1

#include <Adafruit_SoftServo.h>
#include <Adafruit_TiCoServo.h>
#define SERVOPIN 0        //  Creates servo

Adafruit_SoftServo Lace;  // Naming servo instead of servo1


void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(1, OUTPUT);
  Lace.attach(0);
  digitalWrite(2, LOW);
}

void loop() {
   //******************** Wait for tightening button to be pressed ************************
  
  int LED = digitalRead(2);     //LED
  if (LED == HIGH) {            //LED
    digitalWrite(light, LOW);   //LED
  } else {                      //LED
    digitalWrite(light, HIGH);  //LED
  }

  if (digitalRead(2) == LOW) {              // Here down is all for the servo
    for (pos = 0; pos < 180; pos += 180){
      Lace.write(pos);
      delay(100);
    }
    if(digitalRead(2) == HIGH)
      for(pos = 180; pos >= 180; pos -= 90)
      {
        Lace.write(pos);
        delay(100);
      }
    }
}

You removed the timer code that actually updates the servo output signals. You need that for them to do anything...

// 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();
  }
}

Sorry, I should've posted this a while ago, but I figured it out.

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


#define SERVO 0       // Servo control line (orange) on Trinket Pin #0
#define SWITCH1 1     // Button 1 on Trinket Pin #1 (Analog 1)
#define SWITCH2 2     // Button 2 on Trinket Pin #2 (Analog 0)
#define LED 3         // LED on Trinket Pin #3 (Analog 3)

Adafruit_SoftServo servo;

const int speed = 5;

void setup() 
{  
 servo.attach(SERVO);   // Attach the servo to pin 0 on Trinket

 pinMode(SWITCH1, INPUT_PULLUP);     // Initialize Trinket Pin #1 as an input with pullup
 pinMode(SWITCH2, INPUT_PULLUP);     // Initialize Trinket Pin #2 as an input with pullup

 pinMode(LED, OUTPUT);               // Initialize the LED pin as an output
} 

void loop() 
{  
 if (! digitalRead(SWITCH1)) {  // if the button is pressed
 servo.write(speed);      // Setting servos in forward motion.
 servo.refresh();
 delay(15);  
 
 }

 if (! digitalRead(SWITCH2)) {  // if the button is pressed
 servo.write(-speed);      // Setting servos in backward motion.
 servo.refresh();
 delay(15);  
 }

 digitalWrite(LED, HIGH);
 
}