attiny 2313 and servos

Hi

I’m trying to build a useless machine with arduino as a host (and IDE) and an attiny 2313 as target.

The code is very simple as it just moves the servos and checks for a switch, but I have couple of problems:
a) 2313 doesn’t have a 16-bit timer, so I can’t use the standard servo library
b) 2313 only has 2k flash, so using the SoftwareServo in even the most basic sketch uses all of it
c) I cannot buy attiny84 or attiny85, I’m stuck with 2313 :frowning:

so, since the code is very simple, I tried controlling the servos myself, like this:

// servo = pin where to write
void moveServo(byte servo, int d) {
  for (byte i=0; i < 10; i++) {
    digitalWrite(servo, HIGH);
    delay(d);
    
    digitalWrite(servo, LOW);
    delay(20 - d);
  }
}

I’m using an SG-5010 which has Pulse Cycle: 20 ms and Pulse Width: 600-2400 µs

That code works fairly ok, when I supply d = 1000 (for 0°) and d = 2000 (for 180°), but I noticed that the servos don’t actually reach the whole range, they don’t get to 0° or 180°

So I tried another version, more accurate:

// servo = pin where to write
void moveServo(byte servo, int d) {
  for (byte i=0; i < 10; i++) {
    digitalWrite(servo, HIGH);
    delayMicroseconds(d);
    
    digitalWrite(servo, LOW);
    delayMicroseconds(20000 - d);
  }
}

with values d = 600 (for 0°) and d = 2400 (for 180°) as per the specs of my servo, but I found that it works even worse than the milliseconds version…

also I noticed that the number of times that I send the pulse to the servo also alters the angle, in the current code I’m iterating 10 times, i.e. sending 10 pulses to the servo, if I send more (say, 30), then the angles are more narrow.

Any hints or tips on how should I face this challenge? Suggestions on the code?

Thanks in advance

tylernt has made a servo library for tinys:

http://forum.arduino.cc/index.php?topic=198337.msg1463365#msg1463365