1

I have been trying to get a servo to sweep using an attiny13A. And i managed to get the servo to (kindoff) work by the help of a previous thread. But i'm now getting an issue. The servo only goes to 180 degrees and does not return to 0 degrees, instead it just keeps trying to go further then 180 degrees eventhough its not mechanicly capabel of doing so. Anyone have any idea how i can solve this? ( I need it for a small animatronic part) (also the servo is a generic 9gram servo and i have tested it on multiple servos, its the same result) thanks in advance

#define F_CPU 9600000
int servo = PB1;  //connect servo to PB1
long angle;
int pwm;
int counter = 0;
int LED = PB3;
#include <avr/io.h>
void adc_setup (void) 
{
    // Set the ADC input to PB2/ADC1
  ADMUX |= (1 << MUX0);
  ADMUX |= (1 << ADLAR);
  // Set the prescaler to clock/128 & enable ADC
  // At 9.6 MHz this is 75 kHz.
  ADCSRA |= (1 << ADPS1) | (1 << ADPS0) | (1 << ADEN);
}
int adc_read (void)
{
  // Start the conversion
  ADCSRA |= (1 << ADSC);
  // Wait for it to finish
  while (ADCSRA & (1 << ADSC));
  return ADCH; // return the value
}
void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(servo, OUTPUT);  // set servo as output pin
  digitalWrite(LED, HIGH);
  delay(3000);
  pinMode(LED, LOW); //blink LED

}
void loop ()
{
 for(counter = 0; counter < 180; counter++)  //scan fril 0 degrees to 180 degrees
  {
    servoPulse(servo, counter);  // call the servoPulse function.   
    delay(15);                                              
  }                  

delay(1000);
  
 for(counter = 180; counter > 0; counter--)  // now scan back from 180 to 0 degrees  
  { 
    servoPulse(servo, counter);  // call the servoPulse function.    
    delay(15);      
 }
}
void servoPulse (int servo, int counter)
{
  pwm = (angle * 11) + 500;    // Convert angle to microseconds
  digitalWrite(servo, HIGH);
  delayMicroseconds(pwm);
  digitalWrite(servo, LOW);
  delay(50); // Refresh cycle of servo
}
1
  • 2
    I’m a little confused; servos require a pulse every 20ms (this depends on the servo; there’s a fair amount of variability) but you’re pulsing with a 65ms delay (delay in loop + delay in servoPulse) so my first guess would be the PWM signal is bogus—your overall pulse width is > 65ms, with the HIGHS signal a tiny fraction of that. I don’t see angle changed anywhere in the code, or initialized, and servoPulse ignores what I think you meant to use, counter. Jul 31, 2021 at 13:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.