How to send a number as a number or How to rule an LED

form form at nonsanity.com
Fri Feb 4 12:18:52 EST 2011


Try:

write numtochar( tValue ) to file thePort

This will only work for values less than 256, however. You can increase that
range by breaking the number down into individual bytes and sending those in
sequence.

What you should do instead is send the number as ascii as you are now, but
end it with a CR. Then on the Arduino side, you wait till you get the CR or
other end of line character and then add up the values to recreate the
original number. Or if the Arduino has sscanf or something similar, use that
to extract the numerical value from the text string.

It's a lot more work on the Arduino side to send the data as ascii text, but
it allows you to send any sort of number you like. It also makes the stream
human-readable.

Best practice would be to write down a set "language" that the two sides
will communicate in, such as:

LED 125<CR>
BEEP 2<CR>

Then have one parser that does nothing but read the serial port, extracts
the function name and the parameter, and sends them off to the right place
to be executed. Here's a no-Arduino-knowledge guess:

char cmd[255];
int param = 0;
char buf[255];
int bufloc = 0;
while ( true )
{
    buf[ bufloc ] = Serial.Read();
    if ( buf[ bufloc ] == 13 ) // CR EOL
    {
        sscanf( buf, "%s %i", &cmd, &param );
        if ( !strcmp( cmd, "LED" ) ) // if cmd == "LED"
            DoLed( param );
        else if ( !strcmp( cmd, "BEEP" ) ) // if cmd == "BEEP"
            DoBeep( param );

        memset( buf, 0, sizeof( buf ) ); // clear the buffer
        bufloc = 0; // start at the beginning again
    }
    else
        ++bufloc; // next read will go in the next buf char
}


 ~ Chris Innanen
 ~ Nonsanity


On Fri, Feb 4, 2011 at 11:22 AM, Thomas McGrath III <3mcgrath at comcast.net>wrote:

> I am having a problem communicating with an LED. (Never thought I'd hear
> myself say that!)
>
> I have code in LC in a slider:
> -- We are open driver ... for text update
> on mouseUp
>    put the thumbPosition of me into tValue
>    doUpdate(tValue)
> end mouseUp
> on doUpdate tValue
>    put gThePort into thePort -- global port "/dev/cu.usbmodem1a21"
>    read from file thePort until empty -- clear the buffer
>    put it into field "Data" -- just to see what garbage is left over - not
> needed
>    wait 30
>    write tValue to file thePort -- Vaule is 0 -255 as per the Analog specs
> of a PWM on the Arduino Uno for an LED
>    read from file thePort until empty -- Arduino is set to send back the
> data received this is so I can see it
>    put it after field "Data"
>    if the result is not empty then put the result into field "Results" --
> gives us our eof
> end doUpdate
>
> I send out a 125 but I get back three lines:
> 1
> 2
> 5
> but the analogWrite is looking for 125
>
> void loop() {
>  // check if data has been sent from the computer:
>  if (Serial.available()) {
>    // read the most recent byte (which will be from 0 to 255):
>    char received = Serial.read();
>
>    // set the brightness of the LED:
>    analogWrite(ledPin, received);
>    Serial.println(received);
>  }
> }
>
> If I change the Serial.println to Serial.println(received, BYTE); I still
> get:
> 1
> 2
> 5
> But if I change it to Serial.println(received, DEC); I get these results:
> 49
> 50
> 53
>
> So How do I send a number as a number and not as single digits?
>
>
> -- Tom McGrath III
> http://lazyriver.on-rev.com
> 3mcgrath at comcast.net
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



More information about the use-livecode mailing list