Skip to content

Instantly share code, notes, and snippets.

@alfredfrancis
Forked from guyc/capswitch.py
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfredfrancis/c8fe591d7150ee33d62e to your computer and use it in GitHub Desktop.
Save alfredfrancis/c8fe591d7150ee33d62e to your computer and use it in GitHub Desktop.
import wiringpi
import time
# 1M external pull-up resistor
# 1) set pin low and to output to discharge
# 2) make the pin an input without the internal pull-up on
# 3) read input and see how long it takes to go high
# ref: https://github.com/WiringPi/WiringPi-Python
# pins: https://projects.drogon.net/raspberry-pi/wiringpi/pins/
# functions: https://projects.drogon.net/raspberry-pi/wiringpi/functions/
def set_led(is_on):
steps = 100
for i in range(0,steps):
v1 = int((i+0.0)/steps * 100)
v2 = 100 - v1
if is_on:
wiringpi.softPwmWrite(r_pin, v1)
wiringpi.softPwmWrite(b_pin, v2)
else:
wiringpi.softPwmWrite(r_pin, v2)
wiringpi.softPwmWrite(b_pin, v1)
time.sleep(0.005)
def toggle_led(led_on):
led_on = 1 - led_on
set_led(led_on)
return led_on
io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_PINS)
v_pin = 2
s_pin = 3
r_pin = 4
b_pin = 5
io.pinMode(s_pin, io.OUTPUT)
io.pinMode(v_pin, io.OUTPUT)
io.digitalWrite(v_pin, io.HIGH)
r_pwm = wiringpi.softPwmCreate(r_pin, 0, 100)
b_pwm = wiringpi.softPwmCreate(b_pin, 0, 100)
led_on = True
set_led(led_on)
while 1:
repeats = 2
total = 0.0
for i in range(0,repeats):
# 1) set pin low and to output to discharge
io.pinMode(s_pin, io.OUTPUT)
io.digitalWrite(s_pin, io.LOW)
# 2) make the pin an input without the internal pull-up on
io.pinMode(s_pin, io.INPUT)
io.pullUpDnControl(s_pin, io.PUD_OFF)
# 3) read input and see how long it takes to go high
maxCycles = 1000
cycles = 0
while (cycles<maxCycles and io.digitalRead(s_pin)==0):
cycles+=1;
total += cycles
mean = total / repeats
#print mean
#time.sleep(0.05)
if (mean > 40):
log_time()
print mean
led_on = toggle_led(led_on)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment