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();
}
}