diff --git a/README.md b/README.md index 02b03aff1d6709a0b3611d2e6d2c7ba24f20cb02..9283da2e93f2fc35ff1aeea50163234b905639f3 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,7 @@ -# Digital Design Worksheet 1 +# DD Worksheet 1 +By <b>Cosmo Elford</b> 201018021 -### Authors -Cosmo Elford - -## Task 1, 2 +# Task 1, 2 (Getting started) Getting the LED to turn on with a battery and 220 Ohm resistor. In our week 3 lab session I discovered I didn't have the battery so I used the Raspberry Pi as a power source instead. Code: @@ -16,11 +14,14 @@ GPIO.setwarnings(False) GPIO.setup(18,GPIO.OUT) GPIO.output(18,GPIO.HIGH) ``` + <img src="Task1.JPG" width="30%"> ### I then upgraded the breadboard using the same principles I learnt in the previous task. -## Task 3 +# Task 3 (3 LEDs) + +<img src="Task2.JPG" width="30%"> Code: ``` @@ -54,13 +55,103 @@ while True: time.sleep(0.1) ``` -<img src="Task2.JPG" width="30%"> - -### Short video: +Short video: [](http://www.youtube.com/watch?v=7xLNLzCIo9c) -# Task 4 -## Using a button +# Task 4 (Using buttons) + +Adding the first button: + +<img src="Task4.JPG" width="30%"> + +Making sure that the button press is registering: + +<img src="Screenshot 06.10.23.png" width="30%"> + +I then added a second button: + +<img src="Task4_1.JPG" width="30%"> + +I then readded the 3 LEDs, recreating Task 3. To make sure they all worked I tested them with various GPIO pins + +<img src="Task4_2.JPG" width="30%"> + +### After I had tested everything I wrote the seqeuncing code using the two buttons and the 3 LEDs + +Short video: + +[](http://www.youtube.com/watch?v=K9MPf20hbis) + +Final code: +``` +import RPi.GPIO as GPIO +import time + +GPIO.setmode(GPIO.BCM) +GPIO.setwarnings(False) + +# LEDs LEFT-TO-RIGHT order +led_pins = [26, 25, 24] + +# Button 1, 2 +button_1_pin = 22 +button_2_pin = 15 + +GPIO.setup(button_1_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) +GPIO.setup(button_2_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) + +# LEDs +for led_pin in led_pins: + GPIO.setup(led_pin, GPIO.OUT) + +# Initialize variables +sequence_running = True +sequence_direction = 1 # 1 for forward, -1 for reverse +current_led = 0 + +def update_led_state(): + for i in range(len(led_pins)): + GPIO.output(led_pins[i], i == current_led) + +while True: + # Button 1 + if (GPIO.input(button_1_pin) == False): + time.sleep(0.25) + sequence_running = not sequence_running + + if (sequence_running): + print("Start") + else: + print("Stop") + + # Button 2 + if (GPIO.input(button_2_pin) == False): + print("Change sequence") + time.sleep(0.25) + sequence_direction *= -1 + sequence_running = True + + if sequence_running: + # Update the LED state + update_led_state() + + # Wait for 0.5 seconds + time.sleep(0.5) + + # Move to the next LED + current_led += sequence_direction + + # Wrap around if needed + if current_led < 0: + current_led = len(led_pins) - 1 + elif current_led >= len(led_pins): + current_led = 0 +``` + +Console logs: + +<img src="Screenshot 06.10.23_2.png" width="30%"> -<img src="Screenshot 06.10.23.png" width="30%"> \ No newline at end of file +# Task 5 +... \ No newline at end of file diff --git a/Screenshot 06.10.23_2.png b/Screenshot 06.10.23_2.png new file mode 100644 index 0000000000000000000000000000000000000000..b92ead8aef6fedefe96d80dddcb98743fcd2206e Binary files /dev/null and b/Screenshot 06.10.23_2.png differ diff --git a/Task4.JPG b/Task4.JPG new file mode 100644 index 0000000000000000000000000000000000000000..feec6431701a46fd116feb555ca2491e955d52e9 Binary files /dev/null and b/Task4.JPG differ diff --git a/Task4_1.JPG b/Task4_1.JPG new file mode 100644 index 0000000000000000000000000000000000000000..a438a36a13265168438b3e39a6e3b6d15856941a Binary files /dev/null and b/Task4_1.JPG differ diff --git a/Task4_2.JPG b/Task4_2.JPG new file mode 100644 index 0000000000000000000000000000000000000000..cc0a407283d5bf9a51619c6596d5428621dc4286 Binary files /dev/null and b/Task4_2.JPG differ diff --git a/Task5.py b/Task5.py new file mode 100644 index 0000000000000000000000000000000000000000..746346c90f813473260dc33694bb023b181d1442 --- /dev/null +++ b/Task5.py @@ -0,0 +1,62 @@ +import RPi.GPIO as GPIO +import time + +GPIO.setmode(GPIO.BCM) +GPIO.setwarnings(False) + +# LEDs LEFT-TO-RIGHT order +led_pins = [26, 25, 24] + +# Button 1, 2 +button_1_pin = 22 +button_2_pin = 15 + +GPIO.setup(button_1_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) +GPIO.setup(button_2_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) + +# LEDs +for led_pin in led_pins: + GPIO.setup(led_pin, GPIO.OUT) + +# Initialize variables +sequence_running = True +sequence_direction = 1 # 1 for forward, -1 for reverse +current_led = 0 + +def update_led_state(): + for i in range(len(led_pins)): + GPIO.output(led_pins[i], i == current_led) + +while True: + # Button 1 + if (GPIO.input(button_1_pin) == False): + time.sleep(0.25) + sequence_running = not sequence_running + + if (sequence_running): + print("Start") + else: + print("Stop") + + # Button 2 + if (GPIO.input(button_2_pin) == False): + print("Change sequence") + time.sleep(0.25) + sequence_direction *= -1 + sequence_running = True + + if sequence_running: + # Update the LED state + update_led_state() + + # Wait for 0.5 seconds + time.sleep(0.5) + + # Move to the next LED + current_led += sequence_direction + + # Wrap around if needed + if current_led < 0: + current_led = len(led_pins) - 1 + elif current_led >= len(led_pins): + current_led = 0 \ No newline at end of file