Basically what I'm trying to do with this C code is to mimic a momentary push button to turn on an led on and off.
Initially the LED will be OFF.
When LED is OFF and button is pressed the LED turns ON.
When LED is ON and button is pressed the LED turns OFF.
That's all I'm trying to do, one press on, one press off.
So I have the below code, but was wondering what other approaches others would use or if there is a simpler way of doing what I'm trying to accomplish. The below code is not working 100% either, I'm missing something.
Code:#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <windows.h> int main (void) { int BTNpress = 0; //To mimic press of button, a 1 = button has been pressed. int LEDStat = 0; //LED is initialy off(0) while (TRUE) //loop to scan the button state { puts( "Enter 1 to press the button"); scanf( "%d", &BTNpress); if (BTNpress && !LEDStat) //if button is pressed and the LED is OFF then turn LED ON { printf("The button has been pressed\n"); printf("LED is ON\n"); LEDStat = 1; //Change LEDStat to 1 to inidica the LED is ON. } puts( "Enter 1 to press the button"); scanf( "%d", &BTNpress); if (BTNpress && LEDStat) //if button is pressed and the led is ON then turn LED OFF { printf("The button has been pressed\n"); printf("LED is OFF\n"); LEDStat = 0; //Change LEDStat to 0 to inidica the LED is OFF. } } system("PAUSE"); return 0; }



LinkBack URL
About LinkBacks





