Skip to content

Instantly share code, notes, and snippets.

@Raspberry765 Raspberry765/power_led
Last active Aug 6, 2017

What would you like to do?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
from time import sleep
import signal
import sys
sclk = 11
miso = 9
mosi = 10
ce0 = 8
LED_pin = 4
GPIO.setup(sclk, GPIO.OUT)
GPIO.setup(miso, GPIO.IN)
GPIO.setup(mosi, GPIO.OUT)
GPIO.setup(ce0, GPIO.OUT)
GPIO.setup(LED_pin, GPIO.OUT)
def read(adcnum, sclk, mosi, miso, ce0): #cdsによって光を検出する関数を定義
if adcnum > 7 or adcnum < 0:
return -1
GPIO.output(ce0, GPIO.HIGH)
GPIO.output(sclk, GPIO.LOW)
GPIO.output(ce0, GPIO.LOW)
commandout = adcnum
commandout |= 0x18
commandout <<= 3
for i in range(5):
if commandout & 0x80:
GPIO.output(mosi, GPIO.HIGH)
GPIO.output(mosi, GPIO.LOW)
commandout <<= 1
GPIO.output(sclk, GPIO.HIGH)
GPIO.output(sclk, GPIO.LOW)
adcout = 0
for i in range(13):
GPIO.output(sclk, GPIO.HIGH)
GPIO.output(sclk, GPIO.LOW)
adcout <<= 1
if i>0 and GPIO.input(miso) == GPIO.HIGH:
adcout |= 0x1
GPIO.output(ce0, GPIO.HIGH)
return adcout
while True:
data = read(0, sclk, mosi, miso, ce0) #ドアが開いたことを確認する光データを取得
print('data = %s'% data)
if data < 1300:
GPIO.output(LED_pin , GPIO.HIGH) #led on
GPIO.output(LED_pin , GPIO.LOW) #led off
except KeyboardInterrupt:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.