A Trinket, a button and a servo. Looking for help making the

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jedimasta
 
Posts: 16
Joined: Sat Sep 05, 2015 5:44 pm

A Trinket, a button and a servo. Looking for help making the

Post by jedimasta »

Hoping for some help with a project I'm working on. I'm using a Trinket 3v, a couple of servos and a button to make a high(er) tech ventriloquist dummy out of a cheap craft store stuffed bird. I created a small circuit with the Trinket mounted on a board along with all the connections. I triple checked the connections with a multimeter, made sure the servos both worked and ran a bunch of simple sketches on the Trinket to make sure it's working as well.

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();
  }
}
and here's a fritzring: https://imgur.com/a/bYjqfXs

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.

User avatar
Berentis
 
Posts: 6
Joined: Fri Feb 17, 2017 4:46 am

Re: A Trinket, a button and a servo. Looking for help making

Post by Berentis »

Some things to look into:
1). Most servos run off of 4.8v to 6.0v. Going by your fritzing diagram, you are using a 3.7v lipo for power. If you are using this: https://www.adafruit.com/product/169 you might be ok.
2) PB3 and PB4 on the classic Trinkets are not recommended for input, as they are used for USB communications. Having your button on PB4 may run into issues reading it. I suggest using PB0 or PB1 instead. If you use PB1 for your mouth servo, you should be able to see the built-in LED get brighter or dimmer as you change the servo value.

See also this thread: viewtopic.php?f=52&t=83029&p=423791&hil ... il#p423791 as the issue with the servos sounds similar.

User avatar
Baconator
 
Posts: 23
Joined: Wed Sep 05, 2018 9:00 am

Re: A Trinket, a button and a servo. Looking for help making

Post by Baconator »

I see that in your code, you have the mouthButton as INPUT (and not INPUT_PULLUP), but in your fritzing, you don't have a pullup resistor in your circuit. If the fritzing is accurate, that might very well be part of the problem.

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”