I am making a car that is to be controlled by Android app through bluetooth. The problem that I am facing with servo motors is that before bluetooth module hc-05 gets connected to any bluetooth device, the servo motors rotate irrespective of whether it is connected or not.
Here's my arduino code for servo motors:
#include<Servo.h>
Servo servo1;
Servo servo2;
char val;
void setup()
{
Serial.begin(9600);
servo1.attach(5);
servo2.attach(9);
}
void loop()
{
if(Serial.available())
{
switch(Serial.read())
{
case 'F':
servo1.write(180);
servo2.write(180);
break;
case 'B':
servo1.write(90);
servo2.write(90);
break;
}
}
}
I don't know why it' rotating. It's very frustating. Plz help.
By the way the servo motors that I bought rotates only 180 degree.But I made it 360 degree by removing some part from inside.
Plz help and correct me if my code is wrong.
Try to add here:
switch(Serial.read())
- '0'`
Like here:
switch(Serial.read() - '0')
Related
I want to light up a LED wirelessly through processing.
what I have so far.
I can (wirelessly) turn on my LED using a serial terminal called "Bluterm".
I can turn on my LED by pressing 1 or 0 to switch LED on and off in processing.
How can I leave Bluterm out of my equation and use processing to send the 1 and 0 through bluetooth.
Here is my code for processing:
import processing.serial.*;
Serial port;
String string;
void setup(){
String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
port = new Serial(this, portName, 9600);
port.bufferUntil('\n');
}
void draw() {
printArray(string);
}
void keyPressed() {
if (key =='1'){port.write('1');}
if (key=='0') {port.write('0');}
}
void serialEvent(Serial port) {
string = port.readStringUntil('\n');}
and the Arduino code
char data;
int led = 13;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()>0){
data = Serial.read();
}
if (data=='1'){
Serial.println("HELLO");
digitalWrite(led, HIGH);
}
else if (data=='0'){
digitalWrite(led, LOW);
Serial.println("BYE");}
}
I'm kind of lost, can processing talk to bluetooth or do I always need a terminal?
If something isn't clear pls don't hesitate to ask,
Thank you for your time,
Juriaan
The Processing code makes sense.
It could do with a bit of formatting and error checking, but it's all pretty much there:
import processing.serial.*;
Serial port;
String string = "";
void setup() {
String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
try{
port = new Serial(this, portName, 9600);
port.bufferUntil('\n');
}catch(Exception e){
e.printStackTrace();
}
}
void draw() {
background(0);
text(string,10,15);
}
void keyPressed() {
if(port != null){
if (key =='1') {
port.write('1');
}
if (key=='0') {
port.write('0');
}
}
}
void serialEvent(Serial port) {
string = port.readString();
if(string == null){
println("null serial string");
string = "";
}
}
The Arduino code looks legit too.
What's unclear is what Bluetooth module you're using and how you're setting it up.
For example, if you're using something like BlueSmirf, be sure to use the guide
supplied.
The main points are:
Make sure you're using the SerialPortProfile (SPP) Bluetooth Profile
Double check you're wiring: the way your Arduino code reads you would be connect BT module's TX to Arduino's RX pin 0 and BT module's RX pin to Arduino's TX pin 1. Note you may want to do that after you upload your firmware with Arduino (as pin's 0 and 1 are Arduino's hardware Serial), otherwise goto point 3 :) (recommeded)
If you use an Arduino with multiple hardware serial ports (like Arduino mega) go with those (e.g. Serial1 instead of Serial) otherwise use a SoftwareSerial library with a low baud rate (like 9600), avoiding high baud rates.
Update
The HC-05 module uses 3.3V logic, while the Arduino uses 5V logic.
Uses a bidirectional 3.3V <-> 5V logic level converter or resistors, otherwise you risk frying your HC-05 module:
A quick search returns a detailed HowToMechatronics.com Arduino and HC-05 Bluetooth Module Tutorial
i see u are using a hc05 bluetooth device i have this myself but i dont really get what u want to use for sending the 1 and 0 to your hc05 and are you only using a led becouse if it is i would be able to help on (if you wanna send the bluetooth signals with a mobile app try the blynk app fron app store or google play store)
wondering if anyone would know why is this happening.
I have HM-10 Bluetooth module that is connected to Arduino. I use Serial Bluetooth Terminal for communicating with the HM-10 module.
The code below works perfectly fine with Arduino UNO,
the LED is turned ON/OFF
I receive messages in Serial monitor
I receive messages on my mobile phone
However if I use the same sketch and the same scheme with Arduino NANO
the LED is turned ON/OFF
I receive messages in Serial monitor
I do NOT receive any message on my mobile phone. I tried almost all other pins including the TX1 & RX0pins but without any luck. It simply doesn't send any data to the RXD pin of the HM-10 module.
Is this some kind of limitation of Arduino Nano or do I have a faulty one?
// Arduino Bluetooth modul HM-10
#define RX 11
#define TX 10
#define pinLED 13
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(TX, RX);
void setup() {
Serial.begin(9600);
Serial.println("Arduino on");
bluetooth.begin(9600);
bluetooth.print("Arduino ON");
pinMode(pinLED, OUTPUT);
}
void loop() {
byte BluetoothData;
if (bluetooth.available() > 0) {
BluetoothData=bluetooth.read();
switch (BluetoothData) {
case '0':
digitalWrite(pinLED, LOW);
Serial.println("LED turned OFF");
bluetooth.println("LED turned OFF");
break;
case '1':
digitalWrite(pinLED, HIGH);
Serial.println("LED turned ON");
bluetooth.println("LED turned ON");
break;
default:
Serial.println("Unknown command");
bluetooth.println("Unknown command");
}
}
delay(100);
}
Arduino Uno R3, HC-06. There's no response to the AT commands on the serial monitor.
I want to establish Bluetooth communication between PC and Arduino. At first I had to change the baud rate from 9600 to 115200, and the AT+BAUD8 command was working well. But after that change AT commands are not working. I cannot see any respond on the serial monitor.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // Bluetooth module Tx:Digital 2 Rx:Digital 3
void setup() {
Serial.begin(9600);
BTSerial.begin(9600);
Serial.println("ATcommand"); //ATcommand Start
}
void loop() {
if (BTSerial.available())
Serial.write(BTSerial.read());
if (Serial.available())
BTSerial.write(Serial.read());
}
I used the above simple code and also I've already checked the Tx, Rx pins.
What I want to do in the end is Bluetooth communication with a PC as a master and an Arduino as a slave.
Is it possible to upload code to Arduino through Bluetooth?
This code works for me:
#include <SoftwareSerial.h>
SoftwareSerial BT(10,11); // TX to pin_10. RX to pin_11 of Arduino.
void setup() {
Serial.begin(9600);
BT.begin(9600);
}
void loop() {
if(Serial.available()) {
String command = Serial.readStringUntil('\n');
Serial.println(command);
BT.print(command);
}
if (BT.available()){
String retorno = BT.readStringUntil('\n');
Serial.println(retorno);
}
}
Serial Monitor with New line.
AT
AT+VERSION
AT+NAMEmy_blue
AT+BAUDx
I'm having trouble communicating between Tera Term and an Arduino Mega over a bluetooth connection. My goal is to be able to set up the Mega so it can be later used to exchange text commands with a C++ application. Using the code I've found on this site, I can use the Arduino IDE Serial Monitor to send text to the Tera Term terminal, but I cannot send text from the Tera Term terminal to the Arduino. It never recognizes text was sent from the terminal. The bluetooth module I am using is the Bluetooth Mate Gold from SparkFun. The code's purpose is to detect incoming chars and then activate an LED. My code is shown below:
#include <SoftwareSerial.h>
int bluetoothTx = 15;
int bluetoothRx = 14;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
pinMode(13, OUTPUT);
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(115200);
bluetooth.print("$$$");
delay(100);
bluetooth.println("U,9600,N");
bluetooth.begin(9600);
}
void loop() {
//Read from bluetooth and write to usb serial
if(bluetooth.available()) {
char toSend = (char)bluetooth.read();
Serial.print(toSend);
flashLED();
}
//Read from usb serial to bluetooth
if(Serial.available()) {
char toSend = (char)Serial.read();
bluetooth.print(toSend);
flashLED();
}
}
void flashLED() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
}
The only thing that seems to work from Tera Term is entering command mode using "$$$." Doing that, I can run commands such as "D." I'm not sure why I can't send chars from Tera Term to the Arduino and have them be read. Any suggestions are appreciated.
I just built the same thing and i had to add a delay of 500 ms before configuring the module. I also had problems with receiving data since i used pin that did not support the PCINT interrupt.
delay(500);
bluetooth.begin(115200);
bluetooth.print("$");
bluetooth.print("$");
bluetooth.print("$");
delay(100);
bluetooth.println("U,9600,N");
bluetooth.begin(9600);
I'm a new bie on arduino projects. I would like to ask you for some help. I bought a BLE Shield for Arduino from ( http://imall.iteadstudio.com/development-platform/arduino/shields/im130704001.html ). They made this shield using Hm-10 Bluetooth module(http://www.jnhuamao.cn/bluetooth.asp?ID=1). Itead Studio has no sample codes using this shield. I have no idea on how to program it or send AT commands from Arduino.
I read the “AT commands” at the data sheet (ftp://imall.iteadstudio.com/Shield/IM130704001_ITEAD_BLE_Shield/DS_IM130704001_ITEAD_BLE_Shield.pdf) and I tried to send "AT commands” from arduino to BLE shield using this code ( http://arduino.cc/en/Reference/SoftwareSerial ) but I only received the commands back.
Did anybody here ever use this HM-10 bluetooth module ?
I need some arduino sketch for help !
#include <SoftwareSerial.h>
int led = 13;
int bluetoothTx = 2;
int bluetoothRx = 3;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int baudrate[8] ={4800,9600,14400,19200,28800,38400,57600,115200};
int i = 1;
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
while(!Serial){}
Serial.write("AT sent");
delay(500);
bluetooth.write("AT+NAME?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
bluetooth.write("AT+POWE3");
delay(500);
while(bluetooth.available())
{
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+CHAR?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+NAMEFlightline"); //Check Status
delay(500);
while (bluetooth.available()) {
Serial.write((char)bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+CHAR0x2901"); //add charicteristic
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+RELI0");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+SHOW1");
delay(100);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
pinMode(led,OUTPUT);
digitalWrite(led,HIGH);
}
void testAllBaudRates(){
for(int j=0; j<8; j++)
{
bluetooth.begin(baudrate[j]);
delay(100);
Serial.println("boud rate " + String(baudrate[j],DEC) +" i-> "+String(j,DEC));
// Serial.println("");
bluetooth.write("AT");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
Serial.println();
}
delay(100);
}
}
// and now a few blinks of the LED,
// so that we know the program is running
void loop()
{
//Read from bluetooth and write to usb serial
while(bluetooth.available())
{
char toSend = (char)bluetooth.read();
if(toSend == 'x'){
digitalWrite(led,HIGH);
Serial.println("set high");
bluetooth.write("RXOK");
}else if(toSend == 'y'){
digitalWrite(led,LOW);
Serial.println("set low");
bluetooth.write("RXOK");
}
Serial.print(toSend);
}
//Read from usb serial to bluetooth
while(Serial.available())
{
char toSend = (char)Serial.read();
bluetooth.write(toSend);
Serial.print(toSend);
}
}
Have a look at my sketch above I have a few things to point out that I wasted time on.
make sure you have the line
while(!Serial){}
Or you may get have a working shield but miss the responses as the serial monitor is no ready.
remember that you wont get a response from the blue-tooth module, with a command from the Serial Monitor if it is connected to a device. It is connected to the device when the light stops flashing.
if you run this sketch you should get this output
AT sent
OK+Set:3
OK+Get:0x2901 <- this may be blank the first time you run it
OK+Set:Flightline
OK+Set:0x2901
OK+Set:0
OK+Set:1
the most comprehensive list of AT commands can be found here
[All the AT commands and a good explanation][1]
You will need to at Characteristics to the device as I have done here
bluetooth.write("AT+CHAR?");
or you will find it to connect to iOS and Android
If you are connecting to Android use the BluetoothLE Classes not Bluetooth ones.
You can use this sketch with baud rate autodetect to control your HM-10. This is a part of Apploader project that allows to upload to Arduino board over BLE.
This is a little late too, but try the following code, if you send it "AT" it should give you back an "OK":
#include <SoftwareSerial.h>
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
}
void loop()
{
if(bluetooth.available()) // If the bluetooth sent any characters
{
// Send any characters the bluetooth prints to the serial monitor
Serial.print((char)bluetooth.read());
}
if(Serial.available()) // If stuff was typed in the serial monitor
{
// Send any characters the Serial monitor prints to the bluetooth
bluetooth.print((char)Serial.read());
}
// and loop forever and ever!
}