I'm trying to do a basic project servos controlled by keyboard at different timing and speed.
the idea is control different behavior of servos trough the key board.
my only correct approximation is these sketch that runs with delays. (attached at the end)
the big problem here is: If I send a new input before one of the tasks is finish. The new task don't would start until the end of the previous task.
I know that use delays is not the correct way to do this, maybe use millis and put the things in a class.
but when I follow some tutorials neither of them work for me. When I try to initialize the action with the signal from the serial in this case the keyboard. everything is a mess.
I already tried to modify my sketch based on this examples but without success:
Thanks for any advice this is my forth day in the research
//control multiple servos using keyboard keys (1,2 for servo1 atached to pin 15
and key 5,6, for servo atached to pin 13) servos could be continous rotation o standard servo, speed of rotation could be diferent/
#include <Servo.h>
Servo servo1; //regular servo
Servo servo2; // continous rotation
Servo servo3;
Servo servo4;
int val = 0; //Variable input serial
void setup() {
Serial.begin(115200); //serial
servo1.attach(15); //regular servo
servo2.attach(13); //continous rotation servo
//servo3.attach(12); //optional servos
//servo4.attach(16); // optional servos
servo1.write (90); //position 90 degrees
servo2.write (90); // initial speed = 0
//servo3.write (90); // optional servos
//servo4.write (90); // optionalservos
}
void loop() {
//////////serial configuration///////
if(Serial.available() >= 1) //Detect input serial
{
val = Serial.parseInt(); //save as int
//////////serial configuration///////
//////////Main Functions///////
if(val == 1)
{
int pos = 0;
int dtwait = 3; // control servo speed
for (pos= 90; pos > 0; pos-=1){ // control servo position from 90 degrees to 0 degrees
servo1.write(pos);
delay(dtwait);
}
Serial.println("servo1: up");
}
else if(val == 2)
{
int pos = 0; //control servo continuos rotation////////
int dtwait = 3; // control servo time
for (pos= 90; pos > 0; pos-=1){ // control servo speed stop to 180 speed
servo2.write(pos);
delay(dtwait);}
servo2. write(90); // control servo position speed stop=90
Serial.println("servo2: up");
}
else if(val == 5)
{
int pos = 0;
int dtwait = 10; // control servo speed
for (pos= 0; pos < 180; pos +=1){ // control servo position from 0 degrees to 180 degrees
servo1.write(pos);
delay(dtwait);}
for (pos= 180; pos >= 90; pos -=1){ // control servo position from 180 degrees to 90 degrees and stop
servo1.write(pos);
delay(5); // control servo speed
}
Serial.println("servo 1 neutral");
}
else if(val == 6)
{
int pos = 0;
int dtwait = 10; // control servo time
for (pos= 0; pos < 180; pos +=1){ // control servo speed stop to 180 speed clockwise
servo2.write(pos);
delay(dtwait);} //time active
for (pos= 180; pos >= 90; pos -=1){ // control servo speed stop to 180 speed unclockwise
servo2.write(pos);
delay(5); // time active
}
Serial.println("servo 2 neutral");
}
}
}