Support Python3.

Add supporting Python3 , while supporting Python2.7 .
This commit is contained in:
Suhayl
2018-08-03 17:24:29 +08:00
parent e902e7f76d
commit dc5fe9a90f
35 changed files with 158 additions and 155 deletions
+5 -5
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : Blink.py
# Description : Make an led blinking.
# auther : www.freenove.com
# modification: 2016/06/07
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import time
@@ -14,15 +14,15 @@ def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(ledPin, GPIO.OUT) # Set ledPin's mode is output
GPIO.output(ledPin, GPIO.LOW) # Set ledPin low to off led
print 'using pin%d'%ledPin
print ('using pin%d'%ledPin)
def loop():
while True:
GPIO.output(ledPin, GPIO.HIGH) # led on
print '...led on'
print ('...led on')
time.sleep(1)
GPIO.output(ledPin, GPIO.LOW) # led off
print 'led off...'
print ('led off...')
time.sleep(1)
def destroy():
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : ButtonLED.py
# Description : Controlling an led by button.
# Author : freenove
# modification: 2016/06/12
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
@@ -11,7 +11,7 @@ ledPin = 11 # define the ledPin
buttonPin = 12 # define the buttonPin
def setup():
print 'Program is starting...'
print ('Program is starting...')
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(ledPin, GPIO.OUT) # Set ledPin's mode is output
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set buttonPin's mode is input, and pull up to high level(3.3V)
@@ -20,10 +20,10 @@ def loop():
while True:
if GPIO.input(buttonPin)==GPIO.LOW:
GPIO.output(ledPin,GPIO.HIGH)
print 'led on ...'
print ('led on ...')
else :
GPIO.output(ledPin,GPIO.LOW)
print 'led off ...'
print ('led off ...')
def destroy():
GPIO.output(ledPin, GPIO.LOW) # led off
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : Tablelamp.py
# Description : a DIY MINI table lamp
# Author : freenove
# modification: 2016/06/12
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
@@ -12,19 +12,19 @@ buttonPin = 12 # define the buttonPin
ledState = False
def setup():
print 'Program is starting...'
print ('Program is starting...')
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(ledPin, GPIO.OUT) # Set ledPin's mode is output
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set buttonPin's mode is input, and pull up to high
def buttonEvent(channel):#When the button is pressed, this function will be executed
global ledState
print 'buttonEvent GPIO%d'%channel
print ('buttonEvent GPIO%d' %channel)
ledState = not ledState
if ledState :
print 'Turn on LED ... '
print ('Turn on LED ... ')
else :
print 'Turn off LED ... '
print ('Turn off LED ... ')
GPIO.output(ledPin,ledState)
def loop():
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : LightWater.py
# Description : Display 10 LEDBar Graph
# Author : freenove
# modification: 2016/06/13
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import time
@@ -11,7 +11,7 @@ import time
ledPins = [11, 12, 13, 15, 16, 18, 22, 3, 5, 24]
def setup():
print 'Program is starting...'
print ('Program is starting...')
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
for pin in ledPins:
GPIO.setup(pin, GPIO.OUT) # Set all ledPins' mode is output
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : BreathingLED.py
# Description : A breathing LED
# Author : freenove
# modification: 2016/06/14
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import time
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : ColorfulLED.py
# Description : A auto flash ColorfulLED
# Author : freenove
# modification: 2016/06/15
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import time
@@ -13,7 +13,7 @@ pins = {'pin_R':11, 'pin_G':12, 'pin_B':13} # pins is a dict
def setup():
global p_R,p_G,p_B
print 'Program is starting ... '
print ('Program is starting ... ')
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
for i in pins:
GPIO.setup(pins[i], GPIO.OUT) # Set pins' mode is output
@@ -36,7 +36,7 @@ def loop():
g=random.randint(0,100)
b=random.randint(0,100)
setColor(r,g,b)#set random as a duty cycle value
print 'r=%d, g=%d, b=%d '%(r ,g, b)
print ('r=%d, g=%d, b=%d ' %(r ,g, b))
time.sleep(0.3)
def destroy():
+5 -5
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : Doorbell.py
# Description : Controlling an buzzer by button.
# Author : freenove
# modification: 2016/06/12
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
@@ -11,7 +11,7 @@ buzzerPin = 11 # define the buzzerPin
buttonPin = 12 # define the buttonPin
def setup():
print 'Program is starting...'
print ('Program is starting...')
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(buzzerPin, GPIO.OUT) # Set buzzerPin's mode is output
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set buttonPin's mode is input, and pull up to high level(3.3V)
@@ -20,10 +20,10 @@ def loop():
while True:
if GPIO.input(buttonPin)==GPIO.LOW:
GPIO.output(buzzerPin,GPIO.HIGH)
print 'buzzer on ...'
print ('buzzer on ...')
else :
GPIO.output(buzzerPin,GPIO.LOW)
print 'buzzer off ...'
print ('buzzer off ...')
def destroy():
GPIO.output(buzzerPin, GPIO.LOW) # buzzer off
+5 -5
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : Alertor.py
# Description : Alarm by button.
# Author : freenove
# modification: 2016/06/14
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import time
@@ -14,7 +14,7 @@ buttonPin = 12 # define the buttonPin
def setup():
global p
print 'Program is starting...'
print ('Program is starting...')
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(buzzerPin, GPIO.OUT) # Set buzzerPin's mode is output
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set buttonPin's mode is input, and pull up to high level(3.3V)
@@ -25,10 +25,10 @@ def loop():
while True:
if GPIO.input(buttonPin)==GPIO.LOW:
alertor()
print 'buzzer on ...'
print ('buzzer on ...')
else :
stopAlertor()
print 'buzzer off ...'
print ('buzzer off ...')
def alertor():
p.start(50)
for x in range(0,361): #frequency of the alarm along the sine wave change
+4 -4
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : PCF8591.py
# Description : ADC and DAC
# Author : freenove
# modification: 2016/06/18
# modification: 2018/08/02
########################################################################
import smbus
import time
@@ -24,14 +24,14 @@ def loop():
value = analogRead(0) #read the ADC value of channel 0
analogWrite(value) #write the DAC value
voltage = value / 255.0 * 3.3 #calculate the voltage value
print 'ADC Value : %d, Voltage : %.2f'%(value,voltage)
print ('ADC Value : %d, Voltage : %.2f'%(value,voltage))
time.sleep(0.01)
def destroy():
bus.close()
if __name__ == '__main__':
print 'Program is starting ... '
print ('Program is starting ... ')
try:
loop()
except KeyboardInterrupt:
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : Softlight.py
# Description : Potentiometer control LED
# Author : freenove
# modification: 2016/06/18
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import smbus
@@ -35,7 +35,7 @@ def loop():
value = analogRead(0) #read A0 pin
p.ChangeDutyCycle(value*100/255) #Convert ADC value to duty cycle of PWM
voltage = value / 255.0 * 3.3 #calculate voltage
print 'ADC Value : %d, Voltage : %.2f'%(value,voltage)
print ('ADC Value : %d, Voltage : %.2f'%(value,voltage))
time.sleep(0.01)
def destroy():
@@ -43,7 +43,7 @@ def destroy():
GPIO.cleanup()
if __name__ == '__main__':
print 'Program is starting ... '
print ('Program is starting ... ')
setup()
try:
loop()
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : Softlight.py
# Description : Potentiometer control LED
# Author : freenove
# modification: 2016/06/18
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import smbus
@@ -49,7 +49,7 @@ def loop():
p_Green.ChangeDutyCycle(value_Green*100/255)
p_Blue.ChangeDutyCycle(value_Blue*100/255)
#print read ADC value
print 'ADC Value value_Red: %d ,\tvlue_Green: %d ,\tvalue_Blue: %d'%(value_Red,value_Green,value_Blue)
print ('ADC Value value_Red: %d ,\tvlue_Green: %d ,\tvalue_Blue: %d'%(value_Red,value_Green,value_Blue))
time.sleep(0.01)
def destroy():
@@ -57,7 +57,7 @@ def destroy():
GPIO.cleanup()
if __name__ == '__main__':
print 'Program is starting ... '
print ('Program is starting ... ')
setup()
try:
loop()
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : Nightlamp.py
# Description : Photoresistor control LED
# Author : freenove
# modification: 2016/06/18
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import smbus
@@ -35,7 +35,7 @@ def loop():
value = analogRead(0)
p.ChangeDutyCycle(value*100/255)
voltage = value / 255.0 * 3.3
print 'ADC Value : %d, Voltage : %.2f'%(value,voltage)
print ('ADC Value : %d, Voltage : %.2f'%(value,voltage))
time.sleep(0.01)
def destroy():
@@ -43,7 +43,7 @@ def destroy():
GPIO.cleanup()
if __name__ == '__main__':
print 'Program is starting ... '
print ('Program is starting ... ')
setup()
try:
loop()
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : Thermometer.py
# Description : A DIY Thermometer
# Author : freenove
# modification: 2016/06/20
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import smbus
@@ -31,14 +31,14 @@ def loop():
Rt = 10 * voltage / (3.3 - voltage) #calculate resistance value of thermistor
tempK = 1/(1/(273.15 + 25) + math.log(Rt/10)/3950.0) #calculate temperature (Kelvin)
tempC = tempK -273.15 #calculate temperature (Celsius)
print 'ADC Value : %d, Voltage : %.2f, Temperature : %.2f'%(value,voltage,tempC)
print ('ADC Value : %d, Voltage : %.2f, Temperature : %.2f'%(value,voltage,tempC))
time.sleep(0.01)
def destroy():
GPIO.cleanup()
if __name__ == '__main__':
print 'Program is starting ... '
print ('Program is starting ... ')
setup()
try:
loop()
+4 -4
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : Joystick.py
# Description : Read Joystick
# Author : freenove
# modification: 2016/06/18
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import smbus
@@ -32,7 +32,7 @@ def loop():
val_Z = GPIO.input(Z_Pin) #read digital quality of axis Z
val_Y = analogRead(0) #read analog quality of axis X and Y
val_X = analogRead(1)
print 'value_X: %d ,\tvlue_Y: %d ,\tvalue_Z: %d'%(val_X,val_Y,val_Z)
print ('value_X: %d ,\tvlue_Y: %d ,\tvalue_Z: %d'%(val_X,val_Y,val_Z))
time.sleep(0.01)
def destroy():
@@ -40,7 +40,7 @@ def destroy():
GPIO.cleanup()
if __name__ == '__main__':
print 'Program is starting ... '
print ('Program is starting ... ')
setup()
try:
loop()
+8 -8
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : Motor.py
# Description : Control Motor by L293D
# Author : freenove
# modification: 2016/06/21
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import smbus
@@ -43,22 +43,22 @@ def motor(ADC):
if (value > 0):
GPIO.output(motoRPin1,GPIO.HIGH)
GPIO.output(motoRPin2,GPIO.LOW)
print 'Turn Forward...'
print ('Turn Forward...')
elif (value < 0):
GPIO.output(motoRPin1,GPIO.LOW)
GPIO.output(motoRPin2,GPIO.HIGH)
print 'Turn Backward...'
print ('Turn Backward...')
else :
GPIO.output(motoRPin1,GPIO.LOW)
GPIO.output(motoRPin2,GPIO.LOW)
print 'Motor Stop...'
print ('Motor Stop...')
p.start(mapNUM(abs(value),0,128,0,100))
print 'The PWM duty cycle is %d%%\n'%(abs(value)*100/127) #print PMW duty cycle.
print ('The PWM duty cycle is %d%%\n'%(abs(value)*100/127)) #print PMW duty cycle.
def loop():
while True:
value = analogRead(0)
print 'ADC Value : %d'%(value)
print ('ADC Value : %d'%(value))
motor(value)
time.sleep(0.01)
@@ -67,7 +67,7 @@ def destroy():
GPIO.cleanup()
if __name__ == '__main__':
print 'Program is starting ... '
print ('Program is starting ... ')
setup()
try:
loop()
+6 -6
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : Relay.py
# Description : Button control Relay and Motor
# Author : freenove
# modification: 2016/07/04
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
@@ -12,19 +12,19 @@ buttonPin = 12 # define the buttonPin
relayState = False
def setup():
print 'Program is starting...'
print ('Program is starting...')
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(relayPin, GPIO.OUT) # Set relayPin's mode is output
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set buttonPin's mode is input, and pull up to high
def buttonEvent(channel):
global relayState
print 'buttonEvent GPIO%d'%channel
print ('buttonEvent GPIO%d'%channel)
relayState = not relayState
if relayState :
print 'Turn on relay ... '
print ('Turn on relay ... ')
else :
print 'Turn off relay ... '
print ('Turn off relay ... ')
GPIO.output(relayPin,relayState)
def loop():
+3 -3
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : Sweep.py
# Description : Servo sweep
# Author : freenove
# modification: 2016/07/06
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import time
@@ -47,7 +47,7 @@ def destroy():
GPIO.cleanup()
if __name__ == '__main__': #Program start from here
print 'Program is starting...'
print ('Program is starting...')
setup()
try:
loop()
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : SteppingMotor.py
# Description :
# Author : freenove
# modification: 2016/07/07
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import time
@@ -13,7 +13,7 @@ CCWStep = (0x01,0x02,0x04,0x08) #define power supply order for coil for rotating
CWStep = (0x08,0x04,0x02,0x01) #define power supply order for coil for rotating clockwise
def setup():
print 'Program is starting...'
print ('Program is starting...')
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
for pin in motorPins:
GPIO.setup(pin,GPIO.OUT)
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : LightWater02.py
# Description : Control LED by 74HC595
# Author : freenove
# modification: 2016/06/23
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import time
@@ -51,7 +51,7 @@ def destroy(): # When 'Ctrl+C' is pressed, the function is executed.
GPIO.cleanup()
if __name__ == '__main__': # Program starting from here
print 'Program is starting...'
print ('Program is starting...' )
setup()
try:
loop()
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : SevenSegmentDisplay.py
# Description : Control SevenSegmentDisplay by 74HC595
# Author : freenove
# modification: 2016/06/24
# modification: 2018/08/02
########################################################################
import RPi.GPIO as GPIO
import time
@@ -48,7 +48,7 @@ def destroy(): # When 'Ctrl+C' is pressed, the function is executed.
GPIO.cleanup()
if __name__ == '__main__': # Program starting from here
print 'Program is starting...'
print ('Program is starting...' )
setup()
try:
loop()
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : StopWatch.py
# Description : Control 4_Digit_7_Segment_Display by 74HC595
# Author : freenove
# modification: 2016/07/08
# modification: 2018/08/03
########################################################################
import RPi.GPIO as GPIO
import time
@@ -16,9 +16,9 @@ dataPin = 18 #DS Pin of 74HC595(Pin14)
latchPin = 16 #ST_CP Pin of 74HC595(Pin12)
clockPin = 12 #SH_CP Pin of 74HC595(Pin11)
num = (0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90)
digitPin = (19,15,13,11) # Define the pin of 7-segment display common end
digitPin = (11,13,15,19) # Define the pin of 7-segment display common end
counter = 0 # Variable counter, the number will be dislayed by 7-segment display
t = 0 # defien the Timer object
t = 0 # define the Timer object
def setup():
GPIO.setmode(GPIO.BOARD) # Number GPIOs by its physical location
GPIO.setup(dataPin, GPIO.OUT) # Set pin mode to output
@@ -54,15 +54,15 @@ def display(dec): #display function for 7-segment display
time.sleep(0.003) #display duration
outData(0xff)
selectDigit(0x02) # Select the second, and display the tens digit
outData(num[dec%100/10])
outData(num[dec%100//10])
time.sleep(0.003)
outData(0xff)
selectDigit(0x04) # Select the third, and display the hundreds digit
outData(num[dec%1000/100])
outData(num[dec%1000//100])
time.sleep(0.003)
outData(0xff)
selectDigit(0x08) # Select the fourth, and display the thousands digit
outData(num[dec%10000/1000])
outData(num[dec%10000//1000])
time.sleep(0.003)
def timer(): #timer function
global counter
@@ -70,7 +70,7 @@ def timer(): #timer function
t = threading.Timer(1.0,timer) #reset time of timer to 1s
t.start() #Start timing
counter+=1
print "counter : %d"%counter
print ("counter : %d"%counter)
def loop():
global t
@@ -86,7 +86,7 @@ def destroy(): # When "Ctrl+C" is pressed, the function is executed.
t.cancel() #cancel the timer
if __name__ == '__main__': # Program starting from here
print 'Program is starting...'
print ('Program is starting...' )
setup()
try:
loop()
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : LEDMatrix.py
# Description : Control LEDMatrix by 74HC595
# Author : freenove
# modification: 2016/06/24
# modification: 2018/08/03
########################################################################
import RPi.GPIO as GPIO
import time
@@ -56,9 +56,9 @@ def loop():
x=0x80
for i in range(0,8):
GPIO.output(latchPin,GPIO.LOW)
shiftOut(dataPin,clockPin,LSBFIRST,pic[i]) #first shift data of line information to first stage 74HC959
shiftOut(dataPin,clockPin,MSBFIRST,pic[i]) #first shift data of line information to first stage 74HC959
shiftOut(dataPin,clockPin,LSBFIRST,~x) #then shift data of column information to second stage 74HC959
shiftOut(dataPin,clockPin,MSBFIRST,~x) #then shift data of column information to second stage 74HC959
GPIO.output(latchPin,GPIO.HIGH)# Output data of two stage 74HC595 at the same time
time.sleep(0.001)# display the next column
x>>=1
@@ -67,15 +67,15 @@ def loop():
x=0x80 # Set the column information to start from the first column
for i in range(k,k+8):
GPIO.output(latchPin,GPIO.LOW)
shiftOut(dataPin,clockPin,LSBFIRST,data[i])
shiftOut(dataPin,clockPin,LSBFIRST,~x)
shiftOut(dataPin,clockPin,MSBFIRST,data[i])
shiftOut(dataPin,clockPin,MSBFIRST,~x)
GPIO.output(latchPin,GPIO.HIGH)
time.sleep(0.001)
x>>=1
def destroy(): # When 'Ctrl+C' is pressed, the function is executed.
GPIO.cleanup()
if __name__ == '__main__': # Program starting from here
print 'Program is starting...'
print ('Program is starting...' )
setup()
try:
loop()
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : I2CLCD1602.py
# Description : Use the LCD display data
# Author : freenove
# modification: 2017/04/18
# modification: 2018/08/03
########################################################################
from PCF8574 import PCF8574_GPIO
from Adafruit_LCD1602 import Adafruit_CharLCD
@@ -42,13 +42,13 @@ except:
try:
mcp = PCF8574_GPIO(PCF8574A_address)
except:
print 'I2C Address Error !'
print ('I2C Address Error !')
exit(1)
# Create LCD, passing in MCP GPIO adapter.
lcd = Adafruit_CharLCD(pin_rs=0, pin_e=2, pins_db=[4,5,6,7], GPIO=mcp)
if __name__ == '__main__':
print 'Program is starting ... '
print ('Program is starting ... ')
try:
loop()
except KeyboardInterrupt:
@@ -2,7 +2,7 @@
# Filename : PCF8574.py
# Description : PCF8574 as Raspberry GPIO
# Author : freenove
# modification: 2017/04/18
# modification: 2018/08/03
########################################################################
import smbus
import time
@@ -42,11 +42,11 @@ def loop():
while True:
#mcp.writeByte(0xff)
mcp.digitalWrite(3,1)
print 'Is 0xff? %x'%(mcp.readByte())
print ('Is 0xff? %x'%(mcp.readByte()))
time.sleep(1)
mcp.writeByte(0x00)
#mcp.digitalWrite(7,1)
print 'Is 0x00? %x'%(mcp.readByte())
print ('Is 0x00? %x'%(mcp.readByte()))
time.sleep(1)
class PCF8574_GPIO(object):#Standardization function interface
@@ -70,7 +70,7 @@ def destroy():
bus.close()
if __name__ == '__main__':
print 'Program is starting ... '
print ('Program is starting ... ')
try:
loop()
except KeyboardInterrupt:
Binary file not shown.
+9 -9
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : DHT11.py
# Description : read the temperature and humidity data of DHT11
# Author : freenove
# modification: 2018/03/07
# modification: 2018/08/03
########################################################################
import RPi.GPIO as GPIO
import time
@@ -16,21 +16,21 @@ def loop():
while(True):
sumCnt += 1 #counting number of reading times
chk = dht.readDHT11() #read DHT11 and get a return value. Then determine whether data read is normal according to the return value.
print"The sumCnt is : %d, \t chk : %d"%(sumCnt,chk)
print ("The sumCnt is : %d, \t chk : %d"%(sumCnt,chk))
if (chk is dht.DHTLIB_OK): #read DHT11 and get a return value. Then determine whether data read is normal according to the return value.
print"DHT11,OK!"
print("DHT11,OK!")
elif(chk is dht.DHTLIB_ERROR_CHECKSUM): #data check has errors
print"DHTLIB_ERROR_CHECKSUM!!"
print("DHTLIB_ERROR_CHECKSUM!!")
elif(chk is dht.DHTLIB_ERROR_TIMEOUT): #reading DHT times out
print"DHTLIB_ERROR_TIMEOUT!"
print("DHTLIB_ERROR_TIMEOUT!")
else: #other errors
print"Other error!"
print("Other error!")
print"Humidity : %.2f, \t Temperature : %.2f \n"%(dht.humidity,dht.temperature)
print("Humidity : %.2f, \t Temperature : %.2f \n"%(dht.humidity,dht.temperature))
time.sleep(2)
if __name__ == '__main__':
print 'Program is starting ... '
print ('Program is starting ... ')
try:
loop()
except KeyboardInterrupt:
+11 -10
View File
@@ -1,8 +1,9 @@
#!/usr/bin/env python3
#############################################################################
# Filename : Freenove_DHT.py
# Description : DHT Temperature & Humidity Sensor library for Raspberry
# Author : freenove
# modification: 2018/03/07
# modification: 2018/08/03
########################################################################
import RPi.GPIO as GPIO
import time
@@ -39,32 +40,32 @@ class DHT(object):
t = time.time()
while(GPIO.input(pin) == GPIO.LOW):
if((time.time() - t) > loopCnt):
#print "Echo LOW"
#print ("Echo LOW")
return self.DHTLIB_ERROR_TIMEOUT
t = time.time()
while(GPIO.input(pin) == GPIO.HIGH):
if((time.time() - t) > loopCnt):
#print "Echo HIGH"
#print ("Echo HIGH")
return self.DHTLIB_ERROR_TIMEOUT
for i in range(0,40,1):
t = time.time()
while(GPIO.input(pin) == GPIO.LOW):
if((time.time() - t) > loopCnt):
#print "Data Low %d"%(i)
#print ("Data Low %d"%(i))
return self.DHTLIB_ERROR_TIMEOUT
t = time.time()
while(GPIO.input(pin) == GPIO.HIGH):
if((time.time() - t) > loopCnt):
#print "Data HIGH %d"%(i)
#print ("Data HIGH %d"%(i))
return self.DHTLIB_ERROR_TIMEOUT
if((time.time() - t) > 0.00005):
self.bits[idx] |= mask
#print"t : %f"%(time.time()-t)
#print("t : %f"%(time.time()-t))
mask >>= 1
if(mask == 0):
mask = 0x80
idx += 1
#print self.bits
#print (self.bits)
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin,GPIO.HIGH)
return self.DHTLIB_OK
@@ -92,12 +93,12 @@ def loop():
if (chk is 0):
okCnt += 1
okRate = 100.0*okCnt/sumCnt;
print"sumCnt : %d, \t okRate : %.2f%% "%(sumCnt,okRate)
print"chk : %d, \t Humidity : %.2f, \t Temperature : %.2f "%(chk,dht.humidity,dht.temperature)
print("sumCnt : %d, \t okRate : %.2f%% "%(sumCnt,okRate))
print("chk : %d, \t Humidity : %.2f, \t Temperature : %.2f "%(chk,dht.humidity,dht.temperature))
time.sleep(3)
if __name__ == '__main__':
print 'Program is starting ... '
print ('Program is starting ... ')
try:
loop()
except KeyboardInterrupt:
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
########################################################################
# Filename : Keypad.py
# Description : The module of matrix keypad
@@ -186,15 +187,15 @@ rowsPins = [12,16,18,22]
colsPins = [19,15,13,11]
def loop():
keypad = Keypad.Keypad(keys,rowsPins,colsPins,ROWS,COLS)
keypad = Keypad(keys,rowsPins,colsPins,ROWS,COLS)
keypad.setDebounceTime(50)
while(True):
key = keypad.getKey()
if(key != keypad.NULL):
print "You Pressed Key : %c "%(key)
print ("You Pressed Key : %c "%(key) )
if __name__ == '__main__': # Program start from here
print "Program is starting ... "
print ("Program is starting ... ")
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
@@ -1,8 +1,9 @@
#!/usr/bin/env python3
########################################################################
# Filename : MatrixKeypad.py
# Description : obtain the key code of 4x4 Matrix Keypad
# Author : freenove
# modification: 2016/07/13
# modification: 2018/08/03
########################################################################
import RPi.GPIO as GPIO
import Keypad #import module Keypad
@@ -21,11 +22,11 @@ def loop():
while(True):
key = keypad.getKey() #obtain the state of keys
if(key != keypad.NULL): #if there is key pressed, print its key code.
print "You Pressed Key : %c "%(key)
print ("You Pressed Key : %c "%(key))
if __name__ == '__main__': #Program start from here
print "Program is starting ... "
print ("Program is starting ... ")
try:
loop()
except KeyboardInterrupt: #When 'Ctrl+C' is pressed, exit the program.
GPIO.cleanup()
except KeyboardInterrupt: #When 'Ctrl+C' is pressed, exit the program.
GPIO.cleanup()
+5 -5
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : SenseLED.py
# Description : Controlling an led by infrared Motion sensor.
# Author : freenove
# modification: 2016/06/12
# modification: 2018/08/03
########################################################################
import RPi.GPIO as GPIO
@@ -11,7 +11,7 @@ ledPin = 12 # define the ledPin
sensorPin = 11 # define the sensorPin
def setup():
print 'Program is starting...'
print ('Program is starting...')
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(ledPin, GPIO.OUT) # Set ledPin's mode is output
GPIO.setup(sensorPin, GPIO.IN) # Set sensorPin's mode is input
@@ -20,10 +20,10 @@ def loop():
while True:
if GPIO.input(sensorPin)==GPIO.HIGH:
GPIO.output(ledPin,GPIO.HIGH)
print 'led on ...'
print ('led on ...')
else :
GPIO.output(ledPin,GPIO.LOW)
print 'led off ...'
print ('led off ...')
def destroy():
GPIO.cleanup() # Release resource
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : UltrasonicRanging.py
# Description : Get distance from UltrasonicRanging.
# Author : freenove
# modification: 2016/07/15
# modification: 2018/08/03
########################################################################
import RPi.GPIO as GPIO
import time
@@ -34,7 +34,7 @@ def getSonar(): #get the measurement results of ultrasonic module,with unit:
return distance
def setup():
print 'Program is starting...'
print ('Program is starting...')
GPIO.setmode(GPIO.BOARD) #numbers GPIOs by physical location
GPIO.setup(trigPin, GPIO.OUT) #
GPIO.setup(echoPin, GPIO.IN) #
@@ -43,7 +43,7 @@ def loop():
GPIO.setup(11,GPIO.IN)
while(True):
distance = getSonar()
print "The distance is : %.2f cm"%(distance)
print ("The distance is : %.2f cm"%(distance))
time.sleep(1)
if __name__ == '__main__': #program start from here
+1 -1
View File
@@ -138,7 +138,7 @@ class MPU6050:
# Attempt to bypass adafruit lib
#a_data_list = self.__mpu.bus.read_i2c_block_data(0x68, a_address, a_length)
#print('data' + str(a_data_list))
for x in xrange(0, a_length):
for x in range(0, a_length):
a_data_list[x] = self.__bus.read_byte_data(self.__dev_id,
a_address + x)
return a_data_list
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
########################################################################
# Filename : MPU6050RAW.py
# Description : Read the Raw data of MPU6050.
# Author : freenove
# modification: 2016/07/18
# modification: 2018/08/03
########################################################################
import MPU6050
import time
@@ -18,13 +18,13 @@ def loop():
while(True):
accel = mpu.get_acceleration() #get accelerometer data
gyro = mpu.get_rotation() #get gyroscope data
print"a/g:%d\t%d\t%d\t%d\t%d\t%d "%(accel[0],accel[1],accel[2],gyro[0],gyro[1],gyro[2])
print"a/g:%.2f g\t%.2f g\t%.2f g\t%.2f d/s\t%.2f d/s\t%.2f d/s"%(accel[0]/16384.0,accel[1]/16384.0,
accel[2]/16384.0,gyro[0]/131.0,gyro[1]/131.0,gyro[2]/131.0)
print("a/g:%d\t%d\t%d\t%d\t%d\t%d "%(accel[0],accel[1],accel[2],gyro[0],gyro[1],gyro[2]))
print("a/g:%.2f g\t%.2f g\t%.2f g\t%.2f d/s\t%.2f d/s\t%.2f d/s"%(accel[0]/16384.0,accel[1]/16384.0,
accel[2]/16384.0,gyro[0]/131.0,gyro[1]/131.0,gyro[2]/131.0))
time.sleep(0.1)
if __name__ == '__main__': # Program start from here
print"Program is starting ... "
print("Program is starting ... ")
setup()
try:
loop()
@@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#############################################################################
# Filename : LightWater03.py
# Description : Control LED by 74HC595 on the DIY circuit board
# Author : freenove
# modification: 2016/08/16
# modification: 2018/08/03
########################################################################
import RPi.GPIO as GPIO
import time
@@ -59,7 +59,7 @@ def destroy(): # When 'Ctrl+C' is pressed, the function is executed.
GPIO.cleanup()
if __name__ == '__main__': # Program starting from here
print 'Program is starting...'
print ('Program is starting...')
setup()
try:
loop()