Permalink
Browse files

Codenow arduino solutions

  • Loading branch information...
1 parent b0e26c6 commit d41a25c26c6433e84d90b0c7ff99b347fb194f48 @jcasimir jcasimir committed Nov 16, 2012
Showing with 78 additions and 0 deletions.
  1. +58 −0 source/codenow/blink.rb
  2. +20 −0 source/codenow/nightlight.rb
View
@@ -0,0 +1,58 @@
+require 'dino'
+
+board = Dino::Board.new(Dino::TxRx.new)
+lower_led = Dino::Components::Led.new(pin: 11, board: board)
+upper_led = Dino::Components::Led.new(pin: 10, board: board)
+
+def blink(led)
+ led.send(:on)
+ sleep 0.1
+ led.send(:off)
+ sleep 0.1
+end
+
+def celebrate()
+ led.send(:on)
+ sleep 0.1
+ led.send(:off)
+ sleep 0.1
+end
+
+def celebrate(leds)
+ 10.times do
+ leds.each do |led|
+ led.send(:on)
+ end
+
+ sleep 0.05
+
+ leds.each do |led|
+ led.send(:off)
+ end
+
+ sleep 0.05
+ end
+end
+
+secret = rand(101)
+puts "Guess a number from 0 to 100:"
+guess = gets.chomp.to_i
+until guess == secret
+ difference = secret-guess
+ if difference < 0
+ led = upper_led
+ else
+ led = lower_led
+ end
+
+ if difference.abs > 15
+ 3.times{ blink(led) }
+ elsif difference.abs >= 5
+ 2.times{ blink(led) }
+ else
+ blink(led)
+ end
+ puts "Guess a number from 0 to 100:"
+ guess = gets.chomp.to_i
+end
+celebrate([lower_led, upper_led])
@@ -0,0 +1,20 @@
+require 'dino'
+
+board = Dino::Board.new(Dino::TxRx.new)
+lower_led = Dino::Components::Led.new(pin: 11, board: board)
+upper_led = Dino::Components::Led.new(pin: 10, board: board)
+sensor = Dino::Components::Sensor.new(pin: 'A0', board: board)
+
+on_data = Proc.new do |data|
+ if data.to_i < 100
+ #puts "It's light!"
+ upper_led.send(:off)
+ else
+ #puts "It's dark!"
+ upper_led.send(:on)
+ end
+end
+
+sensor.when_data_received(on_data)
+
+sleep

0 comments on commit d41a25c

Please sign in to comment.