-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathmcp3008.rb
55 lines (46 loc) · 1.22 KB
/
mcp3008.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'pi_piper'
#port of the Adafruit MCP3008 interface code found @ http://learn.adafruit.com/send-raspberry-pi-data-to-cosm/python-script
def read_adc(adc_pin, clockpin, adc_in, adc_out, cspin)
cspin.on
clockpin.off
cspin.off
command_out = adc_pin
command_out |= 0x18
command_out <<= 3
(0..4).each do
adc_in.update_value((command_out & 0x80) > 0)
command_out <<= 1
clockpin.on
clockpin.off
end
result = 0
(0..11).each do
clockpin.on
clockpin.off
result <<= 1
adc_out.read
if adc_out.on?
result |= 0x1
end
end
cspin.on
result >> 1
end
clock = PiPiper::Pin.new :pin => 18, :direction => :out
adc_out = PiPiper::Pin.new :pin => 23
adc_in = PiPiper::Pin.new :pin => 24, :direction => :out
cs = PiPiper::Pin.new :pin => 25, :direction => :out
adc_pin = 0
loop do
value = read_adc(adc_pin, clock, adc_in, adc_out, cs)
invert = 1023 - value
mvolts = invert * (3300.0 / 1023.0)
if mvolts < 2700
temp = (mvolts - 380.0) / (2320.0 / 84.0)
else
temp = (mvolts - 2700.0) / (390.0 / 92.0) + 84.0
end
temp_f = (temp * 9.0 / 5.0) + 32
puts "Value = #{value}, invert = #{invert}, mvolts = #{mvolts}, temp = #{temp} C | #{temp_f} F"
sleep 1
end