Hello,
I was working on servo code developing and testing. I have two models for testing and I just have to choose one and implement in a project just to left a gate arm that is cardboard fabric so there's not any heavy load to the servo.
Until I was testing a function for multitasking, using millis function.
But I noticed even adding anything along with calling the function to drive the servo actually affects the behavior of the servo and it functions differently!
I tried the Servo library but the example code uses delay function, and trying without delay function, the servo works so fast and can't take full sweeps as I have to use delay function.
And of course delay function isn't optimal for multitasking code.
So, what to do then?
This is my testing code, there are a lot of commented functions in the loop function, which is my testing work.
My last testing work, as it's obvious, I'm working on the servo_toggle function. And I have commented everything in that function and only put two commands.
One is servo_pulse ... and the second line is Serial.print. Just to test how the function would behave if I added something, and my guess is right. It works differently!
#define SERVO1 3
#define SERVO2 13
#define IR_1 A0
#define POT A1
#define NICE_SPEED 200000
uint16_t pot_val,map_val;
uint32_t servo_st;
uint32_t servo_cr;
uint32_t servo_left = 1000;
uint32_t servo_center = 1750;
uint32_t servo_right = 2600;
uint16_t servo_speed = 1000;
byte servo_state;
byte servo_delay,toggle_state,toggle_lock;
void servo_radar(uint8_t pin, uint32_t servo_speed);
void servo_sweep(uint8_t pin);
void servo_pulse(uint8_t pin, uint16_t pwm);
void servo_gate_ir(uint8_t inpin, uint8_t servono);
void servo_toggle(uint8_t servono);
void setup() {
Serial.begin(9600);
pinMode(IR_1,INPUT);
pinMode(POT,INPUT);
pinMode(SERVO1,OUTPUT);
pinMode(SERVO2,OUTPUT);
servo_st = millis();
}
void loop() {
//servo_radar(SERVO2,NICE_SPEED);
//servo_radar(SERVO1,NICE_SPEED);
//servo_sweep(SERVO1);
/*pot_val = analogRead(POT);
Serial.println(pot_val);
map_val = map(pot_val,0,1023,500,2000);
servo_pulse(SERVO1,map_val);*/
//if()servo_pulse(SERVO1,500);
//servo_gate_ir(IR_1,SERVO1);
//servo_pulse(SERVO1,1000);
servo_toggle(SERVO1);
}
void servo_gate_ir(uint8_t inpin, uint8_t servono){
if(!digitalRead(inpin)){
servo_pulse(servono,500);
}
else{
servo_pulse(servono,1400);
}
}
void servo_toggle(uint8_t servono){
servo_pulse(servono,servo_left);
Serial.print("hi");
/*if(!toggle_state){
servo_pulse(servono,servo_left);
}
else{
servo_pulse(servono,servo_right);
}
*/
/*servo_cr = millis();
if(servo_cr - servo_st >= 1000){
toggle_state ^= 1;
servo_st = millis();
}*/
}
void servo_radar(uint8_t pin, uint32_t servo_speed){
for(uint32_t k=0;k<servo_speed;k++)
servo_pulse(pin,servo_left);
for(uint32_t k=0;k<servo_speed;k++)
servo_pulse(pin,servo_right);
}
void servo_sweep(uint8_t pin){
for (int i=servo_left; i<=servo_right; i+=10){
for(uint32_t k=0;k<servo_speed;k++)
servo_pulse(pin,i);
if(i>=servo_right){
for (int i=servo_right; i>=servo_left; i-=10){
for(uint32_t k=0;k<servo_speed;k++)
servo_pulse(pin,i);
}
}
}
}
void servo_pulse(uint8_t pin, uint16_t pwm){
if(!servo_state && !servo_delay){
digitalWrite(pin,HIGH);servo_state = 1;servo_st = micros();}
if(servo_state && !servo_delay){
servo_cr = micros();if(servo_cr - servo_st >= pwm){
digitalWrite(pin,LOW);servo_delay = 1;servo_st = micros();}}
if (servo_delay){
servo_cr = micros();if(servo_cr - servo_st >= 20000 - pwm){
servo_delay = 0; servo_state = 0;}}
}