Created
January 7, 2010 22:53
-
-
Save lostcaggy/271688 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ledpwmbutton < ArduinoSketch | |
#Turn on LED when the button is pressed and keep it on after is is | |
#released, if the button is held brightness changes | |
#page 61 of getting started with arduino | |
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 = 1 - @state #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 | |
if @val == 1 && @old_val == 1 then | |
if @state ==1 && (millis - @startTime) > 500 then | |
@brightness +=1 #increment brightness by 1 | |
delay 10 | |
if @brightness > 255 then | |
@brightness = 0 #if we go over 255 lets go back to 0 | |
end | |
end | |
end | |
@old_val = @val #val is now old lets store it | |
if @state == 1 then | |
analogWrite led, @brightness #turn led on at the current brightness level | |
else | |
analogWrite led, 0 #turn led off | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment