error with if then statement

3 views
Skip to first unread message

lostcaggy

unread,
Dec 24, 2009, 2:33:31 PM12/24/09
to Ruby Arduino Development
Hi with following code

output_pin 9, :as => :led #pin for led
input_pin 7, :as => :button_one, :device => :button #input pin
of pushbutton

@val = 0 #state of input pin
@old_val = 0 #stores previous value of @val
@state = 0 # 0 = LED off while 1 = LED on

@brightness = 128 #stores brightness value
@startTime = 0 #when did we begin pressing

def loop
@val = digitalRead button_one #read input and store it

#check if there was a transition
if @val == 1 && @old_val == 0 then #this is the line causing
problems

@state = @state - 1 #changes state from off to on and vice
versa

@startTime = millis #millis is the arduino clock returns
number of milliseconds since board was reset
#remembers when button was last pressed
delay 10
end

end

getting following error

private method `gsub' called for nil:NilClass

can someone tell me what I am doing wrong, is there a way to find out
which control statements
or ruby subset work reliably with rad

thanks
Martin

JD Barnhart

unread,
Dec 24, 2009, 5:32:56 PM12/24/09
to ruby-arduino...@googlegroups.com
Martin,

Check out the class DebounceMethods < ArduinoSketch example -- when you declare an input as a button you have a default debounce -- so all you need is:


  output_pin 9, :as => :led
  input_pin 7, :as => :button_one, :device => :button # debounce may be adjusted with  :adjust => 300 
 
     def loop
       do_something if read_input button_one
     end
    
     def do_something
        2.times do |i|
          led.blink 200 + i
        end
      end
     
   end

Hope that helps a bit.



--

You received this message because you are subscribed to the Google Groups "Ruby Arduino Development" group.
To post to this group, send email to ruby-arduino...@googlegroups.com.
To unsubscribe from this group, send email to ruby-arduino-devel...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ruby-arduino-development?hl=en.



Reply all
Reply to author
Forward
0 new messages