diff --git a/Code/Python_Code/01.1.1_Blink/Blink.py b/Code/Python_Code/01.1.1_Blink/Blink.py index d9caa7f..e0996b9 100644 --- a/Code/Python_Code/01.1.1_Blink/Blink.py +++ b/Code/Python_Code/01.1.1_Blink/Blink.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : Blink.py # Description : Make an led blinking. # auther : www.freenove.com -# modification: 2018/08/02 +# modification: 2016/06/07 ######################################################################## 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(): diff --git a/Code/Python_Code/02.1.1_ButtonLED/ButtonLED.py b/Code/Python_Code/02.1.1_ButtonLED/ButtonLED.py index 65d9bad..bcb53ec 100644 --- a/Code/Python_Code/02.1.1_ButtonLED/ButtonLED.py +++ b/Code/Python_Code/02.1.1_ButtonLED/ButtonLED.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : ButtonLED.py # Description : Controlling an led by button. # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/12 ######################################################################## 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 diff --git a/Code/Python_Code/02.2.1_Tablelamp/Tablelamp.py b/Code/Python_Code/02.2.1_Tablelamp/Tablelamp.py index 12d2bf0..a5b70e0 100644 --- a/Code/Python_Code/02.2.1_Tablelamp/Tablelamp.py +++ b/Code/Python_Code/02.2.1_Tablelamp/Tablelamp.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : Tablelamp.py # Description : a DIY MINI table lamp # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/12 ######################################################################## 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(): diff --git a/Code/Python_Code/03.1.1_LightWater/LightWater.py b/Code/Python_Code/03.1.1_LightWater/LightWater.py index 1212533..f0f3fc8 100644 --- a/Code/Python_Code/03.1.1_LightWater/LightWater.py +++ b/Code/Python_Code/03.1.1_LightWater/LightWater.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : LightWater.py # Description : Display 10 LEDBar Graph # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/13 ######################################################################## 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 diff --git a/Code/Python_Code/04.1.1_BreathingLED/BreathingLED.py b/Code/Python_Code/04.1.1_BreathingLED/BreathingLED.py index ad24fea..1597e2c 100644 --- a/Code/Python_Code/04.1.1_BreathingLED/BreathingLED.py +++ b/Code/Python_Code/04.1.1_BreathingLED/BreathingLED.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : BreathingLED.py # Description : A breathing LED # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/14 ######################################################################## import RPi.GPIO as GPIO import time diff --git a/Code/Python_Code/05.1.1_ColorfulLED/ColorfulLED.py b/Code/Python_Code/05.1.1_ColorfulLED/ColorfulLED.py index 6733286..ccdcd5c 100644 --- a/Code/Python_Code/05.1.1_ColorfulLED/ColorfulLED.py +++ b/Code/Python_Code/05.1.1_ColorfulLED/ColorfulLED.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : ColorfulLED.py # Description : A auto flash ColorfulLED # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/15 ######################################################################## 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(): diff --git a/Code/Python_Code/06.1.1_Doorbell/Doorbell.py b/Code/Python_Code/06.1.1_Doorbell/Doorbell.py index e951384..c038f9e 100644 --- a/Code/Python_Code/06.1.1_Doorbell/Doorbell.py +++ b/Code/Python_Code/06.1.1_Doorbell/Doorbell.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : Doorbell.py # Description : Controlling an buzzer by button. # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/12 ######################################################################## 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 diff --git a/Code/Python_Code/06.2.1_Alertor/Alertor.py b/Code/Python_Code/06.2.1_Alertor/Alertor.py index 73ea16f..bbfb6e6 100644 --- a/Code/Python_Code/06.2.1_Alertor/Alertor.py +++ b/Code/Python_Code/06.2.1_Alertor/Alertor.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : Alertor.py # Description : Alarm by button. # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/14 ######################################################################## 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 diff --git a/Code/Python_Code/07.1.1_PCF8591/PCF8591.py b/Code/Python_Code/07.1.1_PCF8591/PCF8591.py index e4381fb..80e0be1 100644 --- a/Code/Python_Code/07.1.1_PCF8591/PCF8591.py +++ b/Code/Python_Code/07.1.1_PCF8591/PCF8591.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : PCF8591.py # Description : ADC and DAC # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/18 ######################################################################## 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: diff --git a/Code/Python_Code/08.1.1_SoftLight/SoftLight.py b/Code/Python_Code/08.1.1_SoftLight/SoftLight.py index 7a3cd88..fed1222 100644 --- a/Code/Python_Code/08.1.1_SoftLight/SoftLight.py +++ b/Code/Python_Code/08.1.1_SoftLight/SoftLight.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : Softlight.py # Description : Potentiometer control LED # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/18 ######################################################################## 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() diff --git a/Code/Python_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.py b/Code/Python_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.py index 57e951d..4278aae 100644 --- a/Code/Python_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.py +++ b/Code/Python_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : Softlight.py # Description : Potentiometer control LED # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/18 ######################################################################## 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() diff --git a/Code/Python_Code/10.1.1_Nightlamp/Nightlamp.py b/Code/Python_Code/10.1.1_Nightlamp/Nightlamp.py index 5579e66..86b28c5 100644 --- a/Code/Python_Code/10.1.1_Nightlamp/Nightlamp.py +++ b/Code/Python_Code/10.1.1_Nightlamp/Nightlamp.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : Nightlamp.py # Description : Photoresistor control LED # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/18 ######################################################################## 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() diff --git a/Code/Python_Code/11.1.1_Thermometer/Thermometer.py b/Code/Python_Code/11.1.1_Thermometer/Thermometer.py index c489ed9..dd0259e 100644 --- a/Code/Python_Code/11.1.1_Thermometer/Thermometer.py +++ b/Code/Python_Code/11.1.1_Thermometer/Thermometer.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : Thermometer.py # Description : A DIY Thermometer # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/20 ######################################################################## 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() diff --git a/Code/Python_Code/12.1.1_Joystick/Joystick.py b/Code/Python_Code/12.1.1_Joystick/Joystick.py index c367e32..7509463 100644 --- a/Code/Python_Code/12.1.1_Joystick/Joystick.py +++ b/Code/Python_Code/12.1.1_Joystick/Joystick.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : Joystick.py # Description : Read Joystick # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/18 ######################################################################## 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() diff --git a/Code/Python_Code/13.1.1_Motor/Motor.py b/Code/Python_Code/13.1.1_Motor/Motor.py index feb17f0..42c9e40 100644 --- a/Code/Python_Code/13.1.1_Motor/Motor.py +++ b/Code/Python_Code/13.1.1_Motor/Motor.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : Motor.py # Description : Control Motor by L293D # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/21 ######################################################################## 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() diff --git a/Code/Python_Code/14.1.1_Relay/Relay.py b/Code/Python_Code/14.1.1_Relay/Relay.py index 615497b..6dec590 100644 --- a/Code/Python_Code/14.1.1_Relay/Relay.py +++ b/Code/Python_Code/14.1.1_Relay/Relay.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : Relay.py # Description : Button control Relay and Motor # Author : freenove -# modification: 2018/08/02 +# modification: 2016/07/04 ######################################################################## 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(): diff --git a/Code/Python_Code/15.1.1_Sweep/Sweep.py b/Code/Python_Code/15.1.1_Sweep/Sweep.py index d561224..89f941a 100644 --- a/Code/Python_Code/15.1.1_Sweep/Sweep.py +++ b/Code/Python_Code/15.1.1_Sweep/Sweep.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : Sweep.py # Description : Servo sweep # Author : freenove -# modification: 2018/08/02 +# modification: 2016/07/06 ######################################################################## 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() diff --git a/Code/Python_Code/16.1.1_SteppingMotor/SteppingMotor.py b/Code/Python_Code/16.1.1_SteppingMotor/SteppingMotor.py index 9b06933..67992df 100644 --- a/Code/Python_Code/16.1.1_SteppingMotor/SteppingMotor.py +++ b/Code/Python_Code/16.1.1_SteppingMotor/SteppingMotor.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : SteppingMotor.py # Description : # Author : freenove -# modification: 2018/08/02 +# modification: 2016/07/07 ######################################################################## 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) diff --git a/Code/Python_Code/17.1.1_LightWater02/LightWater02.py b/Code/Python_Code/17.1.1_LightWater02/LightWater02.py index f0d7861..97b7dce 100644 --- a/Code/Python_Code/17.1.1_LightWater02/LightWater02.py +++ b/Code/Python_Code/17.1.1_LightWater02/LightWater02.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : LightWater02.py # Description : Control LED by 74HC595 # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/23 ######################################################################## 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() diff --git a/Code/Python_Code/18.1.1_SevenSegmentDisplay/SevenSegmentDisplay.py b/Code/Python_Code/18.1.1_SevenSegmentDisplay/SevenSegmentDisplay.py index a08da9a..d7200a2 100644 --- a/Code/Python_Code/18.1.1_SevenSegmentDisplay/SevenSegmentDisplay.py +++ b/Code/Python_Code/18.1.1_SevenSegmentDisplay/SevenSegmentDisplay.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : SevenSegmentDisplay.py # Description : Control SevenSegmentDisplay by 74HC595 # Author : freenove -# modification: 2018/08/02 +# modification: 2016/06/24 ######################################################################## 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() diff --git a/Code/Python_Code/18.2.1_StopWatch/StopWatch.py b/Code/Python_Code/18.2.1_StopWatch/StopWatch.py index c2bcd75..8409b4f 100644 --- a/Code/Python_Code/18.2.1_StopWatch/StopWatch.py +++ b/Code/Python_Code/18.2.1_StopWatch/StopWatch.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : StopWatch.py # Description : Control 4_Digit_7_Segment_Display by 74HC595 # Author : freenove -# modification: 2018/08/03 +# modification: 2016/07/08 ######################################################################## 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 = (11,13,15,19) # Define the pin of 7-segment display common end +digitPin = (19,15,13,11) # Define the pin of 7-segment display common end counter = 0 # Variable counter, the number will be dislayed by 7-segment display -t = 0 # define the Timer object +t = 0 # defien 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() diff --git a/Code/Python_Code/19.1.1_LEDMatrix/LEDMatrix.py b/Code/Python_Code/19.1.1_LEDMatrix/LEDMatrix.py index 2b8a042..9ee08f3 100644 --- a/Code/Python_Code/19.1.1_LEDMatrix/LEDMatrix.py +++ b/Code/Python_Code/19.1.1_LEDMatrix/LEDMatrix.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : LEDMatrix.py # Description : Control LEDMatrix by 74HC595 # Author : freenove -# modification: 2018/08/03 +# modification: 2016/06/24 ######################################################################## 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,MSBFIRST,pic[i]) #first shift data of line information to first stage 74HC959 + shiftOut(dataPin,clockPin,LSBFIRST,pic[i]) #first shift data of line information to first stage 74HC959 - shiftOut(dataPin,clockPin,MSBFIRST,~x) #then shift data of column information to second stage 74HC959 + shiftOut(dataPin,clockPin,LSBFIRST,~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,MSBFIRST,data[i]) - shiftOut(dataPin,clockPin,MSBFIRST,~x) + shiftOut(dataPin,clockPin,LSBFIRST,data[i]) + shiftOut(dataPin,clockPin,LSBFIRST,~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() diff --git a/Code/Python_Code/20.1.1_I2CLCD1602/Adafruit_LCD1602.pyc b/Code/Python_Code/20.1.1_I2CLCD1602/Adafruit_LCD1602.pyc new file mode 100644 index 0000000..2e53d78 Binary files /dev/null and b/Code/Python_Code/20.1.1_I2CLCD1602/Adafruit_LCD1602.pyc differ diff --git a/Code/Python_Code/20.1.1_I2CLCD1602/I2CLCD1602.py b/Code/Python_Code/20.1.1_I2CLCD1602/I2CLCD1602.py index 4e4472b..eb48bda 100644 --- a/Code/Python_Code/20.1.1_I2CLCD1602/I2CLCD1602.py +++ b/Code/Python_Code/20.1.1_I2CLCD1602/I2CLCD1602.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : I2CLCD1602.py # Description : Use the LCD display data # Author : freenove -# modification: 2018/08/03 +# modification: 2017/04/18 ######################################################################## 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: diff --git a/Code/Python_Code/20.1.1_I2CLCD1602/PCF8574.py b/Code/Python_Code/20.1.1_I2CLCD1602/PCF8574.py index 262d9b6..50fb5ce 100644 --- a/Code/Python_Code/20.1.1_I2CLCD1602/PCF8574.py +++ b/Code/Python_Code/20.1.1_I2CLCD1602/PCF8574.py @@ -2,7 +2,7 @@ # Filename : PCF8574.py # Description : PCF8574 as Raspberry GPIO # Author : freenove -# modification: 2018/08/03 +# modification: 2017/04/18 ######################################################################## 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: diff --git a/Code/Python_Code/20.1.1_I2CLCD1602/PCF8574.pyc b/Code/Python_Code/20.1.1_I2CLCD1602/PCF8574.pyc new file mode 100644 index 0000000..f06d07c Binary files /dev/null and b/Code/Python_Code/20.1.1_I2CLCD1602/PCF8574.pyc differ diff --git a/Code/Python_Code/21.1.1_DHT11/DHT11.py b/Code/Python_Code/21.1.1_DHT11/DHT11.py index d8a16a9..31640eb 100644 --- a/Code/Python_Code/21.1.1_DHT11/DHT11.py +++ b/Code/Python_Code/21.1.1_DHT11/DHT11.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : DHT11.py # Description : read the temperature and humidity data of DHT11 # Author : freenove -# modification: 2018/08/03 +# modification: 2018/03/07 ######################################################################## 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: diff --git a/Code/Python_Code/21.1.1_DHT11/Freenove_DHT.py b/Code/Python_Code/21.1.1_DHT11/Freenove_DHT.py index 8fd9571..472bb93 100644 --- a/Code/Python_Code/21.1.1_DHT11/Freenove_DHT.py +++ b/Code/Python_Code/21.1.1_DHT11/Freenove_DHT.py @@ -1,9 +1,8 @@ -#!/usr/bin/env python3 ############################################################################# # Filename : Freenove_DHT.py # Description : DHT Temperature & Humidity Sensor library for Raspberry # Author : freenove -# modification: 2018/08/03 +# modification: 2018/03/07 ######################################################################## import RPi.GPIO as GPIO import time @@ -40,32 +39,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 @@ -93,12 +92,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: diff --git a/Code/Python_Code/22.1.1_MatrixKeypad/Keypad.py b/Code/Python_Code/22.1.1_MatrixKeypad/Keypad.py index 5c0729b..202be7a 100644 --- a/Code/Python_Code/22.1.1_MatrixKeypad/Keypad.py +++ b/Code/Python_Code/22.1.1_MatrixKeypad/Keypad.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 ######################################################################## # Filename : Keypad.py # Description : The module of matrix keypad @@ -187,15 +186,15 @@ rowsPins = [12,16,18,22] colsPins = [19,15,13,11] def loop(): - keypad = Keypad(keys,rowsPins,colsPins,ROWS,COLS) + keypad = 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. diff --git a/Code/Python_Code/22.1.1_MatrixKeypad/MatrixKeypad.py b/Code/Python_Code/22.1.1_MatrixKeypad/MatrixKeypad.py index db852f8..1141fab 100644 --- a/Code/Python_Code/22.1.1_MatrixKeypad/MatrixKeypad.py +++ b/Code/Python_Code/22.1.1_MatrixKeypad/MatrixKeypad.py @@ -1,9 +1,8 @@ -#!/usr/bin/env python3 ######################################################################## # Filename : MatrixKeypad.py # Description : obtain the key code of 4x4 Matrix Keypad # Author : freenove -# modification: 2018/08/03 +# modification: 2016/07/13 ######################################################################## import RPi.GPIO as GPIO import Keypad #import module Keypad @@ -22,11 +21,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() diff --git a/Code/Python_Code/23.1.1_SenseLED/SenseLED.py b/Code/Python_Code/23.1.1_SenseLED/SenseLED.py index 0444125..625f395 100644 --- a/Code/Python_Code/23.1.1_SenseLED/SenseLED.py +++ b/Code/Python_Code/23.1.1_SenseLED/SenseLED.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : SenseLED.py # Description : Controlling an led by infrared Motion sensor. # Author : freenove -# modification: 2018/08/03 +# modification: 2016/06/12 ######################################################################## 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 diff --git a/Code/Python_Code/24.1.1_UtrasonicRanging/UtrasonicRanging.py b/Code/Python_Code/24.1.1_UtrasonicRanging/UtrasonicRanging.py index 0115c04..b7858fb 100644 --- a/Code/Python_Code/24.1.1_UtrasonicRanging/UtrasonicRanging.py +++ b/Code/Python_Code/24.1.1_UtrasonicRanging/UtrasonicRanging.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : UltrasonicRanging.py # Description : Get distance from UltrasonicRanging. # Author : freenove -# modification: 2018/08/03 +# modification: 2016/07/15 ######################################################################## 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 diff --git a/Code/Python_Code/25.1.1_MPU6050/MPU6050.py b/Code/Python_Code/25.1.1_MPU6050/MPU6050.py index a826113..4accc0e 100644 --- a/Code/Python_Code/25.1.1_MPU6050/MPU6050.py +++ b/Code/Python_Code/25.1.1_MPU6050/MPU6050.py @@ -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 range(0, a_length): + for x in xrange(0, a_length): a_data_list[x] = self.__bus.read_byte_data(self.__dev_id, a_address + x) return a_data_list diff --git a/Code/Python_Code/25.1.1_MPU6050/MPU6050RAW.py b/Code/Python_Code/25.1.1_MPU6050/MPU6050RAW.py index f426cf7..5dd5ae0 100644 --- a/Code/Python_Code/25.1.1_MPU6050/MPU6050RAW.py +++ b/Code/Python_Code/25.1.1_MPU6050/MPU6050RAW.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ######################################################################## # Filename : MPU6050RAW.py # Description : Read the Raw data of MPU6050. # Author : freenove -# modification: 2018/08/03 +# modification: 2016/07/18 ######################################################################## 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() diff --git a/Code/Python_Code/27.2.1_LightWater03/LightWater03.py b/Code/Python_Code/27.2.1_LightWater03/LightWater03.py index b34f18b..07e6c66 100644 --- a/Code/Python_Code/27.2.1_LightWater03/LightWater03.py +++ b/Code/Python_Code/27.2.1_LightWater03/LightWater03.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python ############################################################################# # Filename : LightWater03.py # Description : Control LED by 74HC595 on the DIY circuit board # Author : freenove -# modification: 2018/08/03 +# modification: 2016/08/16 ######################################################################## 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()