Help with simple buttonSwitch syntax

/I am a new student and have been trying various ways to get a pause and reset function working for a countdown with 7seg display. I have the pause button working but the reset is hard because I dont know how to make the counting function react to a separate pushbutton fuction. Anything would help I have been at this for so long and I can only imagine how simple the syntax is.....code below/

define pin_A 7

define pin_B 5

define pin_C 8

define pin_D 10

define pin_E 11

define pin_F 9

define pin_G 6

define red_LED 13

define grn_LED 12

define push_A 3

define push_B 4

void setup() { int pin; for(pin=5;pin<=13;++pin){ pinMode(pin, OUTPUT); digitalWrite(pin, HIGH); } pinMode(push_A, INPUT); digitalWrite(push_A, HIGH); pinMode(push_B, INPUT); digitalWrite(push_B, HIGH); Serial.begin(9600); }

void loop() { buttonSwitchCase(); for(char number = 9; number >= 0; --number) { digOneDisplay(number); } }

//this following function is the only thing I cant get taken care of please help!!!

int buttonSwitchCase()

{ char action; if (digitalRead(push_A == LOW)) //i have played with all options of setting this pushB to high with internal pullup and //still no precise action for push B, is function miswritten? action 1 always performs //but action 2 and 3 are both non responsive { action = 1; char number; } else if (digitalRead(push_B == LOW)) { action = 2; } else if (digitalRead(push_A == LOW) && (digitalRead(push_B == LOW))) { action = 3; } switch(action){ case 1: delay(1); while(!digitalRead(push_A)){}; break; case 2: //the statements here are just for testing the button but, i really need to reset my count, clearDisplay(); //but the problem is that the count is a separate function, PLEASE GUIDE ME break; case 3: //case three does not respond clearDisplay(); delay(1); while(!digitalRead(push_B)){ };

}}

void clearDisplay() { digitalWrite(pin_A, HIGH); digitalWrite(pin_B, HIGH); digitalWrite(pin_C, HIGH); digitalWrite(pin_D, HIGH); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, HIGH); digitalWrite(pin_G, HIGH); } void digOneDisplay(char number) //this is where i need to have a reset function written to but it has to { //be written into the button function above delay(1000); buttonSwitchCase(); clearDisplay(); if (number == 9) { digitalWrite(red_LED, HIGH); digitalWrite(grn_LED, LOW); digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, HIGH); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, LOW); digitalWrite(pin_G, LOW); }

if (number == 8) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, LOW); digitalWrite(pin_E, LOW); digitalWrite(pin_F, LOW); digitalWrite(pin_G, LOW); }

if (number == 7) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, HIGH); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, HIGH); digitalWrite(pin_G, HIGH); }

if (number == 6) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, HIGH); digitalWrite(pin_C, LOW); digitalWrite(pin_D, LOW); digitalWrite(pin_E, LOW); digitalWrite(pin_F, LOW); digitalWrite(pin_G, LOW); }

if (number == 5) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, HIGH); digitalWrite(pin_C, LOW); digitalWrite(pin_D, LOW); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, LOW); digitalWrite(pin_G, LOW); }

if (number == 4) { digitalWrite(pin_A, HIGH); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, HIGH); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, LOW); digitalWrite(pin_G, LOW); }

if (number == 3) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, LOW); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, HIGH); digitalWrite(pin_G, LOW); }

if (number == 2) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, HIGH); digitalWrite(pin_D, LOW); digitalWrite(pin_E, LOW); digitalWrite(pin_F, HIGH); digitalWrite(pin_G, LOW); }

if (number == 1) { digitalWrite(pin_A, HIGH); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, HIGH); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, HIGH); digitalWrite(pin_G, HIGH); }

if (number == 0) { digitalWrite(red_LED, LOW); digitalWrite(grn_LED, HIGH); digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, LOW); digitalWrite(pin_E, LOW); digitalWrite(pin_F, LOW); digitalWrite(pin_G, HIGH); delay(3000); } }

Two possibilities that I can see. One is that you are dealing with bounce on the button so it is confusing the program.

The second is one I don't understand either but I was having the same problem over the weekend. I was using an integer called mode that I was incrementing everytime I pressed the button. I know for a fact that mode was incrementing correctly but my switch case simply woudn't work. I tried every possibility and it just ignored the switch and performed every function in the cases. I finally gave up and used a if then loops instead to handle the 'modes'. This worked fine but is less elegant.

Try that and see if it functions correctly then try to figure out what is wrong with the switch case. That is what I am doing.

Luck,

Wade

Thanks, I tried to do the if then statements and ended with the same results...uggghhh

oneToughStudent:   if (digitalRead(push_A == LOW))

This is wrong. What this does is compare the value of push_A with LOW, which will evaluate to true or false depending whether your pin number happens to equal the predefined constant LOW. The true or false value is passed to digitalRead(). That function expects the argument to be a pin number. Since true is represented by the value 1 and false by 0, it will try to read pin 0 or pin 1. The return value from digitalRead() on pin 0 or pin 1 will be an integer value containing one of the predefined integer constants HIGH or LOW. It's poor practice to use a number where a boolean expression is expected, but it happens that HIGH and LOW actually coincide with the values true and false so the if condition will deal sensibly with the digital value. However, since you have read it from the wrong pin, it's unlikely to contain a useful value.

This syntax is what you need:

  if (digitalRead(push_A) == LOW)

When posting code, PLEASE wrap it in “code” tags. Press the “#” button in the post editor to see what they look like.

Sacman:
I tried every possibility and it just ignored the switch and performed every function in the cases. I finally gave up and used a if then loops instead to handle the ‘modes’. This worked fine but is less elegant.

Then you were doing something wrong. Properly implemented switch{} work fine.

@peterh, thank you for that, it is very helpful to know. i appreciate the response. cheers

I got the digRead figured out and the buttons respond to the code, but the main reason i started on here was to figure out how to get the count to reset from the function buttonSwitch, when the count runs from a separate function. does it have anything to do with a static char maybe put into the main loop. Been searching all weekend for this probably simple answer. I cannot close this sketch til I have the reset button working properly. Anything will help. Thanks

oneToughStudent: but the main reason i started on here was to figure out how to get the count to reset from the function buttonSwitch,

What count? I don't see a variable named count anywhere.

//case three does not respond

Not surprising.

Your code has a few problems:

char action;
  if (digitalRead(push_A) == LOW)                        //i have played with all options of setting this pushB to high with internal pullup and       
                                                                     //still no precise action for push B, is function miswritten?  action 1 always performs
                                                                     //but action 2 and 3 are both non responsive
   {   
    action = 1;
    char number;
   }
  else if (digitalRead(push_B) == LOW)
  { 
    action = 2;
  }
  else if (digitalRead(push_A) == LOW && (digitalRead(push_B) == LOW))
  { 
    action = 3;
  }

First, declaring "action" inside buttonSwitchCase means that it will be assigned a random value when you enter the function. If push_A and push_B are both high, you will execute the switch statement with a random value of "action" which could be 1, 2 or 3 or any other value. So, declare action to be zero:

char action = 0;

Second, your if statements will never set action to 3. If push_A is LOW then action is set to 1. If push_B is LOW then action is set to 2. BUT the test for "if push_A AND push_B are both LOW" will never be true because if push_A and push_B are both LOW then push_A is LOW and therefore you will have set action to 1. To get around this, put the test for A and B FIRST.

Third, the statement "char number;" does nothing useful. Get rid of it.

Try this:

char action = 0;
  if (digitalRead(push_A) == LOW && (digitalRead(push_B) == LOW))
  { 
    action = 3;
  }
  else if (digitalRead(push_A) == LOW)                        //i have played with all options of setting this pushB to high with internal pullup and       
                                                                     //still no precise action for push B, is function miswritten?  action 1 always performs
                                                                     //but action 2 and 3 are both non responsive
   {   
    action = 1;
   }
  else if (digitalRead(push_B) == LOW)
  { 
    action = 2;
  }
  else

how to get the count to reset

James beat me to this question - which count?

Pete

oneToughStudent:

//im such a newbie at this, any help or advice would be welcome

void loop() { buttonSwitchCase(); for(char number = 9; number >= 0; --number) { digOneDisplay(number); } }

//functions below main loop above

int buttonSwitchCase()

{ if (digitalRead(push_A) == LOW && (digitalRead(push_B) == HIGH)) { delay(1); while(!digitalRead(push_A)){}; // WORKS FOR PAUSE } else if (digitalRead(push_B) == LOW && (digitalRead(push_A) == HIGH)) { clearDisplay(); //NON RESPONSIVE } else if (digitalRead(push_A) == LOW && (digitalRead(push_B) == LOW)) { clearDisplay(); delay(1); while(!digitalRead(push_B)){}; } }

//above is the function written for the two input buttons to do either pause or reset. the pause is taken care of as push_A and works //i cant get the count written as number to reset in the main loop because i dont know how pass the value of the switch to the following //function which once in the main loop has a for statement to decrement the count, labeled as var _number

void digOneDisplay(char number) { delay(1000); buttonSwitchCase(); clearDisplay(); if (number == 9) { digitalWrite(red_LED, HIGH); digitalWrite(grn_LED, LOW); digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, HIGH); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, LOW); digitalWrite(pin_G, LOW); }

if (number == 8) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, LOW); digitalWrite(pin_E, LOW); digitalWrite(pin_F, LOW); digitalWrite(pin_G, LOW); }

if (number == 7) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, HIGH); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, HIGH); digitalWrite(pin_G, HIGH); }

if (number == 6) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, HIGH); digitalWrite(pin_C, LOW); digitalWrite(pin_D, LOW); digitalWrite(pin_E, LOW); digitalWrite(pin_F, LOW); digitalWrite(pin_G, LOW); }

if (number == 5) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, HIGH); digitalWrite(pin_C, LOW); digitalWrite(pin_D, LOW); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, LOW); digitalWrite(pin_G, LOW); }

if (number == 4) { digitalWrite(pin_A, HIGH); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, HIGH); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, LOW); digitalWrite(pin_G, LOW); }

if (number == 3) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, LOW); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, HIGH); digitalWrite(pin_G, LOW); }

if (number == 2) { digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, HIGH); digitalWrite(pin_D, LOW); digitalWrite(pin_E, LOW); digitalWrite(pin_F, HIGH); digitalWrite(pin_G, LOW); }

if (number == 1) { digitalWrite(pin_A, HIGH); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, HIGH); digitalWrite(pin_E, HIGH); digitalWrite(pin_F, HIGH); digitalWrite(pin_G, HIGH); }

if (number == 0) { digitalWrite(red_LED, LOW); digitalWrite(grn_LED, HIGH); digitalWrite(pin_A, LOW); digitalWrite(pin_B, LOW); digitalWrite(pin_C, LOW); digitalWrite(pin_D, LOW); digitalWrite(pin_E, LOW); digitalWrite(pin_F, LOW); digitalWrite(pin_G, HIGH); delay(3000); } }

sigh

that's "quote" not "code" tags.

k james i got the tags part *unsigh :) still no response on the second two cases...i have jumped from if statements back to switch

int buttonSwitchCase()

{  
  char action = 0;
    
    if (digitalRead(push_A) == LOW && digitalRead(push_B) == LOW) 
    {
      action = 3; 
    }
      else if (digitalRead(push_A) == LOW) 
      {
        action = 1; 
      }
        else if (digitalRead(push_B) == LOW) 
        {
          action = 2;
          Serial.println("DONT WORK");
        }
    switch(action)
    {
      case 1:                                           //pause works here
        delay(1);
        while(!digitalRead(push_A)){};
        break;
      
      case 2:
        clearDisplay();                               //need reset here i just have the clear display func here for testing
      
      case 3:
        delay(1);
        while(!digitalRead(push_B)){};         //i can make up anything here  but until case two is debugged not important
    }
      

}

[quote author=James C4S link=topic=88264.msg663192#msg663192 date=1327283802]

oneToughStudent: but the main reason i started on here was to figure out how to get the count to reset from the function buttonSwitch,

What count? I don't see a variable named count anywhere. [/quote]

In the function I used the var number

k james i got the tags part *unsigh still no response on the second two cases...i have jumped from if statements back to switch

It would have (and still would be) nice if you just went back and Modified the other posts to use them. It'd make navigating the thread much easier.

      case 2:
        clearDisplay();                               //need reset here i just have the clear display func here for testing
      
      case 3:
        delay(1);
        while(!digitalRead(push_B)){};         //i can make up anything here  but until case two is debugged not important
    }

You don't realize it yet, but there is a bug here. case 2 (and case 3) should both end with a "break;". Otherwise, when case 2 is finished, the switch statement will continue into case 3.

Your case 2 is probably responding, but as you've noticed because the Arduino is fast and your eyes are slow.

Look at your function:

void digOneDisplay(char number)                                           //this is where i need to have a reset function written to but it has to
{                                                                                      //be written into the button function above
   delay(1000);
   buttonSwitchCase();
   clearDisplay();
   if (number == 9)

In english, it waits 1 second, checks the buttons, clears the display and then displays a number. Then repeats.

When it repeats what happens? delay for 1 second...

Notice what happens there? If your "case 2" is executed, you clear the display twice. However, you then proceed to immediately write a new value to the display and wait 1 second. So the switch is working fine, your logic is flawed.

It might be better to set and clear a flag variable for when the display should be cleared. If the display is suppose to be cleared, don't display a number...

For my own curiosity does pressing the button read as low, or, is it high?