-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# | ||
# Example of reading an HC-SR04 ultrasonic sensor. | ||
# | ||
require 'bundler/setup' | ||
require 'denko' | ||
|
||
board = Denko::Board.new(Denko::Connection::Serial.new) | ||
hcsr04 = Denko::Sensor::HCSR04.new(board: board, pins: {trigger: 6, echo: 7}) | ||
|
||
hcsr04.poll(0.05) do |distance| | ||
puts "Distance: #{distance} mm" | ||
end | ||
|
||
sleep |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Denko | ||
module Sensor | ||
class HCSR04 | ||
# Speed of sound in meters per second. | ||
SPEED_OF_SOUND = 343.0 | ||
|
||
include Behaviors::MultiPin | ||
include Behaviors::Poller | ||
|
||
def initialize_pins(options={}) | ||
proxy_pin :trigger, DigitalIO::Output | ||
proxy_pin :echo, DigitalIO::Input | ||
end | ||
|
||
def after_initialize(options={}) | ||
super(options) | ||
|
||
# Receive values from echo pin. | ||
echo.add_callback { |data| self.update(data) } | ||
end | ||
|
||
def _read | ||
board.hcsr04_read(echo.pin, trigger.pin) | ||
end | ||
|
||
def pre_callback_filter(us) | ||
# Data is microseconds roundtrip time. Convert to mm. | ||
um = (us/2) * SPEED_OF_SOUND | ||
mm = um / 1000.0 | ||
end | ||
end | ||
end | ||
end |
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
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
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