I wrote this simple code. This code is to control servo by serial monitor. I sent command $11111 or $00000 which should move the servos, but it doesn't work.
Arduino is connected, i can upload code, but serial monitor doesn't work.
Does anybody have idea whats the problem?
I left my magnifying glass somewhere, so no.
Please read this and then post your code properly.
Post your code using code tags. This icon in the editor...

#include <Servo.h>
#define numOfValsRec 5
#define digitsPerValRec 1
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
int valsRec[numOfValsRec];
int stringLength = numOfValsRec * digitsPerValRec + 1;
int counter = 0;
bool counterStart = false;
String receivedString;
void setup() {
Serial.begin(9600);
servo1.attach(11);
servo2.attach(10);
servo3.attach(9);
servo4.attach(8);
servo5.attach(7);
}
void receiveData() {
while(Serial.available())
{
char c = Serial.read();
if (c=='$') {
counterStart = true;
}
if (counterStart) {
if (counter < 'stringLenght') {
receivedString = String ('receievedString'+c);
counter++;
}
if (counter >='stringLenght'){
for (int i = 0; i<numOfValsRec; i++)
{
int num = (i*digitsPerValRec)+1;
valsRec[i] = receivedString.substring(num,num + digitsPerValRec ).toInt();
}
receivedString = "";
counter = 0;
counterStart = false;
}
}
}
}
void loop() {
receiveData();
if (valsRec[0] == 1) {servo1.write(180);} else {servo1.write(0);}
if (valsRec[1] == 1) {servo2.write(180);} else {servo2.write(0);}
if (valsRec[2] == 1) {servo3.write(180);} else {servo3.write(0);}
if (valsRec[3] == 1) {servo3.write(180);} else {servo4.write(0);}
if (valsRec[4] == 1) {servo5.write(180);} else {servo5.write(0);}
}
??? ... why the quotes? ... you seem to be using them in multiple places.
Might want to check the spelling as well...
int stringLength
What do you mean by $11111?
Serial monitor is actually a byte transfer.
Assuming your $11111 means: hex with value 0x11111 - so: three bytes! (not a single one).
But these bytes are not ASCII code (they are binary code).
A serial monitor will not display any character for a value 0x11: as ASCII it is taken as a control character, like a NEWLINE ('\n', hex code 0x0A) is.
A value 0x11 send as ASCII code is: CTRL-Q - which is used for flow control.
I think, you should try to understand: "UART is a byte transfer", "a byte can be binary or coded as ASCII", "what does ASCII code do on serial receiver (terminal program)?"
Hi!
can you confirm the model of your board (UNO, nano etc...).
Here is how to simply use the serial monitor:
you setup it in the Setup() function:
Serial.begin(9600); // done and Ok
where you want to display on serial monitor a value (for example in Loop() ), you use
Serial.print(VALUE);
for instance, and related to your code:
Serial.print(valsRec[0]);
EDIT: here is the official reference for the serial.print function. Only good hints inside! ![]()
And sending a value larger as a byte, e.g. value 0x011111 -> results in a transmission of 3 bytes:
0x01, 0x11, 0x11 - and all are values not representing a character (assuming ASCII code). Nothing will be displayed on an UART terminal program (e.g. TeraTerm) - just taken as CTRL-characters.
without quotes i get mesage
Compilation error: 'stringLenght' was not declared in this scope
note: suggested alternative: 'stringLength'
Arduino Uno
Spelt wrong.
How write it properly, i dont see mistake
Post #5
I see it now
These quotes need to go too.
Ok, i wiil try again
It's working now, thanks.
Remark:
"what is a String?"
Often, it is a sequence of ASCII characters, maybe it can handle a binary byte string. But ASCII and binary is not the same! - even they are byte based.
What happens if you would send a binary byte (character) as 0x00? It could mean "End of String" (C-code string termination).
And what happens if you try to send "strings" which are NUL-terminated (as C-strings, with 0x00 as "end of string") but you do not send the 0x00 (e.g. it is omitted, such a NUL-character is NOT covered by a strlen(s) call: just a NUL there and string length is 0.
So, be careful if a "string" means "a NUL-terminated" byte stream (characters).
If you do not send the NUL (as 0x00) as a "regular character" - the receiver might not see a properly "terminated" (C-code) string.
And a "string" in C++ code has often a length, not a NUL-termination. But when you send now a "string" via UART, the length is NOT transmitted: so, there must be a different criteria that the strong is complete now on reception (e.g. also the NUL was received or you tell the receiver how many characters belong to the "string".
If you receive any number of characters and you "cast" or use it as a "string" - the length can be still unknown (never set).
C++ strings are a "complex" object, potentially you have to set the length of the string.
But the length is not transmitted.
Or: you send C-code strings, which are NOT NUL-terminated (the NUL is not really transmitted).
How to add the string length or the NUL to a received "string"?
I'm not sure, but $ starts in serial monitor because of this part of code.
if (c=='$') {
counterStart = true;
I am confused:
a character which wants to see on UART reception?
An internal indication to enable UART transfer (or something else)?
Is the '$' transmitted via UART? (it would "confuse" a lot the receivers = what does it mean?)
Are you able to elaborate:
- what do you want to send?
- what do you send really at the end (e.g. a LOG file what is transmitted via UART)?
I think, you struggle with the "protocol" : what to send how via a communication interface (e.g. UART, which is byte oriented, ASCII code vs. BINARY code, etc.).
This '$' is very unusual (never used in C/C++ code), maybe part of your "protocol": we would need more insight what this '$' is used for.
