The secret of the servo control

To use a servo with the Arduino, you have two possibilities. You can use the servo library, or you control the servo with your own code. But to write your code, you need to know the basics, how a servo works.
There are two facts: The cycle or refresh time and the pulse width.

The cycle or refresh time.
It’s not enough to send a position value to the servo. You must do this over and over again! If you don’t do this the servo forget the actually position and start to move. The normal cycle or refresh time is 25 ms (some sources in the internet say 20 ms). Each 25 ms you must send the position value.

The pulse width.
With the pulse width you tell the servo the position you want.
The longer the pulse, the longer moves the servo. Different servos has different values, but they are similar.
The path of movement is left (short way)- middle – right (long way). So are also the pulse width values. You reach the left position with 500 µs, the middle position with 1500 µs and the right position with 3000 µs pulse width.
The pulse width to reach the middle position is always 1500 µs. But the other two values are different, depending of the type of servo. So it’s possible you starts with 700 µs and ends with 2700 µs or earlier. You should test it.



The HC-SR04 problem is solved!

Ok, a look at the HC-SR04 datasheet explains and solves the problem.

The speed of ultra sonic is 343 m/s. To measure a distance from 1m, it’s need round about 6 milliSeconds (actually there are two meter, one meter to the destination and one meter back).

So we need a special time for one measurement cycle and we are not able to make as many measurements per second as we want.
The HC-SR04 datasheet shows us the measurement cycle and the cycle time.

The cycle time must be less than 16 Hz. This is not possible with Timer0 and Timer2. Both timer are 8 Bit, and with a prescaler of 1024 we get as slowest frequence 61 Hz (16.000.000 / 1024*256). Therefore we need to use Timer1 if we want to measure permanently.

Here an example for a measurement function:

void Eyes_measurement() {
  digitalWrite(Eyes_Trigger,LOW);        // to be sure the Trigger is down
  digitalWrite(Eyes_Trigger,HIGH);       // start a measurement
  pulsdauer = pulseIn(Eyes_Echo,HIGH);   // get the result
  distance = pulsdauer / 58;             // value in cm

A bad day yesterday!
I tried to optimize the “The little One” code, but it was not successfully. I got a timing problem. To use the interrupt timer2 for the ultra sonic, the frequence is to high, also with a prescaler of 1024. But timer 1 is current in use …