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

Claudi Cornaz claudi.c at fiberworld.nl
Sat Feb 5 06:19:33 CST 2011


Hi tom,

I am working with the same. What I undersrtand and have found on forums etc
you need to use atoi() to convert from type char to int.
You send out 3 chars when sending for instance, 125.
The arduino seial.read routine will fetch 3 chars (125)

I use a startChar 1 or more data chars and then a stopChar like "<125>"
This is what I send to the arduino
Then on the arduino I do serial.read() till I recieve a "<" char.
As soon as I have "<" I start gathering the bytes in a char tDataStr[8] var 
till I see a ">" char. Now I convert this char tDataStr to a int with atoi()
int tDataInt;
tDataInt = atoi(tDataStr);   // now I have a int which I can pass as value to 
analogWrite(ledPin, tDataInt);

At least this is how it is recommended on the forums.

I almost got this working.
By the way I also noticed the following
Use open driver "xxx" for text update  -- same as you

then the first read you do should be in a "for 1 line" or "for x chars"
Once this first read is done with the "for xx" form it looks like something in LC gets initialized right
so subsequent reads can be until empty. 

I am still trying to figure this out more precisly and some real stress testing as soon as I have 
a demo working which involves sending sensor values from the arduino to LC and vice versa
I am almost there. Still struggeling with some C code, but slowly getting the hang of C)
 
I hope to have a nice demo this weekend and will upload it to revOnline once it's working like I want it to.


Best wishes,

   Claudi





On 4 feb 2011, at 17:22, Thomas McGrath III 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