Hello,
I found a code on your website to control a servo with an Arduino Uno witout using a library.
https://www.pololu.com/docs/0J57/8.a
This code is for one servo, I have two servo’s to control. I changed the code for two servo’s but it didn’t work. Any idea what I’m doing wrong?
Translation of a few words:
Servo rechts = Servo right
Servo links = Servo left
This is the code I made:
[code]//Servo rechts
#define SERVO_RECHTS 10
uint16_t volatile servoTime = 0;
uint16_t volatile servoHighTime = 3000;
boolean volatile servoHigh = false;
//Servo links
#define SERVO_LINKS 11
uint16_t volatile servoTime2 = 0;
uint16_t volatile servoHighTime2 = 3000;
boolean volatile servoHigh2 = false;
ISR(TIMER2_COMPA_vect)
{
//SERVO rechts
servoTime += OCR2A + 1;
static uint16_t highTimeCopy = 3000;
static uint8_t interruptCount = 0;
if(servoHigh)
{
if(++interruptCount == 2)
{
OCR2A = 255;
}
if(servoTime >= highTimeCopy)
{
digitalWrite(SERVO_RECHTS, LOW);
servoHigh = false;
interruptCount = 0;
}
}
else
{
if(servoTime >= 40000)
{
highTimeCopy = servoHighTime;
digitalWrite(SERVO_RECHTS, HIGH);
servoHigh = true;
servoTime = 0;
interruptCount = 0;
OCR2A = ((highTimeCopy % 256) + 256)/2 - 1;
}
}
//SERVO LINKS
servoTime2 += OCR2A + 1;
static uint16_t highTimeCopy2 = 3000;
static uint8_t interruptCount2 = 0;
if(servoHigh2)
{
if(++interruptCount2 == 2)
{
OCR2A = 255;
}
if(servoTime2 >= highTimeCopy2)
{
digitalWrite(SERVO_LINKS, LOW);
servoHigh2 = false;
interruptCount2 = 0;
}
}
else
{
if(servoTime2 >= 40000)
{
highTimeCopy2 = servoHighTime2;
digitalWrite(SERVO_LINKS, HIGH);
servoHigh2 = true;
servoTime2 = 0;
interruptCount2 = 0;
OCR2A = ((highTimeCopy2 % 256) + 256)/2 - 1;
}
}
}
void setup()
{
pinMode(SERVO_LINKS, OUTPUT);
digitalWrite(SERVO_LINKS, LOW);
pinMode(SERVO_RECHTS, OUTPUT);
digitalWrite(SERVO_RECHTS, LOW);
TCCR2A = (1 << WGM21);
TCCR2B = (1 << CS21);
TCNT2 = 0;
OCR2A = 255;
TIMSK2 |= (1 << OCIE2A);
sei();
}
void loop()
{
servoLinks(1500); //Between 1501 and 1700 is forward, 1500 is standing still
servoRechts(1500); // Between 1499 and 1300 is backward, 1500 is standing still
}
void servoLinks(uint16_t highTimeMicroseconds)
{
TIMSK2 &= ~(1 << OCIE2A);
servoHighTime2 = highTimeMicroseconds * 2;
TIMSK2 |= (1 << OCIE2A);
}
void servoRechts(uint16_t highTimeMicroseconds)
{
TIMSK2 &= ~(1 << OCIE2A);
servoHighTime = highTimeMicroseconds * 2;
TIMSK2 |= (1 << OCIE2A);
}[/code]
Regards
Chris
PS: Sorry for my bad English