Like Tree1Likes
  • 1 Post By anduril462

One Button program for different uses.

This is a discussion on One Button program for different uses. within the C Programming forums, part of the General Programming Boards category; Hi friends, Being a newbie means I have to be humble and ask for help from time to time. I'm ...

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Toronto
    Posts
    20

    One Button program for different uses.

    Hi friends,

    Being a newbie means I have to be humble and ask for help from time to time.
    I'm trying to write code for a two button set up, high/low, which are used to change their values in memory. I press either button 3 seconds to enter setup. Once I'm in set up, the same two buttons are inputs to change their respective values in memory. I press the high button 3 seconds enter setup, then the high button increments the H value, and the low button decrements the H value. Likewise, when I press the low button 3 seconds, I enter low value setup, high button increments the low value while low button decrements the low value. If I hold a button down, I want the value to increase/decrease until I reach the desired value.

    (forgive me for not following protocol with brackets, I'm still not too good at it)

    Code:
    if (button4 == HIGH)
    {    
     tick1 = tick1++; //wait 3 seconds then enter setup. 
       if(tick1 = 65000){  
          delay(300);
          tick1 == 0; //reset timer 
          if(button4 == HIGH){ 
           lset == lset--; //decrement low value, write it in memory 
           EEPROM.write(11, lset);
          //here I want to loop back to button4 to decrement again as needed.    
    
        }
        else if(button3 == HIGH){
            lset == lset++; //increment low value and write it down
            delay(300);
            EEPROM.write(11, lset);
           //here I want to loop back to button3 to increment again as needed. 
        }
     }
    }
    Last edited by RobertD; 05-23-2011 at 11:34 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    I didn't see a question in there anywhere. Perhaps you want to explain exactly what it is you're having trouble with. Perhaps explaining your magic numbers (65000, 300, 11) would help, particularly, why you chose them and how you know they're correct. The following line of code is wrong:
    Code:
    lset == lset--;
    One = is for assignment, two == is for comparison. You also shouldn't assign the result of a -- operator to the same variable. Change those lines to lset--. That subtracts one and stores the value back in lset, like lset = lset - 1.

    Also, as for the "bracketing", I assume you mean indentation. Here's a few things to read:
    SourceForge.net: Indentation - cpwiki
    Indent style - Wikipedia, the free encyclopedia

    I recommend one of the top 3 styles from the Wikipedia article, and I find a 4-space indentation a good compromise between being clearly visible and not taking up too much space on the screen. 2 is the absolute smallest you should use, and 8 is the absolute max if you want something wider.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    You've got everything turned around in the first few lines.
    Code:
     tick1 = tick1++; //wait 3 seconds then enter setup. 
       if(tick1 = 65000){  
          delay(300);
          tick1 == 0; //reset timer
    Should be
    Code:
       tick1++; //wait 3 seconds then enter setup. (note: no reassignment here)
       if(tick1 == 65000){   // == for comparison
          delay(300);
          tick1 = 0; //reset timer,  = for assignment
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Toronto
    Posts
    20
    Thanks friends. I will correct the code. No ==, lset = lset - 1, and turn around the beginning lines. the 65000 ticks represents millis, so that's about 6.5 secs, too long I admit, I'll change that, the 300 millis is a wait between increments, so that the value doesn't jump too fast. 11 is eeprom location.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by RobertD View Post
    Thanks friends. I will correct the code. No ==, lset = lset - 1,
    Or you can simply do lset-- or lset++ it will decrement and increment in place.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Toronto
    Posts
    20
    Correcting the indentation.

    Code:
    if (button4 == HIGH)
          {    
          tick1++; //wait 3 seconds then enter setup. 
                if(tick1 == 35000)
                     {  
                     delay(300);
                     tick1 = 0; //reset timer 
    
      if(button4 == HIGH)
           { 
           lset == lset--; //decrement low value, write it in memory 
           EEPROM.write(11, lset);
           delay(300);
           //here I want to loop back to button4 to decrement again as needed.    
           }
    
      else if(button3 == HIGH)
           {
            lset = lset + 1; //increment low value and write it down
            EEPROM.write(11, lset);
            delay(300);
            //here I want to loop back to button3 to increment again as needed. 
           }
       }
    }
    Last edited by RobertD; 05-23-2011 at 12:46 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    That's not very corrected indentation. Try something like this (notice how subordinate lines of code are more indented than their superiors):
    Code:
        if (button4 == HIGH)
        {
            tick1++; //wait 3 seconds then enter setup.
            if(tick1 == 35000)
            {
                delay(300);
                tick1 = 0; //reset timer
    
                if(button4 == HIGH)
                {
                    lset--; //decrement low value, write it in memory
                    EEPROM.write(11, lset);
                    delay(300);
                    //here I want to loop back to button4 to decrement again as needed.
                }
    
                else if(button3 == HIGH)
                {
                    lset++; //increment low value and write it down
                    EEPROM.write(11, lset);
                    delay(300);
                    //here I want to loop back to button3 to increment again as needed.
                }
            }
        }
    Also, make note of the changes I highlighed in magenta. Your decrement was still incorrect, and these forms are more succinct and just as readable as the longer version. And since you made a few comments about needing to loop back, I suggest you read the tutorials we have on loops, and Google for some more. Read any textbooks and class notes you have, and work some examples.
    Salem likes this.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Code:
        if (button4 == HIGH)
        {
            tick1++; //wait 3 seconds then enter setup.
            if(tick1 == 35000)
            {
                delay(300);
                tick1 = 0; //reset timer
    
                if(button4 == HIGH)
    Now that the indentation is clear, what does this mean?
    The second if seems to be trivially true, if the first one happened.

    Are your buttonx variables directly linked to some external hardware?
    Are they declared as being 'volatile'?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Toronto
    Posts
    20
    I have two buttons on hardware, 3 and 4, and I use them both to enter setup and to enter the high and low value in memory, so if I press button 3 for three seconds, I enter setup, then I press 3 again, and this time it increments the high value in memory, while button 4 decrements the value stored in the high memory. Conversely, if I press 4 for three seconds, I set up the low memory with both buttons. That's why I have button4 HIGH again, this time, in setup situation, where both buttons serve to raise/lower the value in high memory. It's a dual use of the same button, first to enter setup, second to increment the value.

    And if I let it sit for 5 seconds without changing values, it exits setup.

    PS Will read tutorials.
    Last edited by RobertD; 05-23-2011 at 01:36 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So the question is still: do the button3/4 values change automatically, or do you have to do a read? If the former, make sure you have things marked as volatile so that the compiler won't just optimize all these checks away.

    In either case, I don't really see you checking for "holding the button down" -- right now, if the button is pressed, you wait three seconds, and if the button is down then, you go into set mode. Perhaps you want something like "while (button4 == HIGH)" to do the tick counting, and then at the end if the tick count is high enough, go into set mode. (You've also got another design choice to make: what if I hold the button down for five seconds without letting go? Have I changed the value?)

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Toronto
    Posts
    20
    Indeed a "while button down" count 5 seconds, then enter setup, sounds like the right approach.
    Then, when either button is held down, I want a tick up/down every 300ms, or at every press of the button, so I can reach the target by holding the button till near, then ticking one tick at the time, forward or back if I overshoot.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Toronto
    Posts
    20
    Thanks for your help, I will work on this tomorrow. I have to finish this soon, I have customers waiting.


    Code:
     if (button3 == HIGH)
          {    
          tick1++; //wait 3 seconds then enter setup. 
          if(tick1 == 35000)
                {  
                digitalWrite(backLightPin, LOW); //flash screen two times.
                delay(300);
                digitalWrite(backLightPin, HIGH); 
                delay(300);
                digitalWrite(backLightPin, LOW);  
                delay(300);
                digitalWrite(backLightPin, HIGH); 
                delay(300);
                tick1 = 0;                           //reset timer 
                hset = EEPROM.read(10);      //read and display last setup value
                lcd.print(hset, DEC);  
                     while(button4 == HIGH)
                        { 
                        delay(300);  
                        hset--;                        //decrement high value, write. 
                        EEPROM.write(11, hset);
                        lcd.print(hset, DEC);  
                        }
                         
                      while(button3 == HIGH)
                        {
                        delay(300);  
                        hset++;                         //increment high value, write.
                        EEPROM.write(11, hset);
                        lcd.print(hset, DEC);  
                        }
                            //exit setup
                        }
                 }
          }
    Last edited by RobertD; 05-23-2011 at 03:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program keeps shutting off when I press a button
    By Megamanenm in forum Game Programming
    Replies: 2
    Last Post: 01-11-2009, 02:21 AM
  2. Replies: 2
    Last Post: 06-28-2008, 08:30 PM
  3. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  4. Middle button mouse capture in another program
    By Gayak in forum Windows Programming
    Replies: 5
    Last Post: 06-13-2002, 05:00 PM
  5. c++ program with a button menu
    By quakegod in forum C++ Programming
    Replies: 0
    Last Post: 01-07-2002, 12:09 AM

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