Can you make an Arduino board have a refresh rate?

For example, you have a robot and you are setting the directions as w a s d but instead of typing w then hitting enter to go straight. Can you hold w to make it move and it stops on the key release?

Here is the code that I am working off of:

#include <L298N.h> // Here we are including a library of commands to help make controlling the motors with the L298N easier
// to install this library, go to tools>manage libraries> search for "l298n"> install the one by Lombardo

// Define arduino pins to be used in the program
// We must use 6 pins to connect to the L298N driver to control both motors.  
// The pin placement is only important for the ENA and ENB pins, which must be capable of PWM, indicated by a (~) on the Arduino

#define ENA 3
#define IN1 4
#define IN2 5
#define IN3 9
#define IN4 10
#define ENB 11

// Here we are defining the pins that connect to and receive the signal from the IR sensors
// The IR sensors will tell us if there is an obstacle in the direction that module is facing

#define irLeft 6
#define irFront 7
#define irRight 12

// We also use the RX and TX pins, however these do not need to be defined as they are dedicated transmit and receive pins
// Define variables to be used in the program
// In order for this program to work, we will have to use a number of variables to help us accomplish the intended functions

int left;
int right;
int front;
int turnTime = 500; // this will be the amount of time needed to make a 90 degree turn, determined experimentally
int current = 42;
int last = 43;
int receive = 0;
L298N motorLeft(ENA,IN1,IN2); // Defining the left motor and which pins control it
L298N motorRight(ENB,IN3,IN4); // Defining the right motor and which pins control it

// void setup() is a series of commands that run once to initialize the program
void setup() {
 Serial.begin(9600); // set serial baud rate to 9600 bits/sec, which is a standard data transfer rate
 motorLeft.setSpeed(110); // set the speed of the motor.  This can be a value between 0-255
 motorRight.setSpeed(110); // If the motor speeds are mismatched, consider increasing or decreasing these values independently until they match
 pinMode(irLeft,INPUT);
 pinMode(irRight,INPUT);
 pinMode(irFront,INPUT);
 pinMode(ENA,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);
 pinMode(ENB,OUTPUT);
}

// void loop is a loop that will run indefinitely (keep cycling through and repeating) as long as the Arduino is powered on.
void loop() {
 
// the following code gets readings from the IR sensors and updates the serial monitor when there is any change
// if the sensor detects an object, it will return a low value (0).  If it detects nothing, it will return high (1).
 left = digitalRead(irLeft);
 front = digitalRead(irFront);
 right = digitalRead(irRight);
 current = left+front+right;
 if(current != last){
   Serial.print("Left: ");
   Serial.print(left);
   Serial.print(".   Front: ");
   Serial.print(front);
   Serial.print(".  Right: ");
   Serial.print(right);
   Serial.println(".");
   last = current;
 }
 if(Serial.available() > 0){
   receive = Serial.read();
   Serial.println(receive);
 }
 // the arduino receives chracters based on the ascii table.  If you want to add more controls, look up the key on the ascii table and use that value in the code.
 // must hit key and "enter" to send command
 // w for go forward
 if(receive == 119){
   motorLeft.forward();
   motorRight.forward();
 }
 
 // a for 90 degree turn left
 if(receive == 97){
   motorLeft.forward();
   motorRight.backward();
   delay(turnTime);
   motorLeft.stop();
   motorRight.stop();
 }
 
 // d for 90 degree turn right
 if(receive == 100){
   motorLeft.backward();
   motorRight.forward();
   delay(turnTime);
   motorLeft.stop();
   motorRight.stop();
 }
 
 // x for manual stop
 if(receive == 120){
   motorLeft.stop();
   motorRight.stop();
 }

 // s for go backwards
 if(receive == 115){
   motorLeft.backward();
   motorRight.backward();
 }
}

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

The entire key up, key down behavior would be on your PC, not on the arduino. If you want something like that, it may be better that your program responds to a w by continuing to work until it gets another command to stop or something else.

Also, there is no need to use ascii values in your code. Using something like 'w' is way more obvious than 119.

if (receive == 'w' ) {
  //....
}

Is there a good way to make a key pressed and key released function?

cassarwolf:
Can you hold w to make it move and it stops on the key release?

That is a very common requirement but you may need to write your own PC program to achieve it. Your program would need to detect the key-down event and send (say) 'W' and when it detects the key-up event it can send 'w' (lower case). The ability to detect those events is a standard feature of most PC programming languages.

Sending a continuous stream of characters while a key is pressed is bad practice.

...R

noiasca:
don't use the built in serial monitor but another terminal program which sends the character after you press it - not only after you hit enter.

That still does not solve the key pressed, key released request. At most, it will transmit many characters if you hold down a key

exactly, and he can adopt the code: if he doesn't receive any characters any more to stop his bot.

"Is there a good way to make a key pressed and key released function?"

With the serial monitor, I press the action key with my left hand and the enter key with my right hand. Don't have to do any coding on the pc. Just chose easy action keys for the left hand.

Is a data logger shield even written faster than the serial communication ?
Or are there any other options transmitting the data quickly to the PC ?
Official Site

zoomkat:
With the serial monitor, I press the action key with my left hand and the enter key with my right hand. Don't have to do any coding on the pc. Just chose easy action keys for the left hand.

I don't think that sends one character when the action key is pressed and a different character when it is released.

cassarwolf:
Is there a good way to make a key pressed and key released function?

Using the Python programming language the TKinter library has the necessary functions to detect key presses. It is well documented and there are hundreds of online tutorials. For communicating with your Arduino this Simple Python - Arduino demo should get you started.

...R