Raspberry Pi C Code! NOT PYTHON. C ONLY! Write a code so that when pressing the
ID: 3690549 • Letter: R
Question
Raspberry Pi C Code! NOT PYTHON. C ONLY!
Write a code so that when pressing the switch, the LED turns on and when it's on pressing the switch will turn it off.
Directions:
Switch input: connecting a push button switch to your RPi. Once you've got an LED to blink using a GPIO0 pin in output mode, the next step is to implement a push button switch as an input device. The only tricky part is that switches bounce. When the physical electrical contacts of a mechanical switch collide, they bounce -- for milliseconds, which is too fast for us slow homo sapiens to notice, but computers can respond to every bounce, which can cause problems. So we have to de-bounce mechanical switch contacts by inserting a delay when the physical contacts open and close.
.
For this part, connect one side of the push-button switch to GPIO1 on pin 12 of connector P1 on your RPi, the other side of the switch to 0v on pin 14. Then connect GPIO1 pin 12 to one end of a resistor, and the other end of the resistor to 3.3V on P1 pin 17. When the buton is pushed, it will connect GPIO1 to ground and read as logic LOW or 0, and when the button is not pressed, it will read as logic HIGH or 1.
.
Write code that will "toggle" the LED. That means that pushing the switch will turn the LED ON if it is OFF, and when it is OFF, pushing the button will turn it ON.
Explanation / Answer
#define BUTTON1_PIN 12// Button 1
#define BUTTON2_PIN 17
#define DEFAULT_LONGPRESS_LEN 25
#define DELAY 20
enum { EV_NONE=0, EV_SHORTPRESS, EV_LONGPRESS };
class ButtonHandler {
public:
ButtonHandler(int pin, int longpress_len=DEFAULT_LONGPRESS_LEN);
void init();
int handle();
protected:
boolean was_pressed;
int pressed_counter;
const int pin;
const int longpress_len;
};
ButtonHandler::ButtonHandler(int p, int lp)
: pin(p), longpress_len(lp)
{
}
void ButtonHandler::init()
{
pinMode(pin, INPUT);
digitalWrite(pin, HIGH); // pull-up
was_pressed = false;
pressed_counter = 0;
}
int ButtonHandler::handle()
{
int event;
int now_pressed = !digitalRead(pin);
if (!now_pressed && was_pressed) {
// handle release event
if (pressed_counter < longpress_len)
event = EV_SHORTPRESS;
else
event = EV_LONGPRESS;
}
else
event = EV_NONE;
// update press running duration
if (now_pressed)
++pressed_counter;
else
pressed_counter = 0;
// remember state, and we're done
was_pressed = now_pressed;
return event;
}
// Instantiate button objects
ButtonHandler button1(BUTTON1_PIN);
ButtonHandler button2(BUTTON2_PIN, DEFAULT_LONGPRESS_LEN*2);
void setup()
{
Serial.begin(9600);
// init buttons pins; I suppose it's best to do here
button1.init();
button2.init();
}
void print_event(const char* button_name, int event)
{
if (event)
Serial.print(button_name);
Serial.print(".SL"[event]);
}
void loop()
{
// handle button
int event1 = button1.handle();
int event2 = button2.handle();
// do other things
print_event("1", event1);
print_event("2", event2);
// add newline sometimes
static int counter = 0;
if ((++counter & 0x1f) == 0)
Serial.println();
delay(DELAY);
}
When the button is pressed, the resistor gets a voltage difference equal to VCC, and a current I is flowing: I = VCC / R
pull-down at our disposal, we would have connected the pin to VCC instead of GND, and read HIGH upon press, which is more logical. But since we have a pull-up only, we'll have to reverse the polarity by software, at pin sampling.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.