Bluetooth HC-05 Stop receiving data - bluetooth

i faced a problem with Arduino Uno and HC-05 Bluetooth.
i will be thankful if anyone could help me.
my problem is that: After uploading program to Arduino Uno successfully and sending commands from (Robotic arm app that was created by android studio) to Bluetooth HC-05, At first the Hc-05 receives data without any problem but after some movements to the arms the HC-05 stop receiving data from app and also Tx & Rx LEDs on Arduino don't work. if i restart Arduino it will work at first and then the same problem will happen.
#include <Servo.h>
Servo arm1;
Servo arm2;
Servo arm3;
Servo arm4;
Servo arm5;
char c = ' ';
String strData = "";
void setup() {
arm1.attach(3);
arm2.attach(5);
arm3.attach(6);
arm4.attach(9);
arm5.attach(10);
arm1.write(0);
arm2.write(0);
arm3.write(0);
arm4.write(0);
arm5.write(90);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
while (Serial.available() > 0) {
c = ((byte)Serial.read());
if (c == '?') {
String strDegree = strData.substring(6, strData.length());
int intDegree = strDegree.toInt();
if(strData.indexOf("arm1") >= 0)
arm1.write(intDegree);
if(strData.indexOf("arm2") >= 0)
arm2.write(intDegree);
if(strData.indexOf("arm3") >= 0)
arm3.write(intDegree);
if(strData.indexOf("arm4") >= 0)
arm4.write(intDegree);
if(strData.indexOf("arm5") >= 0)
arm5.write(intDegree);
Serial.println(strData);
strData = "";
break;
}
else {
strData += c;
}
delay(1);
}
}
}
i am waiting to hear something from you.
Best regards.

I hope it's not too late to help: D
Your code is good (Assuming the application you have is also correct)
I think the problem is in the hardware. I think that during the motion of the robot arm motor, there is a significant voltage drop on the arduin and that is why the connection is lost.
Try connecting the HC-05 to a separate power supply (VCC - 3.3-5V, HC-05 GND - Arduino GND - GND external power) and I think your problem will be solved.
I wonder what robot arm you use to integrate with the Arduino? I found a site where there are many robotic arms, but none are compatible with Arduino.

Related

Weird bug in i2c between Arduino and Raspberry Pi 4B

I have a weird bug when I'm sending data from Arduino Mega via I2C to Raspberry Pi 4B.
Arduino Mega is working as slave and Raspberry Pi as master. Raspberry Pi request data from Arduino and it sends a string of letters and numbers that look like this(no newline on the end):
xxE5xNx
but Raspberry Pi receives:
xxE5xN#
For sure that's not a issue with wiring, because I tested sending and receiving of 1000 packets without any error.
This bug does not happen when I change 'E', '5' or 'N' to another character or delete first two characters. When changing any of 'x' to another character bug still exists. Why?
Minimal code for reproducing:
Arduino Mega(slave):
#include <Wire.h>
String s = "xxE5xNx";
void i2cRequest(){
Wire.print(s);
}
void setup() {
Wire.begin(0x01);
Wire.onRequest(i2cRequest);
}
void loop() {}
Raspberry Pi 4B(master):
from smbus2 import SMBus
bus = SMBus(1)
data = bus.read_i2c_block_data(0x01, 0, 32)
text = ""
for m in data:
if(m != 255): text += chr(m)
print(text)

Change servo angle based on Bluetooth input

I'm using an app to either turn on an LED or change the angle of a micro servo depending on which button is pressed (using Arduino). My code works for the LED (while the button is pressed, the LED is on) but nothing happens when I press the button meant to change the angle of the servo to 40.
// Bluetooth serial:
#include <SoftwareSerial.h> // import the serial library
// setup the bluetooth coms
SoftwareSerial BTSerial(8,7);
#include <Servo.h>
int servoPin = 0;
Servo servo;
int angle = 0; // servo position in degrees
int input = 0;
int led2 = 13;
void setup() {
// put your setup code here, to run once:
servo.attach(servoPin);
Serial.begin(9600); // coms w/ computer
BTSerial.begin(9600); // coms w/ Bluetooth
pinMode(led2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (BTSerial.available())
{
input = BTSerial.read();
digitalWrite(led2, LOW);
switch(input) {
case 'E':
angle = 40;
break;
case 'C':
digitalWrite(led2, HIGH);
break;
}
servo.write(angle);
}
}
The input is right as I checked by also turning the LED on in case 'E' where it worked as normal. I had also tried using servo.write() within the case function as well but this didn't work either.
case 'E':
servo.write(40);
break;
You cannot use digital pins 0 or 1 as input:
As mentioned, those are serial send and receive pins. If you power your computer through USB, it can interfere if you try to use them, since it's reading from both the USB to Serial and the pin. Also you have an issue with anything connected (well, not anything, but remove it just to be safe) when you're trying to program. If you use the pins as intended, and program then run it off battery, it should be no problem at all.
Most of us stay away from it because it's a bit of a hassle
Depending on your Arduino model, servo.attach only supports pins 9 and 10:
Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10.
Or you could just use one of those anyway.

Serial monitor constantly say 0 when using arduino and sound sensor

I am having trouble with the arduino sound sensor and LEDs. I keep on getting the value of 0 in my serial monitor, the same thing happens with another sound sensor that I have. I am currently trying to make it light up the LEDs based on the sound but with the serial monitor reading 0 it will not activate the code. There should be a picture attached. The lights on the sound sensor is lighting up so I know the GND and 5V is working. Since it is hard to tell I am using 330 ohm resisters. I got the sound sensor from an elegoo starter kit, so I know it might be cheap. The picture is in the link at the end. Thank you.
int MicPin = A0;
int MicValue1 = 0;
int MicValue2 = 0;
int led1 = 2;
int led2 = 4;
int led3 = 6;
int led4 = 8;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(MicPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
MicValue1 = analogRead(MicPin);
Serial.println(MicValue1);
delay(1);
MicValue2 = analogRead(MicPin);
Serial.println(MicValue2);
if (MicValue1 - MicValue2 > 1) {
digitalWrite(led1, HIGH);
delay(2000);
}
else {
digitalWrite(led1, LOW);
}
}
enter image description here
I assume that you have a simple analog-output sensor module that provides 10bit analog values based on the environment volume level. If this assumption is correct, you wired all pins correctly and the received value is always out of range or at the maximum, you maybe had to integrate a resistor to get a valid value. Try a small resistor and increase the resistance until you receive suitable values. Maybe the documentation of your module provides further information.
There is a small rotating knob connected to the sensor on which we need to rotate to adjust particular resistance values, you can see different values in serial monitor out.
Useful link

ZS-040 (HC-05) Bluetooth module doesn't respond to AT

Hello,
so I have bought a ZS-040 HC-05 Arduino Bluetooth module and I want to change its name. I've learned, that you have to do that in AT Mode. I followed all instructions at http://www.martyncurrey.com/arduino-with-hc-05-bluetooth-module-at-mode/ I succesfully entered the AT Mode (Red LED is Blinking every 2 Seconds), but when I enter AT into the Serial Monitor, i get nothing.
This is my Arduino Code (preaty much exactly the same as described in that article)
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
char c = ' ';
void setup()
{
Serial.begin(9600);
Serial.println("Arduino is ready");
Serial.println("Remember to select Both NL & CR in the serial monitor");
BTserial.begin(38400);
}
void loop()
{
if (BTserial.available())
{
c = BTserial.read();
Serial.write(c);
}
if (Serial.available())
{
c = Serial.read();
BTserial.write(c);
}
}
Everything is connected as described in Method 1 (Other Methods doesn't work for me). I've set the Serial Monitors Baud to 9600 and to Both NL and CR.
If someone has experiense with this problem and solved it, please help :)
Thank you all very much!
I struggled with this for a while.
Upload a blank sketch (void setup(){} void loop(){})
Attach bluetooth module RX to RX on the Arduino (pin0) and TX to TX (pin1)
Insert ground wire from Bluetooth to G on the Arduino and EN to 3.3v
Power the Arduino
Insert the VCC from Bluetooth into 5v while holding down the small button on the bluetooth.
Bluetooth should have a very slow blink now, indicating it is in command mode.
Open the serial monitor on your computer. Set the baud rate to 38400. You will be able to test the connection by typing 'at'. It should respond 'OK' - try typing 'at' a second time if the first time receives an error.
the reason why this connection works is because 0 and 1 pins are used by the serial monitor when communicating with the Arduino. No program is need because it is the default connection when the monitor is opened.
I hope this helps.

Computer to Arduino mega via serial changing values

to start off, this might not be a problem with arduino code or arduino, but I figured I would post here because I really just can not figure out what is wrong.
I am working on this project just for fun to send key strokes from the keyboard, through the computer, and out through the USB to my arduino mega. No additional hardware is here, just the computer, the arduino, and the USB cable.
I am using Microsoft Visual Studio Express 2012 to write code to receive key strokes and send them to the USB. This is the code I am using:
#include "stdafx.h"
#include "conio.h"
using namespace System;
using namespace System::IO::Ports;
int main(array<System::String ^> ^args)
{
String^ portName;
String^ key;
int baudRate=9600;
Console::WriteLine("type in a port name and hit ENTER");
portName=Console::ReadLine();
//arduino settings
SerialPort^ arduino;
arduino = gcnew SerialPort(portName, baudRate);
//open port
try
{
arduino->Open();
while(1)
{
int k = getch();
key = k.ToString();
Console::WriteLine(key);
arduino->Write(key);
if (k == 32)
return 0;
}
}
catch (IO::IOException^ e )
{
Console::WriteLine(e->GetType()->Name+": Port is not ready");
}
}
This code works fine, and sends commands to the arduino. I might as well ask this as well, but after 35 key strokes it just stops sending key strokes, I am unsure as to why, but that is not an arduino problem (I don't think).
So when the certain value for key gets sent to the arduino, it changes. For example, the values that are assigned to the variable key for pressing the number 1 and 2 are 49 and 50, respectively. However, when they get sent to the arduino, the values are different some how. 1 is now 57, and 2 is now 48. I am unsure as to why this is happening. I tried 4 and 5 and they both have their values shift down 2 like the key 2. This is the code I have on the arduino:
int ledPin = 13;
int key=0;
int c;
void setup()
{
pinMode(ledPin, OUTPUT); // pin will be used to for output
Serial.begin(9600); // same as in your c++ script
}
void loop()
{
if (Serial.available() > 0)
{
key = Serial.read(); // used to read incoming data
if (key == 57)
{
digitalWrite(ledPin, HIGH);
}
else if (key == 48)
{
digitalWrite(ledPin, LOW);
}
}
c = key;
Serial.println(c);
}
As of right now it is just to switch a light on and off. I am hoping to involve many more keys and having the values be consistent would be very convenient. Anyways, if anyone could help me with why the values are different that would be awesome. I am not completely new to programming but I am certainly no expert and have not gotten too far into advanced stuff.
Thank you for any help or advice.
This has to do with what you are sending through visual studio. You are converting a keypress to its ASCII value, then converting that ASCII value to string, then sending that string through serial. The arduino is expecting a number, not a string.
For example, if you press the 1 key, your visual studio code converts that to an ASCII number 49, which is then converted to string "49", which the Arduino receives - but since you are sending "49", which is a "4" and an "9", the Arduino is reading 9 which corresponds to 57, as you have seen.
Similarly, pressing 2 converts it to "50", and the Arduino reads "0" which corresponds to the value 48 which you were getting.
To fix this, send the number directly, don't convert it into a string.

Resources