Hello,
Im looking for a little guidance with coding an Arduino. I have my Arduino connected to a bluetooth HC-10 adapter, and I can communicate with the adapter just fine. I used the tutorial (https://www.arduino.cc/en/Tutorial/SoftwareSerialExample).
I am trying to write some code to do the following:
Parse the data (There is actually two responses listed above representing two bluetooth devices in range of the arduino. I am interested in the FFFF line which is their serial number, and also the -045 or -057 values which is the signal strength)
Do something (light an LED if signal strength drops below a given value)
but all this does is display 1 character every 500ms. I am wanting it to scan for available devices, print the results, then pause for 30 seconds, then repeat.
Thoughts as to what I am doing wrong or how to properly implement this?
I took your advice, and moved the mySerial.write to the setup, and the loop is just displaying the results. I am needing this to "re-scan" or send the command AT+DISI? to the bluetooth adapter every 30 seconds or so. I want the unit to keep refreshing... Thoughts on how to accomplish that one?
Good thought… I ran through the example (https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay) but am still experiencing a similar problem. When watching the serial monitor, the arduino is printing our 1 character at a time. (one second apart, as per the led example above).
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7); // RX, TX
// Connect HM10 Arduino Uno
// Pin 1/TXD Pin 7
// Pin 2/RXD Pin 8
// constants won't change. Used here to set a pin number :
const int ledPin = 13; // the number of the LED pin
// Variables will change :
int ledState = LOW; //
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
Serial.begin(9600);
// If the baudrate of the HM-10 module has been updated,
// you may need to change 9600 by another value
// Once you have found the correct baudrate,
// you can update it using AT+BAUDx command
// e.g. AT+BAUD0 for 9600 bauds
mySerial.begin(9600);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
mySerial.write("AT+DISI?");
if (mySerial.available()) {
Serial.write(mySerial.read());
}
}
}
Correct. In order to accomplish that, do I run two loops? I am not understanding how I can have the "listen" loop running and also print the command every 30 sec. Is the arduino able to perform tasks outside of a loop while that loop is running?
Im having some issues with parsing the string that I am receiving and need to see if you have any thoughts. Every 30 seconds, the arduino scans for available bluetooth devices, and prints some info. Here is an example of only 1 available device: