Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C code that will \"toggle\" the LED. That means that pushing the switch

ID: 654700 • Letter: W

Question

Write a C 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. 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.

This is the wiring: http://imgur.com/IX5efP1

I connected one side of the push-button switch to GPIO1 on pin 12 of connector (P1 on RPi), the other side of the switch to 0v on pin 14. I connected GPIO1 pin 12 to one end of a resistor, and the other end of the resistor to 3.3V on P1 pin 17.

This was the first code I wrote, I was told it is wrong but on the right track. Im not sure what values LED and BUTTON should have.

#include
#include
blinkled.c
const int LED = 0;
const int BUTTON = 1;

int main(void)
{
printf("Test1 ");
wiringPiSetupGpio ();
pinMode (LED, OUTPUT);
pinMode (BUTTON, INPUT);
while (1)
{
if (digitalRead(BUTTON))
{
printf("Test2 ");
digitalWrite (LED, HIGH);
}
else
{
printf("Test3 ");
digitalWrite (LED, LOW);
}
delay (500);
}
return 0;
}

Explanation / Answer

The code is right just revers the pins and port you will definately get the correct combinations.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote