Move only while button is pressed

I have created my first robot As it is now I can send letter command( W A S D) to make it move or send P and have it enter obstacle avoidance mode. This is fine but it moves is continuously until I tell it to stop if I send a movement command(ie w moves forward until I send z to make it stop). I would like it to move while I am pressing the key only and stop when nothing is being pressed(ie hold w and it move, let go and it stop. I have no idea how to accomplish this and I am not having much luck ascertaining it from the internet. Couldn't post code, too long . Any help would be greatly appreciated.

Without your code we can’t be of much help. I suggest you look at the while function.

You can attach code as an attachment.

How to use this forum

OK, wasn’t sure y’all would even need the code, though there was just a known method to use. Code is attached

thebot_code_2_bluetooth.ino (9.85 KB)

You are referring to the serial port then when you mention "button is pressed"?

There is no way of knowing when the button is released in this case. If you attached switches to the robot, yes that would be easy.

When you say switches do you mean physical switches? I'm really looking for a way to do it remotely

AgentNoise: When you say switches do you mean physical switches? I'm really looking for a way to do it remotely

In the past I made web pages (below) that had buttons to operate servos that operated based on mousedown and mouseup events. When the cursor was over a button and the mouse left button depressed, a command was sent to the web server,.When the cursor was over the button and the left mouse button released, a different command was sent to the web server. Used to start/stop continuous rotation servos and standard servos.

OK, so while the PC keyboard encodes "key pressed" and "key released" to the operating system, as well as regularly repeating the "key press" code as long as the key is pressed, the operating system of the PC (or in general, whatever computer it is) generally interprets that into an initial character code which will be repeated at a set rate after a set initial delay.

A terminal monitor program may then feed these character codes in turn to the serial port or may (as the Arduino Serial Monitor does,) buffer them and send them all when "Enter" is pressed.

Your problem then is not so much what to do on the Arduino robot, but how the PC interprets your keypresses. Assuming you have an immediate "terminal" program which sends every keypress directly to the serial (USB) port, then you could have your robot interpret each character as a command to move in that direction for a given time - say a quarter of a second - so that a stream of the same character at lesser intervals - generated by holding a key down - will produce a continuous travel.

The consequence is of course, that it takes that length of time for the robot to stop after the key is released, and you have to be ready to accept the next command code before completing the last, in order to ensure that ontinuity.