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

Claudi Cornaz claudi.c at fiberworld.nl
Sat Feb 5 17:12:58 EST 2011


Tom,

Yeah, yeah, that's all certainly possible. 
At last I am getting somewhere, a relaiable connection and communication with the arduino with all kinds of data.

At the moment I work on a demo which has 2 sensors attached to the arduino and a led on the arduino

From LC I can send data from a scrollbar to set the brightnes of the led this is like "<d126>"  ( the d char to indicate it's data that follows)
I can also send "<cp>" or "<cl>" or "<cn>" to signal the arduino into switching to the potentiometer, the LDR or no sensor (the slider in LC get's used)
Arduino will switch to the right sensor and start sendeing it's data (this is the part I am working on now), or stop sending anything when it recieved "<cn>"

I also have a third command to send to the arduino like "<r200>" which means set the delay in the loop routine to 200 msecs. 
(so I can start playing with how often data is send to LC)

the atoi() function converts a ascci str to a integer so the str "125" gets converted to a integer with the value of 125

I use tDataStr[8] so I can recieve 7 bytes (1 cmd char and 6 data bytes and the terminating 0 char) It can be anything you like.

My code won't win a price for ellegance but I am quit proud of myself getting it to work. This is my first encounter with C and
I just start getting the simpelest notion of pointer arithmetic. In half a year my code will look a lot better I geuss.

My next try is to send values in the 10 bits precision of the AD converters to LC.

My demo stack is still a bit rought but you can download as it is now from: http://dl.dropbox.com/u/13685013/Sensor_Test.pde

As soon as I have it completly working I will upload to revOn line.
The stack has 3 cd's card 1 is a simple echo but shows how to make connection so LC knows it has connection
and the arduino will know aswell and enter into the loop() routine which just echo's what it recieves.
card 2 allows you to send some data to the arduno which again will echo the incomming data but now I use start and end chars
The third card is with the sensors. This is the one which iI am working on now.
I hope it will help you.

Best wishes,
     Claudi





On 5 feb 2011, at 17:37, Thomas McGrath III wrote:

> Claudi,
> 
> What is your atoi() code like? Why tDataStr[8] ? is that the number of chars you can read in? can that be more?
> 
> What if we want to send "<13-125>" can we parse that to separate out the 13 for Pin 13 and the 125 for brightness?
> 
> What about "<13-1-125>" and "<13-0-1>" for pin 13 output 125 and 13 input read once 1 or / continuos 0. etc.
> 
> 
> -- Tom McGrath III
> http://lazyriver.on-rev.com
> 3mcgrath at comcast.net
> 
> On Feb 5, 2011, at 7:19 AM, Claudi Cornaz wrote:
> 
>> 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
>> 
>> 
>> _______________________________________________
>> 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
> 
> 
> _______________________________________________
> 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