Hello, i am currently trying to make an Arduino rc car using 4x TT motors, an l298n motor controller, an Arduino mega, uno and a pair of hc12 transceivers. Both HC12 are or the same settings (default).
However i have an issue, the basic code to test a HC12 works, i can send "hello world" from the master to the slave however when i try to send data to the Arduino mega (slave) it will only receive data if the Arduino unos code has a 2 second delay in the loop.
Even with this, the data received by the master is inconsistent and sometimes takes up to 8 seconds to update. Does anyone know where i am going wrong here? Thanks in advance.
The Slave code is as follows:
//MOTOR 1
int motor1pin1 = 2;
int motor1pin2 = 3;
//MOTOR 2
int motor2pin1 = 4;
int motor2pin2 = 5;
void setup() {
Serial.begin(9600); //GOTTA DO THIS CUZ REASONS...
//DECLARING SPECIFIC MOTOR PINS AS OUTPUTS
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
}
void loop() {
//DIRECTIONAL COMMANDS TO MOTORS
//FORWARD MOVEMENT
if(Serial.available() > 0)
{
String input = Serial.readString();
if(input == "forward")
{
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
}
}
//BACKWARD MOVEMENT
if(Serial.available() > 0)
{
String input = Serial.readString();
if(input == "backward")
{
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
}
}
//LEFT TURN
if(Serial.available() > 0)
{
String input = Serial.readString();
if(input == "left")
{
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
}
}
//RIGHT TURN
if(Serial.available() > 0)
{
String input = Serial.readString();
if(input == "right")
{
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
}
}
//IDLE (IF ALL BUTTONS ARE PRESSED)
if(Serial.available() > 0)
{
String input = Serial.readString();
if(input == "idle")
{
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
}
}
}
The Master Code:
``
int forwardbutton = 2;
int backwardbutton = 3;
int leftbutton = 4;
int rightbutton = 5;
void setup() {
Serial.begin(9600);
pinMode(forwardbutton, INPUT);
pinMode(backwardbutton, INPUT);
pinMode(leftbutton, INPUT);
pinMode(rightbutton, INPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
pinMode(2, INPUT_PULLUP); //Sets pin 2 to a default HIGH
pinMode(3, INPUT_PULLUP); //Sets pin 3 to a default HIGH
pinMode(4, INPUT_PULLUP); //Sets pin to a default HIGH
pinMode(5, INPUT_PULLUP); //Sets pin 7 to a default HIGH
forwardbutton = digitalRead(2);
backwardbutton = digitalRead(3);
leftbutton = digitalRead(4);
rightbutton = digitalRead(5);
if((forwardbutton == LOW)&&(backwardbutton == HIGH)&&(leftbutton == HIGH)&&(rightbutton == HIGH)){
Serial.println("forward");
delay(2000);
}
if((forwardbutton == HIGH)&&(backwardbutton == LOW)&&(leftbutton == HIGH)&&(rightbutton == HIGH)){
Serial.println("backward");
delay(2000);
}
if((forwardbutton == HIGH)&&(backwardbutton == HIGH)&&(leftbutton == LOW)&&(rightbutton == HIGH)){
Serial.println("left");
delay(2000);
}
if((forwardbutton == HIGH)&&(backwardbutton == HIGH)&&(leftbutton == HIGH)&&(rightbutton == LOW)){
Serial.println("right");
delay(2000);
}
if((forwardbutton == HIGH)&&(backwardbutton == HIGH)&&(leftbutton == HIGH)&&(rightbutton == HIGH)){
Serial.println("idle");
delay(2000);
}
if((forwardbutton == LOW)&&(backwardbutton == LOW)&&(leftbutton == LOW)&&(rightbutton == LOW)){
Serial.println("idle");
delay(2000);
}
}