I´m trying to received some data (orders)from the Serial port monitor to my arduino uno. When I received the values that I sent, it were wrong. As this didn´t work, I put another simple program to my arduino: please check the program and the serial monitor:
Program:
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
and my monitor displayed: (please check the attached screencapture)
Characters in C are represented in the ASCII character set, which you can look up on google.
What you are seeing is the ASCII value of the character you entered. If you leave off the ",DEC" in your Serial.print() it will print the character itself instead of its ascii value.
@billroy: Thanks for your help!! I left off the "DEC" in the seial println command, but it continued displaying the same. maybe there is a problem with my arduino.... @AWOL: Thanks for your help!!
alumbrin: @billroy: Thanks for your help!! I left off the "DEC" in the seial println command, but it continued displaying the same. maybe there is a problem with my arduino.... @AWOL: Thanks for your help!!
incomingByte is declared as an int. If you want it to print the character rather than the int value, declare incomingByte as a char or cast it to one in the print call.
I often amend my code and then forget to upload it to my Arduino - could that have happened here?
While it is possible for an Arduino (or any other computer) to have a problem it is so unlikely as to be a waste of time considering it. 99.9999999% of the time it is the human that has the problem.
...R
alumbrin: @billroy: Thanks for your help!! I left off the "DEC" in the seial println command, but it continued displaying the same. maybe there is a problem with my arduino.... @AWOL: Thanks for your help!!
AWOL:
DEC is the default for "int" variables, so if you leave it off, it'll print the decimal value, never the character value.
Finally I don´t know what is happening to my arduino I checked three times uploading the program to the board and I got the same ascii values.....So I changed the program to this one:
char incomingByte[6]; // for incoming serial data
int i = 0;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte[i] = Serial.read();
i++;
}
if (incomingByte[i-1] == '.') { // say what you got:
incomingByte[i] = '\0';
i=0;
Serial.print("I received: ");
Serial.println(incomingByte);
}
}
Thank you all for your support!!! I will continue trying to know what caused the error....
char incomingByte[6]; => incomingByte is a char* type
incomingByte = Serial.read(); => Assign a byte to a char*
incomingByte = '\0'; => Again you cannot just assign a char to a char*
I will continue trying to know what caused the error....
There is no error, the behaviour is exactly as expected
Serial.println(incomingByte, DEC);
you asked it to print the numeric value, not the ascii representation.
you need to change the type of incomingByte to char and then just do Serial.println(incomingByte)