Go Down

Topic: Apprentice coder (Read 6012 times) previous topic - next topic

Pedro147

#75
Dec 26, 2012, 12:24 am Last Edit: Dec 26, 2012, 12:33 am by Pedro147 Reason: 1
I have received my 7 Seg displays and wired them up to display, blank display, then zero through to nine and then back to blank display etc . I have modified  the code from my previous post where it was controlling eight LED's. It all works as I hoped for but I cannot for the life of me work out how to debounce the switch input. I have looked at all the examples of debounce codes and understand the basic principles but everything that I have tried so far is not working. As this is part of a larger project any help  (as usual) would be greatly appreciated,
Thank you Pedro.
Code: [Select]
 
 // Thanks to Grumpy Mike Arduino forum for this article - http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

 int pins [] = { 2, 3, 4, 5, 6, 7, 8, 9 };            // pin 9 allocated to DP but not used (first element of binary array in char    elevenCode)
 int digit[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  // array to allow pushbutton to advance through elevenCode array elements
 int switchPin = 12;                                  // pushbutton attached to pin 12
 int buttonState = LOW;                               // initialise buttonState as LOW
 int counter = 0;                                     // initialise counter at zero
 char elevenCode[] = { B00000000, B01111110, B00110000, B01101101, B01111001, B00110011, B01011011, B01011111, B01110000, B01111111, B01111011 };
  // Array element #  No Display      0          1          2          3          4          5          6          7          8           9
 
 void setup()
 {
   for(int i = 0; i < 8; i++)                         // set digital pins as OUTPUTS
    pinMode(pins[i], OUTPUT);
    pinMode(switchPin, INPUT);                       // sets pin 12 as pushbutton INPUT
 }
 
 void loop()
 {
   int newstate = digitalRead(switchPin);            // has the state changed from
   if (buttonState != newstate)                      // HIGH to LOW or vice versa
 
   {                                                
     buttonState = newstate;
     if (newstate == LOW)                            // If the button was pressed
       counter++;                                    // increment the counter by one
   }
 
  if(counter > 10)
     counter = 0;
 
 switch(counter)
   {
   case 0:
     displayEleven(digit[0]);             // function to display element 0 of elevenCode array
     break;
 
   case 1:
     displayEleven(digit[1]);             // function to display element 1 of elevenCode array
     break;
 
   case 2:
     displayEleven(digit[2]);             // function to display element 2 of elevenCode array
     break;
 
   case 3:
     displayEleven(digit[3]);             // function to display element 3 of elevenCode array
     break;
 
   case 4:
     displayEleven(digit[4]);             // function to display element 4 of elevenCode array
     break;
 
   case 5:
     displayEleven(digit[5]);             // function to display element 5 of elevenCode array
     break;
 
   case 6:
     displayEleven(digit[6]);             // function to display element 6 of elevenCode array
     break;
 
   case 7:
     displayEleven(digit[7]);             // function to display element 7 of elevenCode array
     break;
 
   case 8:
     displayEleven(digit[8]);             // function to display element 8 of elevenCode array
     break;
 
   case 9:
     displayEleven(digit[9]);             // function to display element 9 of elevenCode array
     break;
 
   case 10:
     displayEleven(digit[10]);             // function to display element 10 of elevenCode array
     break;
 
   }
 }
 
void displayEleven(int num)               // function to set OUTPUTS of 7_Seg from elevenCode array
 {
   int mask = 1;
   for(int i = 0; i < 8; i++)
   {
     if((mask & elevenCode[num]) == 0)
       digitalWrite(pins[i], LOW);
     else digitalWrite(pins[i], HIGH);
     mask = mask << 1;
   }
 }
 
 

http://www.pedroduino.com

PaulS

Code: [Select]
  switch(counter)
    {
    case 0:
      displayEleven(digit[0]);             // function to display element 0 of elevenCode array
      break;
 
    case 1:
      displayEleven(digit[1]);             // function to display element 1 of elevenCode array
      break;

This is rather silly, since every case uses the value of counter as the index into the digit array.

The whole switch statement could be replaced by
Code: [Select]
      displayEleven(digit[counter]);

Quote
I have looked at all the examples of debounce codes and understand the basic principles but everything that I have tried so far is not working.

The code you posted does something, but you haven't explained what it does.

You want it to do something, though what is not exactly clear.

Pedro147

Thank you for your response PaulS. Yes now that you mention it, it was rather a silly piece of code. Great, your suggestion trimmed my code back by half. What my code does is to count pushbutton presses and display the number of presses on a seven segment LED display. Well actually as it is currently written, after the code is first uploaded there are no LED's alight. The first press displays zero, then subsequent presses display the numbers one through to nine and finally back to no LED's alight. I realise that this sequence of number displays is somewhat unconventional but this is for me a learning experience and as you will appreciate I can modify it to display numbers within the arrays as required. The problem that I am having is with button bounce causing the display to jump through several numbers at once when the button is only meant to be pressed once. I am having trouble trying to incorporate some debounce code into my code. I understand the basic pricple of taking two switchpin readings with a small delay between them and if they are both high then executing the switching action, but after numerous attempts to modify the code I am at a loss as to how to do this. Thanks again for your help and I hope that I have now adequately explained what I am trying to do. Pedro.

http://www.pedroduino.com

wildbill

An easy way to debounce your input is to use a short delay - twenty milliseconds would be enough. Put it in the body of the if that executes if the state of the button has changed. Generally, using delay is frowned upon because it tends to make your program unresponsive. Short delays like this for debouncing won't be noticeable by the user though.

Pedro147

Thanks wildbill, is this the best place for the delay

Code: [Select]

void loop()
  {
    int newstate = digitalRead(switchPin);            // has the state changed from
    if (buttonState != newstate)                      // HIGH to LOW or vice versa
 
    {                                                 
      buttonState = newstate;
      if (newstate == LOW)                            // If the button was pressed
        counter++;                                    // increment the counter by one
    }
 
   if(counter > 10)
      counter = 0;
     delay(20);


   displayEleven(counter);
 
  }
 
http://www.pedroduino.com

wildbill

Ideally, you only want to debounce when a transition has occurred. I'd put it here:
Code: [Select]

      if (newstate == LOW)                            // If the button was pressed
        {
        counter++;                                    // increment the counter by one
        delay(20);
        }

Pedro147

Thank you for your help, that certainly seems to have settled it down  8)
http://www.pedroduino.com

Go Up