using serial data to set varaibles

I have a program that will take input from the serial port to set variables for use in the program. I’m ‘+++’ as header bytes to screen out noise from the serial. The program then loads each byte and performs some addition and multiplication. The code snippet is used to qualify the data and load the values into an array. This will eventually be used to set my real time clock, but for now I’d like to see the values correctly.

I’m missing code to treat these values and int…I think.


//–Serial Data Polling with EEPROM loading and Reading##########
void pollSerialPort(){

//— variables will hold data from the serial port
int data;
int newTime;
int hhnew;
int mmNew10;
int mmNew;
int Lton;
int Loff;
int tmpNew;
int i;
int controlData[13];

//— Check to see if the buffer has enough data to process
if(Serial.available() >= 7) {
data = Serial.read();
//Serial.print(data);
//— Check the first byte
if(data = checkByte1){
data = Serial.read();
//Serial.print(data);
//— Check the second byte
if(data = checkByte2){
data = Serial.read();
//Serial.print(data);
//— Check the third byte
if(data = checkByte3){
//–Reads the remainder of the data into array controlData
for (i = 0; i < 4; i = i + 1) {
controlData* = Serial.read();*
controlData_ = int(controlData*);
}
hhnew = ((controlData[0] * 10) + controlData[1]);
mmNew = ((controlData[2] * 10) + controlData[3]);
newTime = ((hhnew * 100) + mmNew);
Serial.println();
Serial.print("line from data: “);
Serial.print(hhnew);
Serial.print(” “);
Serial.print(mmNew);
Serial.print(” “);
Serial.print(controlData[2]);
Serial.println();
Serial.print(controlData[3]);
Serial.print(” ");
}
}
}
}
}
[/quote]*
**first time posting code…be gentle._

First, you are using = as a equality comparison operator. It is not. The equality comparison operator is ==.

You have not included the definition of checkByte1, checkByte2, etc., so it is impossible to tell whether replacing = with == will solve your problem, or not.

Where is the serial data coming from? If the serial data is coming from the Arduino's IDE's Serial Monitor window, the data is sent as characters. The character '0' is not the same as the number 0.

Multiplying '1' by 10 and adding '2' will not result in hhnew having a value of 12.

You may need to replace the

controlData[i] = Serial.read();

statement with

controlData[i] = Serial.read() - '0';

When setting the time on the RTC, 12:45 is not passed in as 1245, so the newTime calculation is not necessary.

The Serial.print calls could use qualifiers, in some cases. To assure that hhnew prints properly, use

Serial.print(hhnew, DEC);

**first time posting code…be gentle.

Arghhhhhh!
Bish Bosh Bash ;D

quote

Re: using serial data to set varaibles Reply #1 - Today at 22:13:09 First, you are using = as a equality comparison operator. It is not. The equality comparison operator is ==.This part of the code is working fine, but I agree that I need to use ==. I will make those changes.

You have not included the definition of checkByte1, checkByte2, etc., so it is impossible to tell whether replacing = with == will solve your problem, or not.

Where is the serial data coming from? If the serial data is coming from the Arduino's IDE's Serial Monitor window, the data is sent as characters. The character '0' is not the same as the number 0. The serial data is coming from the IDE. Eventually it may come from a python script or from a java.

Multiplying '1' by 10 and adding '2' will not result in hhnew having a value of 12. How do I set the program to do this?

You may need to replace the Code:

controlData = Serial.read(); statement with Code: controlData = Serial.read() - '0'; I'm not familiar with " - '0' " What does that do? When setting the time on the RTC, 12:45 is not passed in as 1245, so the newTime calculation is not necessary. To set the RTC I need hours and minutes stored as two variables. For other event timers, I will need to perform the "newTime" conversion. The Serial.print calls could use qualifiers, in some cases. To assure that hhnew prints properly, use Code: Serial.print(hhnew, DEC); quote

controlData = Serial.read() - '0'; I'm not familiar with " - '0' " What does that do?[/quote] The ASCII character '0' has the value 0x30 (48 decimal). The ASCII character '9' has the value 0x39 (57 decimal). To convert the ASCII '0' to the value zero, we subtract 0x30 (0x30 - 0x30 = 0x00). To convert the ASCII '9' to the value nine, we subtract 0x30 (0x39 - 0x30 = 0x09). See the pattern? > Multiplying '1' by 10 and adding '2' will not result in hhnew having a value of 12. How do I set the program to do this? By multiplying '1' - '0' by 10 and adding '2' - '0'. See above. > This part of the code is working fine I doubt it, and even if it is, it won't next time.

Thanks Awol

It makes sense now.

ps-I will be adding the "==" to my code as well to ensure that is works all the time.