Notifications
Clear all

how can I edit this code to turn led on and off using Hc-12 module

10 Posts
2 Users
0 Likes
1,416 Views
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  

   
Quote
Ruplicator
(@ruplicator)
Member
Joined: 4 years ago
Posts: 127
 

Here is a copy of your code modified to include reading and sending a button press on your controller. On the receiver side you will need to add code to manage what you want to happen when a button is pressed. There is an integer defined "pressed" that contains a number associated with the pressed button where 0=up_button, 1=down_button, 2=left_button, 3=right_button, 4=start_button, 5=select_button, 6=analog_button, 7=none. Keep in mind that only allows one button to be pressed at a time and it is a momentary press. In other words when you press the button on the controller it shows up on the receiver, when you let up on it, it goes away on the receiver. If you want it to toggle you will need to add code on the receiver side to do that.

Note: I couldn't run this code so it very well may have bugs in it. Also I couldn't find out if any of the buttons on the controller toggle, if they do it will mess up the way this works. The best I could find out is that they are all momentary contacts. Good luck I hope this helps. 

P.S. I couldn't figure out how to get the formatted source code to list like you did so I attached a file.

 

int xAxis, yAxis;
int up_button = 2; // Boton Amarillo - A
int down_button = 4; // Boton Amarillo - C
int left_button = 5; // Boton Azul - D
int right_button = 3; // Boton Azul - B
int start_button = 6; // Boton F
int select_button = 7; // Boton E
int analog_button = 8; //
int none = 0; // No button
int buttons[]={up_button, down_button,left_button,
right_button,start_button,select_button,analog_button,none};
int pressed = 7; // 7 used to show no buttons pressed

void setup() {
Serial.begin(9600); // Default communication rate of the Bluetooth module
for(int i; i <7 ; i++)
{
pinMode(buttons[i],INPUT);
digitalWrite(buttons[i],HIGH);
}
}

void loop() {
xAxis = analogRead(A0); // Read Joysticks X-axis
yAxis = analogRead(A1); // Read Joysticks Y-axis
pressed = 7;
for(int i; i <7 ; i++)
{
if(digitalRead(buttons[i])==LOW)
{
pressed = i;
}
}

// 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);
Serial.write(pressed);
delay(20);
}

//Receiver (Rc Car)

 

#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 pressed = 7;

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);
pressed = Serial.read();
delay(10);

// Insert here your code to run when a button is pressed. The "int pressed" will contain the button number
// 0-7 and assoiated with {0=up_button, 1=down_button, 2=left_button, 3=right_button, 4=start_button,
// 5=select_button, 6=analog_button, 7=none} pressed = 7 can be tested to skip code when no button is pressed.

// 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
}

 


   
ReplyQuote
Ruplicator
(@ruplicator)
Member
Joined: 4 years ago
Posts: 127
 

   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  
Posted by: @ruplicator

pinMode(buttons[i],INPUT);
digitalWrite(buttons[i],HIGH);

so here in the place of "i" do I have to write the button number like 2,3 etc in both places ?


   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  

   
ReplyQuote
Ruplicator
(@ruplicator)
Member
Joined: 4 years ago
Posts: 127
 
Posted by: @ruplicator
Posted by: @ruplicator

pinMode(buttons[i],INPUT);
digitalWrite(buttons[i],HIGH);

so here in the place of "i" do I have to write the button number like 2,3 etc in both places ?

No, the "for loop" will initialize all the inputs for all the buttons on the controller. If you look at when "buttons" is initialized it contains all of the input pins that will connect to buttons.

 int up_button     = 2; // Boton Amarillo - A
int down_button   = 4; // Boton Amarillo - C 
int left_button   = 5; // Boton Azul     - D 
int right_button  = 3; // Boton Azul     - B
int start_button  = 6; // Boton F
int select_button = 7; // Boton E
int analog_button = 8; // 
int none          = 0; // No button
int buttons[]={up_button, down_button,left_button,
               right_button,start_button,select_button,analog_button,none};
int pressed = 7;  // 7 used to show no buttons pressed

void setup() {
  Serial.begin(9600); // Default communication rate of the Bluetooth module
  for(int i; i <7 ; i++)
     {
         pinMode(buttons[i],INPUT);
         digitalWrite(buttons[i],HIGH);  
     } 
}

   
ReplyQuote
Ruplicator
(@ruplicator)
Member
Joined: 4 years ago
Posts: 127
 
Posted by: @gulshan

do you think it can be modified to control the RC car ?

The code is just software and can be changed or modified to almost anything. However, it is best to determine what your goal is, then determine the best approach for you to accomplish that goal, then be persistent to accomplish that goal.

You will find numerous code examples that approach a problem various ways. Some are better than others. But more importantly is understanding what the code is doing before you cut and paste it. 


   
ReplyQuote
Ruplicator
(@ruplicator)
Member
Joined: 4 years ago
Posts: 127
 

Just noticed a little bug in the initialization loop for buttons.

 int up_button     = 2; // Boton Amarillo - A
int down_button   = 4; // Boton Amarillo - C 
int left_button   = 5; // Boton Azul     - D 
int right_button  = 3; // Boton Azul     - B
int start_button  = 6; // Boton F
int select_button = 7; // Boton E
int analog_button = 8; // 
int none          = 0; // No button
int buttons[]={up_button, down_button,left_button,
               right_button,start_button,select_button,analog_button,none};
int pressed = 7;  // 7 used to show no buttons pressed

void setup() {
  Serial.begin(9600); // Default communication rate of the Bluetooth module
  for(int i; i <7 ; i++)
     {
         pinMode(buttons[i],INPUT_PULLUP); // change this from INPUT to INPUT_HIGH
     //  digitalWrite(buttons[i],HIGH);    remove this line
     } 
} 

 


   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  

   
ReplyQuote
Ruplicator
(@ruplicator)
Member
Joined: 4 years ago
Posts: 127
 

I actually made very few changes to your original code. In the transmitter section there is code that names all the controller buttons and assigns a pin number to them as well as activating a pullup resistor.

 

 int xAxis, yAxis;
int up_button     = 2; // Boton Amarillo - A
int down_button   = 4; // Boton Amarillo - C 
int left_button   = 5; // Boton Azul     - D 
int right_button  = 3; // Boton Azul     - B
int start_button  = 6; // Boton F
int select_button = 7; // Boton E
int analog_button = 8; // 
int none          = 0; // No button
int buttons[]={up_button, down_button,left_button,
               right_button,start_button,select_button,analog_button,none};
int pressed = 7;  // 7 used to show no buttons pressed

for(int i; i <7 ; i++) { pinMode(buttons[i],INPUT_PULLUP);} // this assigns a input pin to each button } 

__________________________________________________________

This section scans through the each button looking for one that has been pressed. A LOW level indicates a button press and  when it detects one it saves the button index in the integer pressed. Note the normal buttons have an index number of 0-6, if all the buttons are scanned and NO button is pressed the value of 7 remains in the pressed variable. This is so you can test on the receive side for a value of 7 and take no action for buttons.

 pressed = 7;
  for(int i; i <7 ; i++)
     {
         if(digitalRead(buttons[i])==LOW) 
         {
            pressed = i;
         }
     } 

____________________________________________________________

This section sends the two values from your joy stick and I appended an additional serial.write to send the button index. This completes the changes to the transmitter.

 // 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);
  Serial.write(pressed);
  delay(20);

============================================================

RECEIVER CODE

This section loops looking for received data.     Once Serial.available is no longer zero it then reads the two original values that came from the joy stick and finally the index number from the button received and puts that value in the integer pressed.

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

Right below this section of code I added some comments to indicate this would be a good place to test the integer "pressed" to see if it has the value of seven. If it does then do nothing and skip the section of code you write that turns on a light when a button is pressed. You can find lots of examples of code to turn on LEDs.

 

I hope this helps to understand what the goal of code that was added. 


   
ReplyQuote