Hi All,
Newbie here, and using the lockdown time to try and learn some new stuff.
Id like to control a servo using keys on my keyboard.
Press Q it goes to 180 deg
Press A it goes to 0 deg
nothing pressed it sits at 90 deg
Ive tried using the Switch / Default, and the If Else but each time i add it it doesnt work as expected.
Any help greatfully received as to what i should be using ?
Heres my simple code so far which works, aparat from the self centering, Ive spent 2 days experimenting, but thought it was time to ask the experts.
I did manage to use a default function, but it was really jittery as if it was conflicting with itself.
Thanks in advance.
John
#include<Servo.h> // include server library
Servo ser; // create servo object to control a servo
int val; // initial value of input
void setup() {
Serial.begin(9600); // Serial comm begin at 9600bps
ser.attach(9);// server is connected at pin 9
}
void loop() {
if (Serial.available()) // if serial value is available
{
val = Serial.read();// then read the serial value
if (val == 'q') //if value input is equals to d
{
ser.write(180);// the servo will move according to position
delay(15);//delay for the servo to get to the position
}
if (val == 'a') //if value input is equals to a
{
ser.write(0);// the servo will move according to position
delay(15);//delay for the servo to get to the position
}
}
15 milliseconds is a very short time for a servo to slew 180 degrees.
Your spec mentions ’Q' and 'A', but your code uses lower-case.
And I see no code that centers when nothing is pressed.
How do you determine that nothing is pressed? Perhaps some specified duration of time after something IS pressed?
If so, you need to note the time when something is pressed and after that specified duration, if nothing else has been pressed, you could then center the servo.
Something like
Center the servo in setup
Set the last-updated time to now
In the loop
If you have a q
move the servo to 180
Set the last-updated time to now
If you have an a
move the servo to 0
Set the last-updated time to now
If now - last-updated is more than 5 seconds center the servo
ive just been looking at the key pressed and key released function, but only just started looking to see if that might solve it.
So it could be
Starts at 90
key pressed goes to 180
key released goes to 90
etc..
I will have to look at how to input time to it but thanks for the idea.
ive just been looking at the key pressed and key released function,
What have they got to do with Serial?
i was looking different options to solve the problem, it made sense that if i could send a command when the button was pressed, and then send another when released it might work how i wanted it.
Bit out of my comfort zone here, but trying to learn 
The clue here is that Serial doesn't have key pressed/released methods.
@roamingrobots
Use some of this free time you have to get to the following links.
Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.
Continued cross posting could result in a time out from the forum.
Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.
sorry i realised i put it in the wrong place ill delete the other one
"Any other suggestions?"
Well, continue to experiment and test using the serial monitor. You will probably need some type of terminal program on the device with the key board to send repetitive character commands from a depressed key. Below is some servo test code that uses characters sent from the serial monitor that might give you some ideas.
//send a character a, b, c, d or string of characters
//like aaaaaaaaaaabbbbbbccccccddddddddddddr
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int pos1 = 90;
int pos2 = 90;
int delayPeriod = 50; // increasing this slows down the servo movement
char c;
void setup()
{
Serial.begin(9600);
myservo1.attach(8); // attaches the servo on pin 8 to the servo object
myservo2.attach(9); // attaches the servo on pin 9 to the servo object
myservo1.write(pos1); // center the servo1
myservo2.write(pos2); // center the servo2
}
void loop()
{
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
Serial.print("received character is ");
Serial.println(c);
//}
if (c == 'a')
{
// in steps of 1 degree
if ( pos1 > 0)
--pos1;
myservo1.write(pos1); // tell servo to go to position in variable 'pos'
Serial.print("servo 1 is at ");
Serial.println(pos1);
//delay(delayPeriod);
}
if (c == 'b')
{
if ( pos1 < 180)
++pos1;
myservo1.write(pos1); // tell servo to go to position in variable 'pos'
Serial.print("servo 1 is at ");
Serial.println(pos1);
//delay(delayPeriod);
}
//
if (c == 'c')
{
// in steps of 1 degree
if ( pos2 > 0)
--pos2;
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
Serial.print("servo 2 is at ");
Serial.println(pos2);
//delay(delayPeriod);
}
if (c == 'd')
{
if ( pos2 < 180)
++pos2;
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
Serial.print("servo 2 is at ");
Serial.println(pos2);
//delay(delayPeriod);
}
//
if (c == 'r')
{
myservo1.write(90);
myservo2.write(90);
Serial.println("servos reset at 90 deg.");
}
}
}
It would be easy enough to do if you don't mind writing a program at the PC end using something like Processing. But I can't think of any existing PC program that does what you need.
It would also work if you used an IR Remote because they send continuation codes as long as a key is pressed down.
Steve
It would be easy enough to do if you don't mind writing a program at the PC end using something like Processing. But I can't think of any existing PC program that does what you need.
You could also make it work if you used an IR Remote instead of a PC keyboard because they send continuation codes for as long as a key is pressed down.
Steve