using one button to change states

This is a discussion on using one button to change states within the C Programming forums, part of the General Programming Boards category; Basically what I'm trying to do with this C code is to mimic a momentary push button to turn on ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    52

    using one button to change states

    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;
    }
    Last edited by drkidd22; 05-10-2011 at 12:11 PM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    The below code is not working 100% either
    Don't keep us in suspense, what's not working about it?

    You don't need the second copy of
    Code:
        puts( "Enter 1 to press the button");
        scanf( "%d", &BTNpress); 
    ...
    Just have the user input once, and test for the LED state and essentially, toggle it. In fact, I'd make a function called ToggleLED and call it each time the "button" is pressed.
    Last edited by mike65535; 05-10-2011 at 12:33 PM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Like this...
    Code:
    while (1)
     {/wait for button press
     if (ledoff)
       turn led on
       ledoff = false
     else
       turn led off
       ledoff = true
    }

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    52
    Ok, this is what I'm down to now.

    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 indicate the LED is ON.
    	 }
    
    	 else 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;
    }
    Now I want to add debounce to it.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are still overcoding....
    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( "Press Enter to press the button");
         getchar();                 <--- it's going to sit here until you push the button, it's not going anywhere
    
         if (BTNpress  && !LEDStat)  <--- bold faced text is unnecessary
          { //printf("The button has been pressed\n"); 
            printf("LED is ON\n");
            LEDStat = 1;  }
           else if (BTNpress && LEDStat)   <---- since there are only two posible states this is unnecessary		 {
              {  //printf("The button has been pressed\n"); 
               printf("LED is OFF\n");
               LEDStat = 0;  }		
     }
    
    system("PAUSE");
    return 0;
    }
    Leaving you with.....
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <windows.h>
    
    int main (void)
    {  int LEDStat = 0;  //LED is initialy off(0)
    
    while (TRUE) //loop to scan the button state
    {
         puts( "Press Enter to press the button");
         getchar(); 
    
         if (!LEDStat)  
          {  printf("LED is ON\n");
             LEDStat = 1;  }
         else 
          {  printf("LED is OFF\n");
             LEDStat = 0;  }		
     }
    
    system("PAUSE");
    return 0;
    Last edited by CommonTater; 05-10-2011 at 01:56 PM.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    52
    thanks for you explanation and making my mess cleaner.
    I have no idea where the bold stuff came from, but ok.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Like this...
    Code:
    while (1)
     {/wait for button press
     if (ledoff)
       turn led on
       ledoff = false
     else
       turn led off
       ledoff = true
    }
    You can also just use a single statement:
    Code:
    ledstate = ! ledstate;

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by drkidd22 View Post
    thanks for you explanation and making my mess cleaner.
    I have no idea where the bold stuff came from, but ok.
    I put it in there to show you what you didn't need....

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    I put it in there to show you what you didn't need....
    Bold or in color, people just can't understand simple highlighting.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Bold or in color, people just can't understand simple highlighting.
    Quzah.
    What? I thought it was perfectly obvious.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    What? I thought it was perfectly obvious.
    Don't get me started! I had a program (I still have it here some place) that would color every character on a gradient, or random colors.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. Change Power Button Action in Windows XP
    By linucksrox in forum Windows Programming
    Replies: 3
    Last Post: 04-21-2010, 09:38 PM
  3. Change button text from a thread?
    By guitarist809 in forum Windows Programming
    Replies: 9
    Last Post: 05-06-2008, 07:14 PM
  4. how to change font of button's caption using MFC
    By urvashi in forum Windows Programming
    Replies: 5
    Last Post: 04-27-2004, 07:09 AM
  5. How To Change Caption's On A Button When It Is Click ?
    By SonicWave in forum Windows Programming
    Replies: 1
    Last Post: 09-19-2001, 03:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21