Workshop kit code

None of the above? Put it here! Boop!
gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Workshop kit code

Post by gb4192 » Fri Nov 09, 2018 2:51 pm

Hello,

A few months ago I purchased the YouTube Workshop kit: https://www.modmypi.com/raspberry-pi/se ... rkshop-kit and really enjoyed working with it. I did modify the code and make some progressional versions which I thought I would share here.

As this is my first post, here is the first program '3_blinky.py' from part 3 that does some alternating of the LEDs:

Code: Select all

#!/usr/bin/python

from time import sleep # import the time function from the sleep library
import RPi.GPIO as GPIO # import our GPIO library

GPIO.setmode(GPIO.BCM) # set the board numbering system to BCM

# setup our output pins
GPIO.setup(17,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)

# Alternate the LED's
print "Blue on, Red off"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.LOW)
sleep(1) # sleep for 1 second

print "Red on, Blue off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.HIGH)
sleep(1)

print "Blue on, Red off"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.LOW)
sleep(1) # sleep for 1 second

print "Red on, Blue off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.HIGH)
sleep(1)

# Turn LEDs off
print "lights off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.LOW)
GPIO.cleanup() # The clean-up function will reset all the configurations made in this script.  This will stop the warnings we got from the tutorial 2.
If this post works well, then I'll post more! I've not yet added a GPLv3 license comment yet though, see 'Code license' question in the Q & A on https://www.modmypi.com/raspberry-pi/se ... rkshop-kit.

Cheers,

Gareth

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Sat Nov 10, 2018 1:40 pm

This is a screen shot from Meld (an open source difference application) on Ubuntu (as running without the desktop on the Pi) showing the changes between the supplied code and 3_blinky.py above:

3_blinky_diff.png
3_blinky_diff.png (124.06 KiB) Viewed 440 times

does it help? If so when I post my other code then I'll do the same.

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:26 pm

More code.... 3_blinkies.py....

Code: Select all

#!/usr/bin/python

from time import sleep # import the time function from the sleep library
import RPi.GPIO as GPIO # import our GPIO library

GPIO.setmode(GPIO.BCM) # set the board numbering system to BCM

# setup our output pins
GPIO.setup(17,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)

# Alternate the LED's
print "Blue on, Red off"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.LOW)
sleep(1) # sleep for 1 second

print "Red on, Blue off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.HIGH)
sleep(1)

print "Red on, Blue on"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.HIGH)
sleep(1)

print "Blue on, Red off"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.LOW)
sleep(1) # sleep for 1 second

# Turn LEDs off
print "lights off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.LOW)
GPIO.cleanup() # The clean-up function will reset all the configurations made in this script.  This will stop the warnings we got from the tutorial 2.

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:27 pm

3_blinkyblink.py....

Code: Select all

#!/usr/bin/python

from time import sleep # import the time function from the sleep library
import RPi.GPIO as GPIO # import our GPIO library

GPIO.setmode(GPIO.BCM) # set the board numbering system to BCM

# setup our output pins
GPIO.setup(17,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)

# Turn LEDs on
print "lights on"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.HIGH)
sleep(1) # sleep for 1 second

# Turn LEDs off
print "lights off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.LOW)
sleep(1)

# Turn LEDs on
print "lights on"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.HIGH)
sleep(1)

# Turn LEDs off
print "lights off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.LOW)

print "Blue on, Red off"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.LOW)
sleep(1) # sleep for 1 second

print "Red on, Blue off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.HIGH)
sleep(1)

print "Blue on, Red off"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.LOW)
sleep(1) # sleep for 1 second

print "Red on, Blue off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.HIGH)
sleep(1)

print "Blue on, Red off"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.LOW)
sleep(1) # sleep for 1 second

print "Red on, Blue off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.HIGH)
sleep(1)

print "Red on, Blue on"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.HIGH)
sleep(1)

# Alternate the LED's
print "Blue on, Red off"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.LOW)
sleep(1) # sleep for 1 second

print "Red on, Blue off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.HIGH)
sleep(1)

print "Red on, Blue on"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.HIGH)
sleep(1)

print "Blue on, Red off"
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.LOW)
sleep(1) # sleep for 1 second

# Turn LEDs off
print "lights off"
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.LOW)
GPIO.cleanup() # The clean-up function will reset all the configurations made in this script.  This will stop the warnings we got from the tutorial 2.

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:29 pm

4_button.py - I think I may have tidied things up....

Code: Select all

#!/usr/bin/python

import os
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

# Setup our input pin.
# We use an internal pull up resistor to hold the pin at 3v3, otherwise the inputs value could chatter between high and low.

GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    if ( GPIO.input(10) == False ):
        print("Button Pressed")
        os.system('date') # print the systems date and time
        print GPIO.input(10)
        sleep(5)
    else:
        os.system('clear') # clear the screens text
        print ("Waiting for you to press a button")
        sleep(0.1)

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:30 pm

5_user_input_blink.py

Code: Select all

#!/usr/bin/python

import os
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(17,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)

# Setup variables for user input.
led_choice = 0
count = 0

os.system('clear')

print "Which LED would you like to blink"
print "1: Red?"
print "2: Blue?"

led_choice = input("Choose your option: ") # Ask for an input.

if led_choice == 1:
    os.system('clear')
    print "You picked the Red LED"
    count = input("How many times would you like it to blink?: ")
    while count > 0: # While the value of count is greater than 0.
        GPIO.output(27,GPIO.HIGH)
        sleep(1)
        GPIO.output(27,GPIO.LOW)
        sleep(1)
        count = count - 1 # Reduce the value of count by 1.

if led_choice == 2:
    os.system('clear')
    print "You picked the Blue LED"
    count = input("How many times would you like it to blink?: ")
    while count > 0: # while the value of count is greater than 0.
        GPIO.output(17,GPIO.HIGH)
        sleep(1)
        GPIO.output(17,GPIO.LOW)
        sleep(1)
        count = count - 1 # Reduce the value of count by 1.

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:31 pm

6_morsecode.py

Code: Select all

#!/usr/bin/python

import os
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(22,GPIO.OUT)

loop_count = 0

# Define a function called morsecode.
def morsecode ():
    #Dot Dot Dot.
    GPIO.output(22,GPIO.HIGH)
    sleep(.1)
    GPIO.output(22,GPIO.LOW)
    sleep(.1)
    GPIO.output(22,GPIO.HIGH)
    sleep(.1)
    GPIO.output(22,GPIO.LOW)
    sleep(.1)
    GPIO.output(22,GPIO.HIGH)
    sleep(.1)

    #Dash Dash Dash.
    GPIO.output(22,GPIO.LOW)
    sleep(.2)
    GPIO.output(22,GPIO.HIGH)
    sleep(.2)
    GPIO.output(22,GPIO.LOW)
    sleep(.2)
    GPIO.output(22,GPIO.HIGH)
    sleep(.2)
    GPIO.output(22,GPIO.LOW)
    sleep(.2)
    GPIO.output(22,GPIO.HIGH)
    sleep(.2)
    GPIO.output(22,GPIO.LOW)
    sleep(.2)

    #Dot Dot Dot.
    GPIO.output(22,GPIO.HIGH)
    sleep(.1)
    GPIO.output(22,GPIO.LOW)
    sleep(.1)
    GPIO.output(22,GPIO.HIGH)
    sleep(.1)
    GPIO.output(22,GPIO.LOW)
    sleep(.1)
    GPIO.output(22,GPIO.HIGH)
    sleep(.1)
    GPIO.output(22,GPIO.LOW)
    sleep(.7)

os.system('clear')
print "Morse Code"

loop_count = input("How many times would you like SOS to loop?: ")

while loop_count > 0:
    loop_count = loop_count - 1
    morsecode ()

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:31 pm

4_button_2.py

Code: Select all

#!/usr/bin/python

import os
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

# Setup our input pin.
# We use an internal pull up resistor to hold the pin at 3v3, otherwise the inputs value could chatter between high and low.
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_UP)

print ("Waiting for you to press the button")
while True:
    if ( GPIO.input(10) == False ):
        print("Button pressed")
        os.system('date') # Print the system date and time.
        print GPIO.input(10)
        sleep(5)
        os.system('clear') # Clear the screen text.
        print ("Waiting for you to press the button")
    else:
        sleep(0.1)

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:32 pm

6_morsecode_blink.py

Code: Select all

#!/usr/bin/python

import os
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(22,GPIO.OUT) # Buzzer
GPIO.setup(17,GPIO.OUT) # Blue
GPIO.setup(27,GPIO.OUT) # Red

# We use an internal pull up resistor to hold the pin at 3v3, otherwise the inputs value could chatter between high and low.
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_UP)

loop_count = 120 # One minute of waiting time.

# Define a function called morsecode.
def morsecode ():
    #Dot Dot Dot.
    GPIO.output(22,GPIO.HIGH)
    GPIO.output(17,GPIO.HIGH)
    sleep(.1)
    GPIO.output(22,GPIO.LOW)
    GPIO.output(17,GPIO.LOW)
    sleep(.1)
    GPIO.output(22,GPIO.HIGH)
    GPIO.output(17,GPIO.HIGH)
    sleep(.1)
    GPIO.output(22,GPIO.LOW)
    GPIO.output(17,GPIO.LOW)
    sleep(.1)
    GPIO.output(22,GPIO.HIGH)
    GPIO.output(17,GPIO.HIGH)
    sleep(.1)
    GPIO.output(17,GPIO.LOW)

    #Dash Dash Dash.
    GPIO.output(22,GPIO.LOW)
    sleep(.2)
    GPIO.output(22,GPIO.HIGH)
    GPIO.output(27,GPIO.HIGH)
    sleep(.2)
    GPIO.output(22,GPIO.LOW)
    GPIO.output(27,GPIO.LOW)
    sleep(.2)
    GPIO.output(22,GPIO.HIGH)
    GPIO.output(27,GPIO.HIGH)
    sleep(.2)
    GPIO.output(22,GPIO.LOW)
    GPIO.output(27,GPIO.LOW)
    sleep(.2)
    GPIO.output(22,GPIO.HIGH)
    GPIO.output(27,GPIO.HIGH)
    sleep(.2)
    GPIO.output(22,GPIO.LOW)
    GPIO.output(27,GPIO.LOW)
    sleep(.2)

    #Dot Dot Dot.
    GPIO.output(22,GPIO.HIGH)
    GPIO.output(17,GPIO.HIGH)
    sleep(.1)
    GPIO.output(22,GPIO.LOW)
    GPIO.output(17,GPIO.LOW)
    sleep(.1)
    GPIO.output(22,GPIO.HIGH)
    GPIO.output(17,GPIO.HIGH)
    sleep(.1)
    GPIO.output(22,GPIO.LOW)
    GPIO.output(17,GPIO.LOW)
    sleep(.1)
    GPIO.output(22,GPIO.HIGH)
    GPIO.output(17,GPIO.HIGH)
    sleep(.1)
    GPIO.output(22,GPIO.LOW)
    GPIO.output(17,GPIO.LOW)
    sleep(.7)

os.system('clear')
print "Morse Code"
print "Press the button!"

while loop_count > 0:
    if ( GPIO.input(10) == False ):
        print("Button pressed")
        os.system('date') # Print the system date and time.
        morsecode()
        sleep(1)
        os.system('clear') # Clear the screen text.
        print ("Waiting for you to press the button")
    else:
        loop_count = loop_count - 1
        if (loop_count % 2 == 0):
            os.system('clear')
            print ("Waiting for you to press the button")
            print(loop_count / 2)
        sleep(0.5)

GPIO.cleanup() # The clean-up function will reset all the configurations made in this script. This will stop the warnings we got from the tutorial 2.

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:32 pm

8_ldr.py

Code: Select all

#!/usr/bin/python

import os
import datetime
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

def RCtime (RCpin):
    reading = 0
    GPIO.setup(RCpin, GPIO.OUT)
    GPIO.output(RCpin, GPIO.LOW)
    sleep(.1)

    GPIO.setup(RCpin, GPIO.IN)
    # This takes about 1 millisecond per loop cycle.
    while (GPIO.input(RCpin) == GPIO.LOW):
        reading += 1
    return reading

while True: 
    GetDateTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    LDRReading = RCtime(3)
    print RCtime(3)

    # Open a file.
    fo = open("/home/pi/gpio/8_light.txt", "wb")
    fo.write (GetDateTime)
    LDRReading = str(LDRReading)
    fo.write ("\n")
    fo.write (LDRReading)

    # Close opened file.
    fo.close()
    sleep(1)

Post Reply