Question

Code the following example and screenshot the text output.

import RPi GPIO as GPIO #sets up library to access the GPIO pins import time #imports the time library GPIO.setmode (GPIO.BCM

After you have the test code working, modify it so that it has the following features: ○ The ‘while’ loop is inside a try/except error check. This allows a <CTRL>C to break out of the loop ○ Keep track of the number of button presses with an accumulator variable ○ After the loop is ended with an exception, create an attractive output which tells the user that the program has ended as well as the number of times the button was pressed ○ Also, include the GPIO.cleanup() code to nicely disconnect from the GPIO ● Run the code and take a screenshot after you have broken out of the loop ● Here’s an example output:

pi@mkwiatPI:~/pythonProjects $ python3 Button pressed!! <Ctrl>C to exit, Button pressed!! <Ctrl>C to exit Button pressed!! <C

this is python programming.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

count = 0
buttonPin = 12

GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
while True:
   inputState = GPIO.input(buttonPin)
   if inputState == False:
       count = count + 1
       print (count, "presses so far")
       time.sleep(.2)
except:
pass

finally:
GPIO.cleanup()


or


from gpiozero import Button
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
from time import sleep
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
count = 0
button = Button(12, pull_up=False)

try:
while True:
button.wait_for_press()
print("The button was pressed!")
count = count + 1
print (count, "presses so far")
  
except:
pass

finally:
GPIO.cleanup()
  

if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and we can edit and change the answers if you argue, thanks :)

Add a comment
Know the answer?
Add Answer to:
Code the following example and screenshot the text output. After you have the test code working,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • can you please solve in c and clear 1 - General purpose eneral purpose 1/0 [30...

    can you please solve in c and clear 1 - General purpose eneral purpose 1/0 [30 pts re going to use the DDR, PORT, and PIN registers from your AVR device ions are the same as your AVR device). Assume that your AVR has. LED2) connected to pins 0, 1, 2 of Port C, respectively. In this question, you are going (the register definitions are the san • I pushbutton connected to pin 3 and Port C • 3 LEDs...

  • Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...

    Run the code in Linux and provide the screenshot of the output and input #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> static void cleanup(); static void docleanup(int signum); static const char *SERVER_ADDR = "127.0.0.1"; static const int SERVER_PORT = 61234; static int cfd = -1; int main(int argc, char *argv[]) { struct sockaddr_in saddr; char buf[128]; int bufsize = 128, bytesread; struct sigaction sigact; printf("client starts running ...\n"); atexit(cleanup); sigact.sa_handler =...

  • I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED str...

    I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED strips and 3 - ultrasonics. I have them working independently but I need to combine the code so they work together. Here is what I have: Memory Game #define PLAYER_WAIT_TIME 2000 // The time allowed between button presses - 2s byte sequence[100]; // Storage for the light sequence byte curLen = 0; // Current length of the sequence byte inputCount =...

  • I need to modify below code to have output like: Example: RBGY [ You have 1...

    I need to modify below code to have output like: Example: RBGY [ You have 1 white flag and you have 1 red flag] Example: CCGG [ You have 0 white flag and you have 1 red flag] ================================== #include <stdio.h> #include <string.h> #include <time.h> #include <stdlib.h> #define SIZE_STRING 4 #define SIZE_STRING_LONG 15 int main(int argc, char *argv[]){ if(argc != 2) { printf("Usage: ./main <scComputer>"); exit(0); } char szUser[SIZE_STRING_LONG]; char *szComputer = argv[1]; int counter=0; int colour=0; int position=0; char...

  • Example Output: STARTING CODE: Assignment5RecursionConsoleDriver.java: import java.io.*; public class Assignment5RecursionConsoleDriver {    public static void main(String[]...

    Example Output: STARTING CODE: Assignment5RecursionConsoleDriver.java: import java.io.*; public class Assignment5RecursionConsoleDriver {    public static void main(String[] args) throws IOException    {        System.out.println("puzzleFormula(1) returns: " + Assignment5Recursion.puzzleFormula(1));        System.out.println("puzzleLoop(1) returns: " + Assignment5Recursion.puzzleLoop(1));        System.out.println("puzzleRecurse(1) returns: " + Assignment5Recursion.puzzleRecurse(1));        System.out.println("\npuzzleFormula(2) returns: " + Assignment5Recursion.puzzleFormula(2));        System.out.println("puzzleLoop(2) returns: " + Assignment5Recursion.puzzleLoop(2));        System.out.println("puzzleRecurse(2) returns: " + Assignment5Recursion.puzzleRecurse(2));        System.out.println("\npuzzleFormula(3) returns: " + Assignment5Recursion.puzzleFormula(3));        System.out.println("puzzleLoop(3) returns: " + Assignment5Recursion.puzzleLoop(3));        System.out.println("puzzleRecurse(3)...

  • ARM assembly using breadboard Ladder Game - This game involves a setup of LEDs in a...

    ARM assembly using breadboard Ladder Game - This game involves a setup of LEDs in a row and a button. The goal is to get from the bottom led all the way to the top without them resetting. The LEDs will flash and you can only move up one led at a time, when the led is lit up, or else you get reset to the bottom. code in C please convert to ARM assembly #include <stdio.h> #include <stdlib.h> #include...

  • Here is the code I made, but the test case is not working, it goes wrong...

    Here is the code I made, but the test case is not working, it goes wrong when the binary string convert to decimal. please help. #include "stdafx.h" #include <iostream> #include <string> #include <math.h> #include <locale> using namespace std; // function for option 1 void decToBin(int number) {        int array[16];        int i = 0;        for (int counter = 0; counter < 16; counter++)        {               array[counter] = 0;        }        while (number > 0)        {...

  • Complete a program In C#: some code is included, you edit the areas that have not...

    Complete a program In C#: some code is included, you edit the areas that have not been filled. For your C# program you will complete code that plays the War card game. In this game, the deck of cards is evenly divided among two players. The players are not allowed to look at the cards in their hand. On each round of the game, both players lay down the top card from their hand. The player with the higher value...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • Have you ever wanted to predict the future? Well the Magic Eight Ball does just that....

    Have you ever wanted to predict the future? Well the Magic Eight Ball does just that. The original game was a softball sized “8-ball”. You would ask a question, shake it up and look at the result. There are 20 responses…10 positive, 5 negative, and 5 are vague. For this project, we want to recreate this, but give the ability to read in a set of responses, and add additional responses, and print out all of the responses in alphabetical...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT