Page 1 of 1

Serial struggle

Posted: Fri Dec 19, 2008 2:19 am
by hamlynart
Hi Folks,

I'm still struggling away with my RunRev Arduino Microcontroller interface. Probably way over my head but too excited by the possibilities to give up. I've been struggling all day with something which doesn't make any sense to me. The microcontroller is set to send lines of data (Serial.Println()) to the serial port. The data reports on a potentiometer and reads from 0 to 1024.

I have the following code in RunRev to read the incomming numbers:

Code: Select all

on readPort
         set the numberformat to "0000"
  if the hilite of btn "Port open" = true then
    put the label of btn "Port" into thePort
    read from driver thePort until 100
    if it is not empty then put it into recVar --and it is a number
    put the number of lines in recVar into fld "recLines"
    repeat with x=1 to the number of lines in recVar
      put item 1 of line x of recVar & "," before line 1 of tList
      put max (tList) into fld "AinFld0"
   end repeat
    send readPort to me in 50 milliseconds 
end if
end readPort
The problem is that when the potentiometer/microcontroller reads 1000 to 1010 I get a reading of 100 and if I leave it on this reading too long it gets stuck and I have to reset the port. I also get frequent errors on starting read from the port because of non numbers. However if my script checks if the data is a number I don't get any readings at all!

Does any of this make sense because I'm at a complete loss:?

Any thoughts or advice much appreciated.

Jim H

Posted: Fri Dec 19, 2008 3:08 pm
by hamlynart
Well a good night's sleep can do a world of good!

Code: Select all

local recEOL = empty

on resetEOL
  put "CR" into recEOL
put numToChar(13) into recEOL
end resetEOL

on readPort
  if the hilite of btn "Port open" = true then
    if recEOL is empty then resetEOL
    put the label of btn "Port" into thePort
    if the hilite of btn "OS X" then
      read from driver thePort until recEOL
    else
      read from file thePort until recEOL
    end if
    if it is not empty then put it into fld "recField"
    repeat with x=2 to the number of lines in fld"recField"-5
       put fld "recField" into maxVar
             put item 1 of line x of maxVar & "," before line 1 of tList
      put max (tList) into fld "maxField"
    end repeat
    if last char of fld "recField" <> cr then put cr after fld "recField"
    put the number of lines in fld "recField" into fld "iField"
    send readPort to me in 5 ticks
  end if
end readPort
Cheers

Jim H