serial port - How can I receive data from an HM10 on another Arduino? -


link set of arduino's beginner in things arduino. tried set ble hm10 connection between 2 arduinos. connected ble modules ftdi cable , set them master , slave. connect instantly when power them up. set baud rate 9600 (default) , tried send "sensor value" slave master board servo should moved.

i wrote code down , tested in many ways. hm10 rx/tx pins in arduino pin 0/1 , 7/8... nothing works me. wrote statement if data of slave reaches master serial terminal outputs nothing instead of "a".

i used arduino uno slave code:

#include <softwareserial.h>  softwareserial myserial(7, 8); // rx, tx   // connect hm10      arduino uno //     txd          pin 7 //     rxd          pin 8  int reading = a0; // fsr attached a0 int fsrreading; int val;     void setup() {   serial.begin(9600);   myserial.begin(9600);   //bluetooth serial begin }  void loop() {  int reading = analogread(a0); //read fsr value     serial.print("analog reading = ");     serial.println(reading);     int val = map(reading, 0, 1023, 0, 180); // convert servo value     serial.print("servo value = ");     serial.println(val);     myserial.write(val);//send fsr value bt serial master     delay(500); //tweak lower value if communication working } 

and code master on arduino leonardo:

#include <softwareserial.h> #include <servo.h>  softwareserial myserial(7, 8); // rx, tx // connect hm10      arduino uno //     txd          pin 7 //     rxd          pin 8 int servopin = 9; //attached pin 9 (pwm) servo myservo;  int val; int data;  void setup() {   serial.begin(9600);   myserial.begin(9600);//bluetooth serial begin   myservo.attach(servopin);//servo obj attached pin9 }  void loop() {     int avail = myserial.available();//check serial available         if (avail > 0)         {             (int = 0; < avail; i++)             {                 int data = myserial.read();                 //no need map or constrain                 // data between 0 180                 myservo.write(data);//move servo pos                 serial.println(data,dec);                 delay(50);             }         } } 

if open serial terminal , don't action if press on "sensor" (fsr)... fsr value in serial not in serial of master... don't know hell did wrong. please guys, can see through spaghetti code , tell me fault?

there problems code.and maybe fsr analog input.

connect 1 end of fsr 5v, other end analog 0. connect 1 end of 10k resistor analog 0 ground.

this way voltage between 0 volt(no pressure) , 4.9 volt(full pressure ~22lb) on analog input.

this result in analog value between 0 , 1023.

myserial.write(val) sending integer byte, int value higher 255 not fit in byte, garbage send. map analog value before sending. if map value servo no conversion needed in receiver. so:

 int reading = analogread(a0); //read fsr value     serial.print("analog reading = ");     serial.println(reading);     int val = map(reading, 0, 1023, 0, 180); // convert servo value     serial.print("servo value = ");     serial.println(val);     myserial.write(val);//send fsr value bt serial master     delay(500); //tweak lower value if communication working 

this line:

int val = myserial.println(fsrreading); //convert fsr value in clean int 

is not doing think doing. maybe returns fsrreading length, probable garbage , not needed, remove it.

in example lines set delay 500. buffer of hm10 can handle 20 bytes, set delay low possible without overflowing buffer.

in receiving code read 1 byte. have smooth servo motion must read available , handle bytes set servo. read available there no need clean serial , checking available after reading or cleaning print in sketch.

instead check data received , print that, know receive. code:

void loop() {     int avail = myserial.available();//check serial available         if (avail > 0)         {             (int = 0; < avail; i++)             {                 int data = myserial.read();                 //no need map or constrain                 // data between 0 180                 myservo.write(data);//move servo pos                 serial.println(data,dec);                 delay(50);             }         } } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -