Hi everyone,
Im trying to get readings from my bluetooth adapter (HC06) on my Arduino Micro.
ive got a very simple code but im only getting "-1" om my serial monitor. does someone has a clue why this is happening?
#include <SoftwareSerial.h>
int bluetoothTx = 7;
int bluetoothRx = 8;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(9600);
}
void loop()
{
Serial.println(bluetooth.read());
}
PS. i've got an adroid app that should send out values from 1000-3255
PS. i've got an adroid app that should send out values from 1000-3255
That sounds like just the sort of thing everybody needs.
You might find the following background notes useful
http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino
Have a look at this example:
https://www.arduino.cc/en/Tutorial/SoftwareSerialExample
(https://www.arduino.cc/en/Tutorial/SoftwareSerialExample)
-1 is the return value from read() function when there is nothing in the port to read.
-1 is the return value from read() function when there is nothing in the port to read.
Testing that there is data to read, BEFORE reading, will get rid of the pesky -1.
Hi Guys!
so ive managed to get some data.
if I send a 1bytenumber with my android app (appinventor2.0) it works great. ill get a number from 0-255 (more won't fit in a single byte so thats an issue).
but when i try to send it as a 2bytenumber, 4bytenumber or text I get 3 lines on my serial monitor instead of one single line with one number.
for example:
3
7
6
what I want:
376
does anybody know why this is happening and how i can fix it?
Thnaks in advance :)
Tymen
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(19200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(13, OUTPUT);
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() //
{
while(mySerial.available()){
delay(10);
char text = mySerial.read();
Serial.println(text);
}
does anybody know why this is happening
Your Android code and you Arduino code make this happen. You seem to have forgotten to post either one.
and how i can fix it?
A 10 lb. sledgehammer.
Since you did post the code, after I commented...
You clearly expect to read the data as text. So, clearly the Android is sending the value as text.
You might want to take a peak at the manual for parseInt() (https://www.arduino.cc/en/Reference/StreamParseInt).
Why don't you use the free hardware Serial on the Micro?
@ paul : here u go!
(http://i68.tinypic.com/2ivgzz8.jpg)
@ Paul : I also found out that Serial only take 1 byte at a time. so that why its acting so wierd on 2/4/text.
so i figured i need to use the buffer, but i havent figured out how. first time im using this function.
@Whandall: each time i want to upload a new code i have to unplug and plug the BT module. its a pain... so im using the software serial instead.
first time im using this function.
Using which function?
Serial Input Basics - Updated (http://forum.arduino.cc/index.php?topic=396450.0)
@Whandall: each time i want to upload a new code i have to unplug and plug the BT module. its a pain... so im using the software serial instead.
You don't have to do that on a Micro, it has a seperate hardware Serial.
Serial goes to upload and USB,
Serial1 goes to the free rx and tx pins.
In addition, some pins have specialized functions:
Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data using the ATmega32U4 hardware serial capability. Note that on the Micro, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class.
https://www.arduino.cc/en/Main/ArduinoBoardMicro (https://www.arduino.cc/en/Main/ArduinoBoardMicro)
Using which function?
Serial Input Basics - Updated (http://forum.arduino.cc/index.php?topic=396450.0)
buffering your serial input so i cant get multiple bytes of data in to one var. I keep getting multiple lines for one push of data. and i do not know how to get this in to one variable.
i tried this tutorial (https://www.youtube.com/watch?v=X5u2qCzcPn8&t=1s), but i still get this on my Serial monitor:
P
I
N
O
N
1
3
______
i created a button in AppInventor that sends "PINON 13" as TEXT.
i tried this tutorial
I'm not watching a movie to see some code. You already watched it, and stole, or typed, the code. Post it here.
You CAN collect data in an array, as Robin2's tutorial (that I posted a link to) shows.
i think i got the solution.
ive made a string var and put the serial data in there. but i also change "mySerial.read" to "mySerial.readString"
and now i get readble and expected values.
thanks for helping though!