I am relatively new to robotics, I am having some trouble with my code. I can press a key and the robot moves in a certain direction, however i want to be able to hold down a specific key it sets the robot in motion so that when i release the key it stops. Here is my code so far, it is obviously getting stuck in an endless loop I'm just not sure how to break from the loop. Any advice would be great! Hopefully this is in the right topic I'm sorry if it is not!
void loop()
{
char ch;
while(mySerial.available())
{
ch = mySerial.read();
while (ch == 'w')
{
forward(); // function to set servo wheels forward
if ( ch != 'w')
{
stopServo(); //function to stop servo
break;
}
}
while (ch == 's')
{
backward();
if ( ch != 's' )
{
stopServo();
break;
}
}
while (ch == 'a')
{
left();
if ( ch != 'a' )
{
stopServo();
break;
}
}
while (ch == 'd')
{
right();
if (ch != 'd')
{
stopServo();
break;
}
}
}
}
I can press a key and the robot moves in a certain direction, however i want to be able to hold down a specific key it sets the robot in motion so that when i release the key it stops.
What is sending the data? The Serial Monitor does not have the ability to keep sending the same key over and over.
You are better off having whatever application send a character once when the key is pressed, and some other character when the key is released, You have 26 pairs of characters that could be sent that way (a/A, b/B, etc.).
while (ch == 'w')
{
forward(); // function to set servo wheels forward
if ( ch != 'w')
{
stopServo(); //function to stop servo
break;
}
}
The while loop does not cause a change in the value of ch, so the inner if test will never be true, and the while loop will never end. (Unless there is something in forward() that changes ch, which you didn't show us.)
My original code just used if statements to send the robot in motion. The library i use for the serial monitor is NewSoftSerial, I use xbees to communicate the serial data. For the serial monitor i just use PUTTY or teraterm. I'm not sure how to have it send another character when I release the key...
For the serial monitor i just use PUTTY or teraterm.
Do these send the same value over and over when a key is pressed? How often?
No that any of that matters as once you start a while loop because a character was received, you never read again. You just expect to somehow KNOW that that character is no longer arriving, even though you aren't reading the serial port.
Could I read in from the serial monitor again in the while loop? I believe it repeats the value when a key held down. I'm not sure if there is a way to change this (perhaps it is impossible to do serially?)
Could I read in from the serial monitor again in the while loop?
Of course.
I think that you might be better off writing your own application on the PC that reads key-press and key-release events, and sends data (upper case for key press; lower case for key release) to the serial port. Processing could be used, or C# or other languages - whatever you know and like.