Communication between two hc 05 Bluetooth modules not working - arduino

I Have a problem with the connection between two hc 05 modules. The bluetooth modules are paired using AT commands. But when I run the code I cant get any data from the reciever. The bluetooth modules blink together but no data is displayed in the reciever. This is for a RC car that is controlled by a remote controller.
This is the code for the remote
void setup() {
pinMode(A6, INPUT);
pinMode(A5, INPUT);
pinMode(A4, INPUT);
pinMode(A2, INPUT);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
Serial.begin(38400);
}
void loop() {
int rud = analogRead(A6);
int rlr = analogRead(A5);
int lud = analogRead(A4);
int llr = analogRead(A3);
int led = digitalRead(3);
int obs = digitalRead(4);
int line = digitalRead(5);
Serial.println(lud);
Serial.println(llr);
Serial.println(led);
Serial.println(obs);
Serial.println(line);
Serial.print("\n");
if (lud <= 50) {
Serial.write('2');
delay(500);
}
else if (lud >= 950) {
Serial.write('1');
delay(500);
}
else if (llr <= 50) {
Serial.write('3');
delay(500);
}
else if (llr >= 950) {
Serial.write('4');
delay(500);
}
else if (led == 0) {
Serial.write('6');
delay(500);
}
else if (obs == 0) {
Serial.write('7');
delay(500);
}
else if (line == 0) {
Serial.write('8');
delay(500);
}
else if (llr > 50 && llr < 950 && lud > 50 && lud < 950){
Serial.write('5');
delay(500);
}
}
I have made sure all the baudrates are 38400 and when I just type the numbers in the serial monitor of the reciver it works fine. I have switched the bluetooth modules but It didn't work either. Boath remote and car has TX - RX and RX - TX wiring.
This is the code of the reciever
int fl = 9;
int fr = 10;
int bl = 11;
int br = 12;
int Max = 255;
char data = 0;
void setup() {
pinMode(fl, OUTPUT);
pinMode(fr, OUTPUT);
pinMode(bl, OUTPUT);
pinMode(br, OUTPUT);
Serial.begin(38400);
}
void forword() {
Serial.println("Foword");
analogWrite(fl, Max);
analogWrite(fr, 0);
analogWrite(bl, Max);
analogWrite(br, 0);
}
void backword() {
Serial.println("Backword");
analogWrite(fl, 0);
analogWrite(fr, Max);
analogWrite(bl, 0);
analogWrite(br, Max);
}
void left() {
Serial.println("Left");
analogWrite(fl, 0);
analogWrite(fr, Max);
analogWrite(bl, Max);
analogWrite(br, 0);
}
void right() {
Serial.println("Right");
analogWrite(fl, Max);
analogWrite(fr, 0);
analogWrite(bl, 0);
analogWrite(br, Max);
}
void Stop() {
Serial.println("Stop");
analogWrite(fl, 0);
analogWrite(fr, 0);
analogWrite(bl, 0);
analogWrite(br, 0);
}
void loop() {
if (Serial.available() > 0) {
data = Serial.read();
if (data == '1') {
forword();
}
else if (data == '2') {
backword();
}
else if (data == '3') {
right();
}
else if (data == '4') {
left();
}
else if (data == '5') {
Stop();
}}
}
I used softwere serial as well but It gave me the same result. The reciever is an arduino mega and the transmitter is an arduino nano
Pls help.
Thank you.
I have made sure all the valtages are correct and I have made a robot like this before and it worked but I just can't find what's wrong. I expect it to send data to the receiver with out any noise.

Related

AttachIntrerrupt stops NRF24L01+ from working - ARDUINO

If I remove attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING); The code works. But once its added, the radio.available doesnt let anything under it run.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
struct InputData // define stuct
{
int x;
int y;
};
InputData data;
// Motor A connections
int motor_enA = 9;
int motor_in1 = 10;
int motor_in2 = 6;
int encoder1 = 2;
int encoder2 = 3;
int counter = 0;
int angle = 0;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
// Set all the motor control pins to outputs
pinMode(motor_enA, OUTPUT);
pinMode(motor_in1, OUTPUT);
pinMode(motor_in2, OUTPUT);
// Turn off motors - Initial state
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
analogWrite(motor_enA, 255);
pinMode (encoder1, INPUT);
pinMode (encoder2, INPUT);
attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING);
}
void loop() {
readEncoder();
if (radio.available()) {
radio.read(&data, sizeof(data));
// Serial.println(data.y);
if (data.y > 5) {
digitalWrite(motor_in1, HIGH);
digitalWrite(motor_in2, LOW);
}
else if (data.y < -5) {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, HIGH);
}
else {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
}
}
if(counter>1){
counter=0;
angle+=2;
}else if(counter<-1){
counter=0;
angle-=2;
}
Serial.print("Position: ");
Serial.println(angle);
}
void readEncoder()
{
if(digitalRead(encoder1)==HIGH){
int b = digitalRead(encoder2);
if(b>0){
counter++;
}
else{
counter--;
}
}
}
I have tried removing and adding the line, as described above^^
as mentioned by Hcheung, make counter volatile and remove readEncoder(); from loop.
I simplify a bit ISR readEncoder();
volatile int counter = 0;
[....]
void readEncoder() {
//if(digitalRead(encoder1)==HIGH){ //we are precisely here because digitalRead(encoder1) = HIGH !
if(digitalRead(encoder2)) counter++;
else counter--;
}

Serial communication stuck after first attempt

I am using arduino Mega 2560 to control some vibration motors with touchdesigner through Serial Communication. I maped the pixels to control each motors, It works for a few seconds and gets stuck very soon. Is there anything wrong with my code?
Here is my arduino sketch:
#define MOTOR_COUNT 12
int motors[MOTOR_COUNT];
void setup()
{
Serial.begin(115200);
Serial.println("Ready to receive frames.");
for (int i = 0; i <= 12; i++) {
pinMode(i, OUTPUT);
}
void loop()
{
if (Serial.available())
{
char c = Serial.peek();
if (!(c >= '0' && c <= '9'))
{
Serial.read(); // Discard non-digit character
}
else if (Serial.read() == '\n')
{
for (uint16_t i = 0; i < MOTOR_COUNT; i++)
{
motors[i] = Serial.parseInt();
Serial.print("motor ");
Serial.print(i);
Serial.print(":");
Serial.println(motors[i]);
if (i <= 12) {
analogWrite(i + 2, motors[i]);
}
}
}
}
}

Arduino Uno and HC-05: Not showing any output on serial monitor

Arduino Uno - HC-05
Connections are: TX-RX, RX-TX, LED-D13, 5V-5V+
For this project, we can supply power to the Arduino through any +5V power source. You can use a USB port from your computer to power the Arduino, but in this project I used my laptop.
while (Serial.available()) is returning 0 and Serial.read() is returning -1.
Need Help!
Used Bluetooth voice recognition tool from playstore-"Arduino Voice Control"
#include <SoftwareSerial.h> //Replace (' ') with (< >)
SoftwareSerial BLU(0,1);
String voice;
int Green = 13; //Connect To Pin #13
//int Yellow = 2; //Connect To Pin #2
//int Red = 3; //Connect To Pin #3
void allon() {
//digitalWrite(Red, HIGH);
//digitalWrite(Yellow, HIGH);
Serial.print("start");
digitalWrite(Green, HIGH);
}
void alloff() {
//digitalWrite(Red, LOW);
//digitalWrite(Yellow, LOW);
digitalWrite(Green, LOW);
}
void setup() {
Serial.begin(9600);
BLU.begin(9600);
//pinMode(Red, OUTPUT);
//pinMode(Yellow, OUTPUT);
pinMode(Green, OUTPUT);
}
void loop() {
//Serial.print("start loop");
//Serial.print(Serial.available());
while (Serial.available()) { //Check if there is an available byte to read
//Serial.print("start");
delay(10); //Delay added to make thing stable
char c = Serial.read(); //Conduct a serial read
//Serial.print(Serial.read());
if (c == '#') {
break; //Exit the loop when the # is detected after the word
}
//Serial.print(c);
voice += c;
//Serial.print(voice+"\n");
}
if (voice.length() > 0) {
Serial.print("Start");
Serial.print(voice);
if (voice == "*turn on all LED") {
allon();
}
else if (voice == "*turn off all LED") {
alloff();
}
/*else if(voice == "*switch on red") {
digitalWrite(Red,HIGH);
}
else if(voice == "*switch on yellow") {
digitalWrite(Yellow,HIGH);
}*/
else if(voice == "*switch on green") {
digitalWrite(Green,HIGH);
}
/*else if(voice == "*switch off red") {
digitalWrite(Red,LOW);
}
else if(voice == "*switch off yellow") {
digitalWrite(Yellow,LOW);
}*/
else if(voice == "*switch off green") {
digitalWrite(Green,LOW);
}
voice=""; //Reset variable
}
}
You need to check for the app output first. If you already know it then mention that in comment otherwise do the following for printing app output first:-
const int LED = 5;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
while(Serial.available()>0){
switchstate = Serial.read();
Serial.print(switchstate); // First check what output are you getting from the application
Serial.print("\n");
delay(15);
if(switchstate == '1'){ // Compare your app output accordingly
digitalWrite(5, HIGH);
}
else if(switchstate == '0'){
digitalWrite(5, LOW);
}
}
}

Implementing the function interrupt with if statements

I want to implement the function interrupt () but I don't know exactly how..In this case there is 2 for loops which can be seen in the code:I want whenever one of the 2 buttons is pressed the process inside the loop to be interrupted immediately:
void loop() {
int brightButton = digitalRead(K1);
int ldrStatus = analogRead(ldrPin);
if (brightButton == LOW && ldrStatus >= 200)
{
for (int i = 0; i < 10; i++)
{
digitalWrite(greenLed, HIGH);
tone(buzzer,400);
delay(500);
noTone(buzzer);
delay(500);
}
}
else {
digitalWrite(greenLed, LOW);
}
int tempButton = digitalRead(K2);
int valNTC = analogRead(NTC);
if (tempButton == LOW && valNTC > 512)
{
for (int i = 0; i <10; i++)
{
digitalWrite(redLed, HIGH);
tone(buzzer,450);
delay(300);
noTone(buzzer);
delay(1000);
}
}
else {
digitalWrite(redLed, LOW);
}
}
Example code from the Arduino manual:
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
Note that this will interrupt the for loop and return to it once the interrupt service routine is finished.
If you want to abort the for loop check the pin state in every loop cycle and break if you want to leave the for loop or return if you want to leave loop().
Of course this is not "immediately".

processing arduino connection difficulty

i am trying to send char's from processing to arduino, but arduino only recognizes 2 of them,
the problem is with the '2' char
when i press the 's' key the processing code is sending the '2' char because i can see the arduino rx led lighting but the motor does nothing,
with the '1' or '0' chars i have no problem,
i switched the '2' in the arduino code to correspond to drive_forward and then to drive_reverse, but the one that had '2' char assigned to it did not work in both cases,
as i said the '1'and '0' char are sent and received well
i guess it is something in the arduino code, but i don't know what
arduino code:
int motor1 = 4;
int motor2 = 5;
char val;
// --------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);
// Setup motors
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
}
// ---------------------------------------------------------------- Loop
void loop() {
if (Serial.available()>0)
{ // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == '2'){
drive_forward();
}
if (val == '1'){
drive_reverse();
}
if (val == '0'){
motor_stop();
}
}
// --------------------------------------------------------------------------- Drive
void motor_stop(){
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
}
void drive_forward(){
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
delay(15);
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
delay(15);
}
void drive_reverse(){
digitalWrite(motor2, HIGH);
digitalWrite(motor1, LOW);
delay(15);
digitalWrite(motor2, LOW);
digitalWrite(motor1, LOW);
delay(15);
}
processing code:
import processing.serial.*;
Serial myPort;
void setup()
{
size(200,200);
myPort = new Serial(this, Serial.list()[2], 9600);
}
void draw() {
}
void keyPressed() {
if (key == 'w' || key == 'W')
{
myPort.write('1');
println("1");}
if (key == 's' || key == 'S')
{
myPort.write('2');
println("2");}
}
void keyReleased() {
myPort.write('0');
println("0");
}
As #tailedmouse said, send data as integer.
Processing code:
//skipped some code
void keyPressed() {
if (key == 'w' || key == 'W') {
myPort.write(1);
}
if (key == 's' || key == 'S') {
myPort.write(2);
println("2");
}
}
void keyReleased() {
myPort.write(0);
println("0");
}
Arduino code:
//skipped some code.
void loop() {
if (Serial.available()>0) {
// If data is available to
read, val = Serial.read();
// read it and store it in val
}
if (val == 2) {
drive_forward();
}
if (val == '1') {
drive_reverse();
}
if (val == '0') {
motor_stop();
}
}

Resources