I'm a new user to the Arduino, so far really enjoying using it. I've run through some of the tutorials, and am now trying to piece together a program.
I am trying to communicate with a device which sends 9 characters, of those 9 I am interested in the 5th value. For right now I am reading the entire string into an array, declaring the 5th value in the array as a different variable and lighting some LEDs from there.
My questions are :
Can I use the serial monitor while my program is also reading a serial value on pin 1?
So far I can't get any change out of the LEDs, I am unsure if it is because I am not filling the array properly, or if I am accessing improperly. Could you fine folks comment on my code?
int ledPin = 8;
int ledPinLow = 7;
int IncomArray[9];
int TensDigit = 0;
void setup() {
Serial.begin(300);
pinMode(ledPin, OUTPUT);
pinMode(ledPinLow, OUTPUT);
Serial.flush();
}
void loop(){
while (Serial.available() >= 9) { // Wait 'till there are 9 Bytes waiting
for(int n=0; n<9; n++)
IncomArray[n] = Serial.read(); // Then: Get them.
}
IncomArray[5] = TensDigit; // Fill Tens digit place with value in IncomArray position 5
if(TensDigit >= 5){ // If the tens digit is equal to 5,
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // Wait one second
Serial.flush(); // Flush buffer
}
if(TensDigit < 5){ // If tens digit is less than 5,
digitalWrite(ledPinLow, HIGH); // Turn on ledPinLow
delay(1000); // Wait one second
Serial.flush(); // Flush buffer
}
}
Making progress! I've made some modifications and am able to receive my serial values. However the values that I am seeing are not what I expected. I have modified my code to be:
char Value[9]; // Initialize array for values
int TensDigit; // First digit in the tens place
int OnesDigit; // Second digit in the ones place
int DecimalDigit; // Decimal Digit
int LEDpin = 13; // Alarm LED
void setup(){
Serial.begin(300); //Start serial to device
pinMode(LEDpin, OUTPUT); // define LEDpin as an output
}
void loop(){
while (Serial.available()>8) { // Wait 'till there are 9 Bytes waiting
Serial.println("Serial is working"); // Let me know that the serial is available
for(int n=0; n<9; n++){
Value[n] = Serial.read(); // Loop iterates until each spot is filled
}
Serial.flush(); //Clear buffer after reading values, EM noise etc.
}
TensDigit = Value[5] - '0'; // Tens digit in array Value subtracted from '0' to give a usable integer
OnesDigit = Value[6] - '0'; // Ones digit in array Value subtracted from '0' to give a usable integer
DecimalDigit = Value[7] - '0'; // Decimal digit in array Value subtracted from '0' to give a usable integer
Serial.println(Value); // Print entire value array
Serial.println(TensDigit); // Print the integer value in TensDigit
Serial.println(OnesDigit); // Print the integer value in OnesDigit
Serial.println(DecimalDigit); // Prints the integer value in DecimalDigit
if(TensDigit >= 5) // If the integer in TensDigit is greater than or equal to five:
{
digitalWrite(LEDpin, HIGH); // Turn on the LED
}
else // If not:
{
digitalWrite(LEDpin, LOW); // Turn it off
}
delay(1000); // Wait one second and repeat (Neccessary?)
}
I am watching it using the serial monitor and I get my verification that the serial is working, but the array returns :
ÓÛÛÛ<~w¿
The expected return should be a numerical string... Can anyone offer some insight?
I am attempting to communicate with a hardness testing head, the expected format for my arrayed values is:
0 = Start
1 = Sequence 0
2 = Sequence 1
3 = Sequence 2
4 = Blank
5 = Tens Digit
6 = Ones Digit
7 = Decimal Digit
8 = Tolerance
9 = Carriage return
Serial.println(Value); // Print entire value array
Value is an array of characters. It is not a string. This will not work.
The difference between an array of characters and a string is that a string is an array of characters that is NULL terminated. You don't even have room for the NULL terminator in the array.
I think you should change this:
Serial.println(Value); // Print entire value array
Serial.println(TensDigit); // Print the integer value in TensDigit
Serial.println(OnesDigit); // Print the integer value in OnesDigit
Serial.println(DecimalDigit); // Prints the integer value in DecimalDigit
to this:
Serial.print("Complete value: [");
Serial.print(Value); // Print entire value array
Serial.println("]");
Serial.print("Tens digit: [");
Serial.println(TensDigit); // Print the integer value in TensDigit
Serial.println("]");
Serial.print("Ones digit: [");
Serial.println(OnesDigit); // Print the integer value in OnesDigit
Serial.println("]");
Serial.print("Decimal digit: [");
Serial.println(DecimalDigit); // Prints the integer value in DecimalDigit
Serial.println("]");
This makes it easier to see what data is being sent back (in)correctly.
I agree. But, there may have been control characters that moved the cursor around, resulting in the legible data being overwritten. We can't see how much data was output. There were 4 lines that should have been printed, but OP only showed one line, and didn't say which line that was.
I amended the program, still had issues and decided to investigate other issues. The manufacturer pinout that was provided has proven to be incorrect, once I located the proper pins I am now receiving values that make sense! Though they are not in the expected format as defined by the manufacturer. (Surprise!)
I have changed my code to use array positions 10, 11 and 13 to display digits so that portion works like a charm.
I can now see that it is fragile, and if a perfect string isn't received it won't behave as expected. I extended the array to see if anything else is after my decimal digit, but all I receive is :ÿÿÿÿÿÿÿÿÿÿ
for the remainder of the array regardless of size.
I am quite pleased so far that I am receiving what I want, but I am at a loss on how to ensure that the data format as I see it above is displayed properly.
Could you offer any insight?
I've been looking at this http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1271794997 and was wondering if something like below. Would I modify it to work so that it expects a character count? Or is there a better way to check for an end?
CharIn = '\0';
while(Serial.available() > 0 && CharIn != FOOTER && i<INPUT_MAX_LENGTH-1)
{
CharIn = Serial.read();
StringIn[i++] = CharIn;
StringIn[i] = '\0'; // Append a NULL so the string is NULL terminated
}