Hi
Need help in understanding part of the working code below.
char cmd[100]; thru to analogWrite(motorSpeedPin, val);
}
cmd[100] is set up to use as c in the code below ? Is this correct
Void() excCmd
analogWrite(motorSpeedPin, val);
if( cmd[0]=='s' && etc
What does this do, if command 6 is the first with new data.(is it something to do with sending each int separately) What if only 3digit number required to be sent. A less number of command index?
thanks
Charles Harris
/ Control dc motor speed over Bluetooth using RoboRemo app
// www.roboremo.com
// Hardware setup:
// BT module Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin0
// RX-I ------ pin1
// Vin
// |^^^^^^^^^^^|___|
// ==| dc motor |___
// |___________| |
// | C
// ___ B |/
// pin9--|___|-----| NPN
// 220 Ohm |->.
// | E
// |
// GND
#define bluetooth Serial
int motorSpeedPin = 9; // pin 9 (PWM) to contorl motor speed
char cmd[100];
int cmdIndex;
void exeCmd() {
if( cmd[0]=='s' &&
cmd[1]=='p' &&
cmd[2]=='e' &&
cmd[3]=='e' &&
cmd[4]=='d' &&
cmd[5]==' ' ) {
int val = 0;
for(int i=6; cmd[i]!=0; i++) { // number begins at cmd[6]
val = val*10 + (cmd[i]-'0');
}
// if cmd is "speed 100", val will be 100
analogWrite(motorSpeedPin, val);
}
}
void setup() {
delay(500); // wait for bluetooth module to start
bluetooth.begin(115200); // Bluetooth default baud is 115200
pinMode(motorSpeedPin, OUTPUT);
analogWrite(motorSpeedPin, 0);
cmdIndex = 0;
}
void loop() {
if(bluetooth.available()) {
char c = (char)bluetooth.read();
if(c=='\n') { // from RoboRemo, ending line
cmd[cmdIndex] = 0;
exeCmd(); // execute the command
cmdIndex = 0; // reset the cmdIndex
} else {
cmd[cmdIndex] = c;
if(cmdIndex<99) cmdIndex++;
}
}
}
So cmd is just an array of characters. Think of it as 100 little boxes and each one can hold one character. You're going to use these slots to accumulate the characters in your command. THe cmdIndex is going to be a number that lets you know which one of those little boxes you're on right now.
This is a true novice who doesn't know how strcmp works. It is checking the first 6 of those characters in that array to see if they spell "speed". I might not follow this coder's example. I would just use strcmp. It's much easier.
for(int i=6; cmd[i]!=0; i++) { // number begins at cmd[6]
val = val*10 + (cmd[i]-'0');
}
This is a novice coder who has never heard of atoi. He is going through the rest of that array until he gets to the end of the string (marked by a 0 google "strings in C") and turning them into a multidigit number by mujltiplying by ten and adding in the next digit. Again, I would use atoi and not follow this example. This one is particularly bad because it lacks a check to see if those are indeed digits. If someone threw a 'K' in the middle of that transmission it would muck up this whole section.
And finally that number that he puts together there gets written out to a PWM pin with the analogWrite line.
What Delta_G said. If you want to learn to program C++, this code is a bad example to be using. The only reason to code like this might be if you were trying to teach the very basics of the language first, and then later moving on to the standard libraries.
I will use your new coding and I now understand a lot more how things work, noobie to this sort of thing. (74 years and retired, mostly tired)
One last question. The use of the letter i . I can see how it works etc, but is the ' i ' an operator or a 'keyword' , are there any more I should know about? Have done a lot of searching but nothing I found gives the convention for using the character.
Nope. The i is just the name they picked for a variable. They could have called it bob or Fred or asdfjuhe if they had wanted to. i is just a name lots of people like to use for for loop counters. But there's nothing special about it and you definitely don't have to use i