Mintoris Forum

Author Topic: Serial data graphing with error checking  (Read 1841 times)

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Serial data graphing with error checking
« on: Sep 13, 2014, 01:01 AM »
The USB serial connection between Mintoris Basic and Arduino opens up a ton of possibilities, but only if you can rely on the data coming from your Arduino. Below is attached an Arduino sketch and a Basic program. The Arduino program reads an analog input, encodes the data in a record (with a checksum) and transmits it to the Android device via USB. The Basic program reads the data record, verifies the data using the checksum and graphs the data. Invalid records are discarded. Each data record contains a time code containing the time of the reading.

The Arduino sketch can be modified to send any number of data values.


-Chuck
« Last Edit: Sep 15, 2014, 08:11 PM by Chuck »

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Serial data graphing with error checking
« Reply #1 on: Sep 14, 2014, 01:00 AM »
Here is the Arduino function that encodes the data into a text record.

Code: [Select]
String encodeData(unsigned long timeStamp, int data0) {
 
  String outputRec = String(timeStamp, DEC);
  outputRec = String(outputRec + "," + String(data0, DEC));
  
//  outputRec = String(outputRec + "," + String(data1, DEC));
// More data values may be added by uncommenting/duplicating the line above
// and passing more data values to this function.
 
  int sum = 0;
  int len = outputRec.length();  
  for(int i = 0; i < len; i++) {
    sum = sum + outputRec.charAt(i);
  }
  
  outputRec = String("DR," + String(sum % 255, DEC) + "," + String(len, DEC) + "," + outputRec);  
  return outputRec;
  
}

And, here is the Mintoris Basic function that decodes the record.

Code: [Select]
Sub decodeData(data$, timeIndex, value)

  ' Check the data record for valid data

  RecID$ = ItemExtract$(data$,0)
  if RecID$ <> "DR" then return false
  data$ = ItemRemove$(data$,0)

  checksum$ = ItemExtract$(data$,0)
  if not isNumber(checksum$) then return false
  checksum = val(checksum$)
  data$ = ItemRemove$(data$,0)

  length$ = ItemExtract$(data$, 0)
  if not isNumber(length$) then return false
  length = val(length$)
  data$ = ItemRemove$(data$,0)
  
  if len(data$) <> length then return false

  ' Calculate the checksum of the remaining record
  sum = 0
  for i = 1 to length
    sum = sum + ascii(mid$(data$, i, 1))
  next
  if sum % 255 <> checksum then return false    

  ' If you get this far you've got good data

  timestamp = val(ItemExtract$(data$,0))

  ' Check if the time offset is within 10 sec of the current time
  ' else recalculate the time offset
  ' This will also fix the time when the Arduino clock wraps back to
  ' zero after about 50 days.
  if abs(Time() - timeOffset + timestamp) > 10000 then
    timeOffset = Time() - timestamp
  endif

  ' Convert the timestamp to actual time
  timestamp = timestamp + timeOffset

  ' Convert timestamp to a time index
  timeIndex = int(timestamp / 250 + 0.5) % 240

  ' Get the actual data
  lastData$ = ItemExtract$(data$,1)

  ' Convert value to volts to volts (5 / 1024)
  value = val(lastData$) * 0.0048828125

  Fixdecimal 3
  lastData$ = str$(value)

  return true

End Sub
« Last Edit: Sep 14, 2014, 01:03 AM by Chuck »