Now I'm stuck on something really simple. I have
char buf[80]
it read from the BT "S0512"
I want to set integer speed to the 512
How do I reference the 2nd to the 5th characters of buf?
I'm trying to do something like speed = buf[].toInt();
I must not be understanding the difference between char[] array and strings. ??
The method that you're talking about, buf.toInt() only works with Strings, as opposed to strings.
Note the capital letter, it differentiates between an Arduino object (String) and a C style string.
Strings are convenient in that they have methods to do all sorts of things, but when dealing with something as lacking in memory as the UNO, they present problems in the long run. One particular problem is that they consume a lot of memory and can lead to crashes if used extensively.
Perform a search on this forum with the term String and you'll get a lot of hits, mostly negative in nature.
A good thing to do is to understand how strings work.
Visit this page which gives some good information.
I notice that wildbill posted while I was tying this, so I'll just add that &buf[1] passes the address of element 2 of the character array (string) to atoi.
Well, mostly good news now. The robot is running forward and backwards. It's steering left and right. I just got it working tonight. Now I'm losing my BT connection from my cheap phone and from my expensive Samsung tablet. I'm wondering if when the servo or the motor pull current, it could be the BT is losing 5 v. for an instant and losing connection. I'll put a separate battery on it to test. The whole thing is now powered by 3 3.7 v Li Ion batteries making 12 v. The 12 v. goes to an L298N motor controller. The controller has a 5 v. output that powers everything except the drive motor. Anyway, I can try some things but if you have had experience with BT devices dropping their connection, you might save me some time. I'm using a DSD Tech HC-06. I just tried a different brand HC-06 and it loses connection even faster. (15 seconds) It does stay connected if it's just sitting there so I'll pursue the power theory.
tkolsenattds:
Well, mostly good news now. The robot is running forward and backwards. It's steering left and right. I just got it working tonight.
That's cool!
Anyway, I can try some things but if you have had experience with BT devices dropping their connection, you might save me some time. I'm using a DSD Tech HC-06. I just tried a different brand HC-06 and it loses connection even faster. (15 seconds) It does stay connected if it's just sitting there so I'll pursue the power theory.
I've not had much experience other than to have my BT earphones cut out whenever I walk past a microwave, or get close to a shopping centre kiosk with brightly lit cabinets using certain types of downlights.
A factor might be the BT Modules' physical proximity to the motors and the driver. If you're able to, move the module as far away from the driver/motors as you can. The RF interference from the motors might be reduced by doing so.
Power draw might be an issue, especially if the batteries are old/cheap/underpowered.
You could also read this article on ways of reducing noise on a circuit by using capacitors.
Bluetooth losing connection problem seems to be solved. It seems it was power.
I'm using 3 Li-ion 3.7v 18650 3000 mAh batteries to get about 12 v. That's good.
I was going to my L298N motor controller and using the 5 v output from that to
power the servo, the arduino, and the BT. That seemed to be the problem. When
the servo made a big move, it seemed to momentarily drop the 5 v. too much and
both the BT HC-06 and the Arduino mega lost power.
Now, I'm going with the 12 v to the motor controller and letting that power just the
drive motor and the servo.
Then 12 v from the same batteries, goes to a 5v voltage regulator. That 5v powers
the Arduino and the BT.
I did put a 1000 micro farad capacitor across that 5v but haven't tested yet if it was
necessary.
Walaa - the BT never loses connection.
I'm sure there will be more challenges but for now, things are looking good.
Thank you - Thank you for your help!!! It is so nice to be able to ask when I'm stuck.
Next problem: This one is just my lack of knowledge in handling characters in C.
I'm receiving mostly good data from the phone app. Occasionally, the data is not good. I'm thinking that is to be expected and I need to try to verify each new chunk of data before processing it. That's where my knowledge is lacking. I know the syntax in a couple other languages but not in C.
The data that should be coming is is either a "D" or an "S" as the first character followed by a 4 digit number. That should be followed by a . Examples:
D0512
S0100
D0000
etc.
My failing code is:
bool badData = false;
if (receivedChars[0] != 'D' && receivedChars[0] != 'S') badData = true;
if (receivedChars[5] != '\r')
{
Serial.print( "no <CR> ");
badData = true;
}
for (int j = 1; j < 5; j++)
{
if (receivedChars[j] < "0") badData = true;
if (receivedChars[j] > "9") badData = true;
}
if (badData) // if bad, display it
{
Serial.print("-BAD- BAD- ");
Serial.println(receivedChars);
}
if (! badData) // if good, process it
{