Example sketch to read analog inputs

1 view
Skip to first unread message

David Michael

unread,
Apr 3, 2008, 10:35:28 PM4/3/08
to Ruby Arduino Development
This sketch reads from 2 analog inputs, blinks an LED on pin 13 in
response to an analog input reaching a threshold value, and writes the
results back to the serial port (here the computer).

The one problem is that I was unable to assign variables from
analogRead in the loop like so:

light = analogRead(ldr)

which throws the error:
error: invalid conversion from 'int' to 'void*'

Is this a bug?


------------------------------------------------------------------------

# Example sketch to read analog inputs
#
# David M Michael
# April 3, 2008
# http://unnature.net

class AnalogInputs < ArduinoSketch
input_pin 0, :as => :potentiometer
input_pin 3, :as => :ldr
output_pin 13, :as => :led
vars :light => 0, :threshold => 512

serial_begin

def loop
# light = analogRead(ldr) # <-- this throws an error!
# Blink the LED to test the photoresistor
(analogRead(ldr) < threshold) ? digitalWrite(led, 1) :
digitalWrite(led, 0)
# Here is the message back to the computer in the format 'int,int'
serial_print analogRead(ldr)
serial_print(",")
serial_println analogRead(potentiometer)
# Lets slow it down, shall we?
delay 30
end
end

Greg Borenstein

unread,
Apr 4, 2008, 12:29:09 AM4/4/08
to ruby-arduino...@googlegroups.com
So, here you're starting to run into some of the limitations of RAD's
process for translating your sketch into C. RubyToC (the library that
I use to translate the loop method) has to do some funny stuff due to
the dynamic nature of Ruby. In this case, since it can't tell the type
of the return value of analogRead and since 'light' has not been
initialized to anything whose type it can detect, it goes ahead and
takes a crazy shot in the dark by declaring light as a void *. I bet
if you look in your .cpp file that it's generating, you'll see
something along those lines.

One workaround for this problem is to trick RubyToC into correctly
declaring your variable by assigning it to something that is obviously
an integer on the line above:

light = 0
light = analogRead(ldr)

I think that should do the trick. If not (or if this is too gross for
us, which it is to me), we'll have to look into giving declaration
instructions to RubyToC. Unfortunately, RubyToC is pretty advanced
stuff and is only mildly penetrable to me. If you wanna dig in there,
someone having a greater understanding of RubyToC would be an
invaluable resource to RAD's further development.

-- Greg

David Michael

unread,
Apr 4, 2008, 11:51:08 AM4/4/08
to Ruby Arduino Development

Yep, that works for assignment of analogRead to a variable called
'light'.
But the following does not work:

light = 0
light = analogRead ldr
serial_print light # -> call of overloaded 'serial_print(long
int&)' is ambiguous

I found a fix to this by adding an extra overloaded function to the
serial_boilerplate method in the arduino_sketch.rb

out << "void serial_print( long i) {"
out << "\treturn Serial.print( i );"
out << "}"

By adding that, I was actually able to get the whole statmement chain
working with the light var declared outside the loop:

light = analogRead ldr
(light < threshold) ? digitalWrite(led, 1) : digitalWrite(led, 0)
serial_print light

So in this case at least, it seems that an integer declaration is of
type long int in C - as is the return value from analogRead()
> > #http://unnature.net

Greg Borenstein

unread,
Apr 4, 2008, 1:23:32 PM4/4/08
to ruby-arduino...@googlegroups.com
Nice! I bet there's a boat load of those signatures we still need to
add for each of the overloaded library functions. I'll think a little
bit about a helper for that kind of thing to reduce some of this code
duplication

In the meantime, if you wrap that up in a patch, I can apply it to the
current svn and work it into the next release.

I'm planning to move the RAD source into Git (and onto github.com)
pretty soon, which will make creating and applying contributions like
this much easier.

Thanks David!

-- Greg

Reply all
Reply to author
Forward
0 new messages