New Zealand
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« on: January 26, 2013, 04:21:03 pm » |
Hello,
My Arduino Mega periodically reads the voltage applied to one of its analog inputs. I then form a string which is to be transmitted out on one of the serial TX pins into another hardware device. Various parameters make up the whole string but I must write a checksum to the end of the string.
To calculate the checksum, I need to add the ASCII code for all characters in the string and then perform calculations. The result of the checksum is then placed at the end of the string and transmitted. An example string could be s0DFFID1234562.7 and I can form this okay and place it in a String variable.
I cannot find how to place the ASCII code for each character into a variable so I can do the number crunching. Have Googled etc but still struggling.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25506
Solder is electric glue
|
 |
« Reply #1 on: January 26, 2013, 04:26:40 pm » |
Use a char* array instead of a string, then you just pick them off one at a time:- char* myArray [] = "This is my string"; sum = int(myArray[0]) + int(myArray[1]) + int(myArray[2]) ...... Well you will use a loop and index but you get the idea.
|
|
|
|
« Last Edit: January 26, 2013, 04:29:33 pm by Grumpy_Mike »
|
Logged
|
|
|
|
|
New Zealand
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #2 on: January 26, 2013, 04:37:26 pm » |
Excellent, thank you.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 9
Posts: 836
|
 |
« Reply #3 on: January 26, 2013, 04:39:28 pm » |
Many applications also use the bitwise exclusive or to form the checksum. You can run into issues over whether the char is signed or unsigned and how much of the checksum you actually use.
|
|
|
|
|
Logged
|
|
|
|
|
New Zealand
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #4 on: January 26, 2013, 06:12:27 pm » |
I have data stored in String variables already that I would like to move one character at a time into the array but get the message "cannot convert 'String' to 'char' in assignment". Does that mean that I can never extract a single substring character and assign it to an array location.
|
|
|
|
|
Logged
|
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5929
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #5 on: January 26, 2013, 06:34:36 pm » |
can you just not use String and use char array instead?
|
|
|
|
|
Logged
|
|
|
|
|
New Zealand
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #6 on: January 26, 2013, 07:34:28 pm » |
I am working to just use the array but am new to this so solving the problems one by one. I am using an array to hold most of the individual characters that make up the whole command but I am struggling with getting the voltage value into the array as each number being a character. Please see my code below which gets the three digits from the voltage I am reading. // calculate first voltage digit int digit1Int = int(voltage); Serial.print("Voltage digit one :"); Serial.println(digit1Int); //Serial.println(); // calculate second voltage digit int digit2Int = int(10*(voltage-digit1Int)); Serial.print("Voltage digit two :"); Serial.println(digit2Int); //Serial.println(); // calculate third voltage digit int digit3Int = int((100*voltage)-(int(voltage*10)*10)); Serial.print("Voltage digit three :"); Serial.println(digit3Int); // set array values locations to hold the command commandArray[0] = 's'; // s for send sdm commandArray[1] = '0'; // command size commandArray[2] = 'D'; // command size commandArray[3] = '1'; // delay commandArray[4] = '9'; // delay commandArray[5] = '1'; // destination commandArray[6] = '2'; // destination commandArray[7] = '3'; // destination commandArray[8] = '4'; // destination commandArray[9] = '5'; // destination commandArray[10] = '6'; // destination commandArray[11] = '7'; // destination commandArray[12] = '8'; // destination commandArray[13] = digit1Int; // voltage commandArray[14] = digit2Int; // voltage commandArray[15] = digit3Int; // voltage // print out the values for (int loopCntr=0; loopCntr <= 15; loopCntr++) { Serial.println(commandArray[loopCntr]); Serial.println(int(commandArray[loopCntr]));
} When I run this it does not print the voltage information correctly. Thank you for your help. Moderator edit: [code] [/code] tags added.
|
|
|
|
« Last Edit: January 26, 2013, 07:48:51 pm by Coding Badly »
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 31
Posts: 2931
I only know some basic electricity....
|
 |
« Reply #7 on: January 26, 2013, 07:57:52 pm » |
// calculate first voltage digit
// set array values locations to hold the command commandArray[0] = 's'; // s for send sdm commandArray[1] = '0'; // command size commandArray[2] = 'D'; // command size commandArray[3] = '1'; // delay commandArray[4] = '9'; // delay commandArray[5] = '1'; // destination commandArray[6] = '2'; // destination commandArray[7] = '3'; // destination commandArray[8] = '4'; // destination commandArray[9] = '5'; // destination commandArray[10] = '6'; // destination commandArray[11] = '7'; // destination commandArray[12] = '8'; // destination commandArray[13] = digit1Int; // voltage commandArray[14] = digit2Int; // voltage commandArray[15] = digit3Int; // voltage
Is commandArray[] ints or chars?
|
|
|
|
« Last Edit: January 26, 2013, 08:00:04 pm by GoForSmoke »
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
New Zealand
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #8 on: January 26, 2013, 08:07:00 pm » |
Hello GoForSmoke, please see the code below where the array is char.
void setup() { // initialize both serial ports. Serial prints to PC Serial.begin(9600); // initialise the serial port to send data to the radio Serial1.begin(9600); }
void loop() { // initialise variables String voltageStr; String sizeStr; int commandSizeInt; String commandStr; String commandSizeHex; String commandSizeStr; int loopCounter; int commandSubStringInt; String commandSubStringString; char commandChr; char commandArray[20]; // read the input on analog pin 0: int sensorValue = analogRead(A0); // calculate the actual voltage float voltage = sensorValue * (5.0 / 1023.0); // print out the values above: Serial.print("Sensor value 0-1023 :"); Serial.println(sensorValue); Serial.print("Calculated voltage :"); Serial.println(voltage); // calculate first voltage digit int digit1Int = int(voltage); Serial.print("Voltage digit one :"); Serial.println(digit1Int); //Serial.println(); // calculate second voltage digit int digit2Int = int(10*(voltage-digit1Int)); Serial.print("Voltage digit two :"); Serial.println(digit2Int); //Serial.println(); // calculate third voltage digit int digit3Int = int((100*voltage)-(int(voltage*10)*10)); Serial.print("Voltage digit three :"); Serial.println(digit3Int); // set array values locations to hold the command commandArray[0] = 's'; // s for send sdm commandArray[1] = '0'; // command size commandArray[2] = 'D'; // command size commandArray[3] = '1'; // delay commandArray[4] = '9'; // delay commandArray[5] = '0'; // destination commandArray[6] = '8'; // destination commandArray[7] = '0'; // destination commandArray[8] = '0'; // destination commandArray[9] = 'T'; // destination commandArray[10] = 'E'; // destination commandArray[11] = 'S'; // destination commandArray[12] = 'T'; // destination commandArray[13] = digit1Int; // voltage commandArray[14] = digit2Int; // voltage commandArray[15] = digit3Int; // voltage for (int loopCntr=0; loopCntr <= 15; loopCntr++) { Serial.println(commandArray[loopCntr]); Serial.println(int(commandArray[loopCntr])); }
delay(5000); }
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6388
-
|
 |
« Reply #9 on: January 26, 2013, 08:09:36 pm » |
How about: sprintf(commandArray, "s0D1912345678%03d", voltage);
The %03d is replaced with a three digit decimal representation of the value of voltage, padded with leading zeros if necessary.
|
|
|
|
|
Logged
|
|
|
|
|
New Zealand
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #10 on: January 26, 2013, 08:24:44 pm » |
Thank you to all that have replied. I will experiment with your advice.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 9
Posts: 836
|
 |
« Reply #11 on: January 26, 2013, 08:38:44 pm » |
int voltage=240 ; char buffer[4] ; sprintf(buffer,"%03d",voltage); buffer[3]='\0' ;
// And to calculate the checksum byte cs=0 ; char* p=buffer ; while(*p != '\0' ) { cs ^= *p ; p++ ; }
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 31
Posts: 2931
I only know some basic electricity....
|
 |
« Reply #12 on: January 26, 2013, 10:11:23 pm » |
Hello GoForSmoke, please see the code below where the array is char. commandArray[13] = digit1Int; // voltage commandArray[14] = digit2Int; // voltage commandArray[15] = digit3Int; // voltage } [/quote]
Then stuffing 16-bit ints into 8-bit chars is probably not a good idea if you want the answers to make sense.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
New Zealand
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #13 on: January 26, 2013, 11:08:51 pm » |
Thank you to all who have responded with advice and code samples. I have solved the problem using your responses. Best regards.
|
|
|
|
|
Logged
|
|
|
|
|
|