Pages: [1]   Go Down
Author Topic: Need Help Parsing Strings and Finding Absolute Difference of Two Values  (Read 479 times)
0 Members and 1 Guest are viewing this topic.
New Hampshire (USA)
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I’m trying to find the absolute value of the difference between two test results.

We have a tester that outputs results in ASCII and I have been able to display this data in the serial monitor; see image below.

The test sequence goes like this:
(1)   Turn the tester on and it sends out a “memory check”. This happens only once and we usually turn on the machine on/off once or twice per day.
(2)   Device to be tested is hooked up to the tester
(3)   “PD” (pressure decay) test is performed
(4)   “FLOW” test #1 is performed
(5)   Second “FLOW” test #2 is performed
(6)   New device is hooked up to the tester
(7)   “PD” test is performed
(8)   “FLOW” test #1 is performed
(9)   Second “FLOW” test #2 is performed
(10)    New device is hooked up to the tester
(11)    …..

The cycle repeats and each individual device gets one PD test and two FLOW tests. We test approximately 1000 devices per day.

The goal of the program is to find the absolute difference (delta) between the two FLOW tests per device. This difference will eventually be displayed on an LCD.

I envision populating variables called “FLOW 1”, “FLOW 2” and “FLOW DELTA”.

IF the “FLOW DELTA” is less than 150, the result would be “PASS”
IF the “FLOW DELTA” is greater than 150, the result would be “FAIL”.

Per device, the variables would display “WAITING” on the LCD unless they were populated; see image below.

I’ve been trying to get this to work using sscanf(), but I just can’t get anything to compile. I believe that the incoming string is in the following format:

"%d\t%d\t%s\t%f\t%s\t%s\t%s"

So far, here is the code that I’m using to display the string in the serial monitor:

Code:
String incomingByte;   // 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:
                char incomingByte = Serial.read();
              
                Serial.print(incomingByte);
        }
}

I’m not very good at programming, so here what I’m thinking in comments:

Code:
         // use sscanf() to store the incoming string
          // IF the string contains "FLOW", store the value immediately following the word "FLOW" as "FLOW_1". If "FLOW_1" already has a numeric value, then store as "FLOW_2"
          // IF the string contains "PD", then set "FLOW_1" and "FLOW_2" to a value of "WAITING" (this resets the values for the next device under test)
          // Caculate "RESULT" = abs(FLOW_1 - FLOW_2);
          // IF "RESULT" is greater than 150, then "RESULT" is stored as "FAIL"
          // IF "RESULT" is less than 150, then "RESULT" is stored as "PASS"

How can I write code to execute my comments? Translating my comments into code is literally like speaking a foreign language where I can’t mumble or misspeak, elsewise I get compiling error roadblocks that I don’t understand. I’ll keep at it, though!  


* Untitled-1.jpg (180.28 KB, 2394x1844 - viewed 10 times.)
« Last Edit: July 31, 2013, 07:04:25 am by Yinno » Logged

New Hampshire (USA)
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is an image of just the tester output:


* Tester Ouput.JPG (25.43 KB, 521x261 - viewed 12 times.)
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I assume those white spaces in the output are tabs ('\t')? In that case, I would just create a buffer big enough to store a single line. Store incoming bytes into the buffer sequentially. When you receive the new line character, terminate the buffer with a null ('\0') and run through the various tab deliminated tokens with strtok(), and use strcmp() to look for "FLOW". The next token will be the value, and you can pass it to atoi() to get the numeric value.
Logged

New Hampshire (USA)
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...create a buffer...terminate the buffer with a null ('\0')...strtok()...strcmp()...atoi() to get the numeric value.

Arrch, I really appreciate the help! I don't have the understanding to execute your suggestions, so I just ordered a copy of the "Arduino Cookbook" so I can start at page 1 and get a grip on programming (I know there are  ton of free online resources, but a physical book would be easier for me). I spend a lot of time trying to guess my way through programming and I'm hoping that investing some time educating myself will pay off. This project is for work, so if I can't get the code written soon, I will pay for someone to write it.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arrch, I really appreciate the help! I don't have the understanding to execute your suggestions, so I just ordered a copy of the "Arduino Cookbook" so I can start at page 1 and get a grip on programming (I know there are  ton of free online resources, but a physical book would be easier for me). I spend a lot of time trying to guess my way through programming and I'm hoping that investing some time educating myself will pay off.

Thoroughly exploring and working through a lot of the examples also helps get a better understanding of the programming, but it's going to take time and effort to put everything together. The String object might make it a bit easier, and I'm sure Zoomkat will be by shortly to post vaguely relevant example code that uses Strings, but this typically isn't a good idea on a system like the Arduino with very limited memory.

Quote
This project is for work, so if I can't get the code written soon, I will pay for someone to write it.

If you're more interested in getting the project completed rather than learning, then this seems like the more sensible approach. The Gigs and Collaboration would be the place to go for that sort of thing.
Logged

Atlanta, USA
Offline Offline
Edison Member
*
Karma: 33
Posts: 1424
AKA: Ray Burne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just like parsing GOS serial... Take a search on GPS and study the examples.
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8911
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and I'm sure Zoomkat will be by shortly to post vaguely relevant example code that uses Strings,

If I did, wouldn't that make "no code Arrch" look kind of lame?  smiley-cool
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and I'm sure Zoomkat will be by shortly to post vaguely relevant example code that uses Strings,

If I did, wouldn't that make "no code Arrch" look kind of lame?  smiley-cool

I don't think so. Not in comparison to "vaguely relevant with no explanation Zoomkat", at least.
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8911
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and I'm sure Zoomkat will be by shortly to post vaguely relevant example code that uses Strings,

If I did, wouldn't that make "no code Arrch" look kind of lame?  smiley-cool

I don't think so. Not in comparison to "vaguely relevant with no explanation Zoomkat", at least.

Sooooo sad!  smiley-cool
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

New Hampshire (USA)
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The String object might make it a bit easier...but this typically isn't a good idea on a system like the Arduino with very limited memory.

As I'm new to programming, I was actually pretty psyched to learn about Strings (SubString etc.) because it was easier to understand.

However, until everyone is 100% on board with using Strings, I would not use them; especially since this program would be for work and it wouldn't be a good situation if it gave bad results or crashed.
Logged

Pages: [1]   Go Up
Jump to: