controlling led light with HC 12 Transceiver

Hi I have A project in which I am controlling my 4wd robot car with hc-12 transceiver modules. But the problem is that i can only control my car with it. I want to upgrade this code to control the led light too with Hc-12 transmitter and receiver. transmitter is my joystick shield on which there are buttons that i want to use to control led lights on the receiver which is my 4wd Robot car.
Please Help

Here are the codes:-

transmitter

int xAxis, yAxis;

void setup() {
Serial.begin(9600); // Default communication rate of the Bluetooth module
}

void loop() {
xAxis = analogRead(A0); // Read Joysticks X-axis
yAxis = analogRead(A1); // Read Joysticks Y-axis

// Send the values via the serial port to the slave HC-05 Bluetooth device
Serial.write(xAxis/4); // Dividing by 4 for converting from 0 - 1023 to 0 - 256, (1 byte) range
Serial.write(yAxis/4);
delay(20);
}

Receiver
#define enA 2
#define in1 3
#define in2 4
#define enB 7
#define in3 5
#define in4 6

int xAxis, yAxis;
int x = 0;
int y = 0;

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

Serial.begin(9600); // Default communication rate of the Bluetooth module
}

void loop() {
// Default value - no movement when the Joystick stays in the center
xAxis = 510;
yAxis = 510;

// Read the incoming data from the
while (Serial.available() == 0) {}
x = Serial.read();
delay(10);
y = Serial.read();
delay(10);

// Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below
xAxis = x * 4;
yAxis = y * 4;

// Y-axis used for forward and backward control
if (yAxis < 470) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;
motorSpeedB = 0;
}

// X-axis used for left and right control
if (xAxis < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
int xMapped = map(xAxis, 470, 0, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
int xMapped = map(xAxis, 550, 1023, 0, 255);
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
// Confine the range from 0 to 255
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

So you need read the joystick buttons and transmit/receive their values then use them to switch the LEDs.

Do you know the button connections of your joystick shield? If you give some details of what Arduino and joystick shield you're using it will be a lot easier to help.

What have you tried so far? Please post your best attempt at adding buttons and LEDs and say what works and what doesn't.

Steve

the button connections on my joystick shield are on D2,D3,D4,D5,D6,D7

I did found a code to control led by using the buttons on joystick shield via Hc-12 transceiver modules but that code uses a special library which is not present in my code of the rc car control. Here below I have mentioned that code which can control the led using Hc-12 modules wirelessly.

Transmitter

//HC-12 Momentary Button Send
//Autor Tom Heylen tomtomheylen.com

#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); //TX, RX

int buttonPin = 2;
boolean onOff = 0;
void setup() {
pinMode(buttonPin, INPUT);
mySerial.begin(9600);
}

void loop() {

int buttonState = digitalRead(buttonPin);//read button state

if(buttonState == 0){//if button is down
mySerial.println(1111);//send unique code to the receiver to turn on. In this case 1111
onOff = 1;//set boolean to 1
}
if(buttonState == 1 && onOff == 0){//Verifier to send off signal once
mySerial.println(0000);//send unique code to the receiver to turn off. In this case 0000
}
delay(20);//delay little for better serial communication
}

Receiver

//HC-12 Momentary Button Receive
//Autor Tom Heylen tomtomheylen.com

#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); // TX, RX

int ledPin = 10;

void setup() {
mySerial.begin(9600);
pinMode(ledPin, OUTPUT);
}

void loop() {

if(mySerial.available() > 1){
int input = mySerial.parseInt();//read serial input and convert to integer (-32,768 to 32,767)
if(input == 1111){//if on code is received
digitalWrite(ledPin, HIGH);//turn LED on
}
if(input == 0000){//if off code is received
digitalWrite(ledPin, LOW);//turn LED off
}
}
mySerial.flush();//clear the serial buffer for unwanted inputs

delay(20);//delay little for better serial communication

}

YDHUyMpEHdd2oOTOtK41E7IV7E170J7OJIylS1Qa.jpg

SoftwareSerial isn't anything special. It just does what your code does with Serial. So you can merge the two sets of code taking out all references to SoftwareSerial and changing the references to mySerial into Serial and it will be pretty much what you need.

Give it a try first with just one button and when that works you can add other buttons/LEDs. If you have problems post your best attempt here and we help to it working (but please use </> code tags for posting your code - you've been around long enough to have had time to read "How to use this forum" which tells you how to do it properly).

Steve

I tried what you said to me and it worked up-to some extent. like after removing the "software serial" from my code the Led turning on code worked fine but when I tried to merge the two codes it seems like the two codes are Interfering with each other. means when the button is pressed on the transmitter side, the led turns on button side of my chassis wheel start moving continuously and won't stop and also the led won't turn off even after I have left the button on the transmitter side. Here i have provided the code that I have merged and also a video link to show you what problem I am facing.

Transmitter

int xAxis, yAxis;

int buttonPin = 2;
boolean onOff = 0;
void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() {
 
  int buttonState = digitalRead(buttonPin);//read button state
  
  if(buttonState == 0){//if button is down
    Serial.println(1111);//send unique code to the receiver to turn on. In this case 1111
    onOff = 1;//set boolean to 1
  }
  if(buttonState == 1 && onOff == 0){//Verifier to send off signal once
    Serial.println(0000);//send unique code to the receiver to turn off. In this case 0000
  }
  delay(20);//delay little for better serial communication

  xAxis = analogRead(A0); // Read Joysticks X-axis
  yAxis = analogRead(A1); // Read Joysticks Y-axis
  
  // Send the values via the serial port to the slave HC-05 Bluetooth device
  Serial.write(xAxis/4); // Dividing by 4 for converting from 0 - 1023 to 0 - 256, (1 byte) range
  Serial.write(yAxis/4);
  delay(20);
}

receiver

#define enA 2
#define in1 3
#define in2 4
#define enB 7
#define in3 5
#define in4 6

int xAxis, yAxis;
int  x = 0;
int  y = 0;

int motorSpeedA = 0;
int motorSpeedB = 0;

int ledPin = 10;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void loop() {
   
  if(Serial.available() > 1){    
    int input = Serial.parseInt();//read serial input and convert to integer (-32,768 to 32,767)    
    if(input == 1111){//if on code is received
      digitalWrite(ledPin, HIGH);//turn LED on
    }
    if(input == 0000){//if off code is received
      digitalWrite(ledPin, LOW);//turn LED off
    }
  }
  Serial.flush();//clear the serial buffer for unwanted inputs     
  
  delay(20);//delay little for better serial communication

    // Default value - no movement when the Joystick stays in the center
  xAxis = 510;
  yAxis = 510;

  // Read the incoming data from the 
  while (Serial.available() == 0) {}
  x = Serial.read();
  delay(10);
  y = Serial.read();
  delay(10);

  // Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below
  xAxis = x * 4;
  yAxis = y * 4;

  // Y-axis used for forward and backward control
  if (yAxis < 470) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
  }
  else if (yAxis > 550) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  }
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }

  // X-axis used for left and right control
  if (xAxis < 470) {
    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
    int xMapped = map(xAxis, 470, 0, 0, 255);
    // Move to left - decrease left motor speed, increase right motor speed
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (xAxis > 550) {
    // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
    int xMapped = map(xAxis, 550, 1023, 0, 255);
    // Move right - decrease right motor speed, increase left motor speed
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
 
}
1 Like

here is the video link:-

please take a look