0

Update:

I changed the code to this, but it does not work either.

`if ((millis() - lastDebounceTime) > debounceDelay) {

   if (button) {
 //DEBOUNCING

     if (!button_prev && button) {

      int e = input[j];`

I am trying to debounce a button and integrated this code http://arduino.cc/en/Tutorial/Debounce into my sketch. The result is, that there is no reaction to the button at all. I can't figure out the mistake, but maybe someone else sees what I overlook.

Circuit enter image description here

Parts marked with //DEBOUNCING are only for the purpose of it, parts marked //DEBOUNCE relevant are necessary for the code to run as well when there is no debouncing.

void setup() {

  Serial.begin(9600);

  //setting button pin as input and LOW

  pinMode(8, INPUT);
  digitalWrite(8, LOW); //DEBOUNCE relevant


  //setting input pins as output

  int act_pin[6] = {A0, A1, A2, A3, A4};

  int k = 0;

  for (k = 0; k < 5; k++) {

      pinMode(act_pin[k], OUTPUT);

    }


  //setting state pins as output

  int z_pin[5] = {2, 3, 4, 5};

  int a = 0;   

  for (a = 0; a < 4; a++) {

      pinMode(z_pin[a], OUTPUT);

   }


  //setting output pins as output

  int out_pin[6] = {9, 10, 11};

  int b = 0;

  for (b = 0; b < 3; b++) {

      pinMode(out_pin[b], OUTPUT);

   }


  //setting input  

  for(k = 0; k < 5; k++) {

      randomSeed(analogRead(5));

      int i = random(2);


      if(i == 1){

         digitalWrite(act_pin[k], HIGH);

         input[k] = 1;

         Serial.print(input[k]);

      }

       else{

         digitalWrite(act_pin[k], LOW);

         input[k] = 0;

         Serial.print(input[k]);


      }

    } 

   //setting initial situation

   digitalWrite(2, LOW);
   digitalWrite(3, HIGH);
   digitalWrite(4, LOW);
   digitalWrite(5, LOW);


   }

//DEBOUNCING

long lastDebounceTime = 0;  // the last time the button pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
int buttonState;

//DEBOUNCING


boolean button_prev = 0;

boolean button = 0;

int j = 0; //value shows current lamp being processed

//setting initial state

char z[4] = "z_0"; 

void loop() {

    button_prev = button;

    button = digitalRead(8); //DEBOUNCE relevant

    // DEBOUNCING
    if (button != button_prev) {

    lastDebounceTime = millis();

    }

    if ((millis() - lastDebounceTime) > debounceDelay) {

       if (button != button_prev) {
     //DEBOUNCING

         if (!button_prev && button) {

          int e = input[j];

           //provides a nice output
            Serial.println(" ");
            Serial.print("Current lamp being processed:");
            Serial.println(j);
            Serial.println(" ");
            Serial.print("Current input:");
            Serial.println(e);


             delta(e, z);

            Serial.print("State outside function:");
            Serial.println(z);

            lamda(e, z);

             j = (j+1) % 5;

      }


    } 

   }
}     
1

It's difficult to tell without seeing your circuit, but it looks like there could be a mix up between pin 12 and pin 8. Can see that you initialize the pinMode for pin 12 in setup, but it isn't used in the loop. You didn't initialize pin 8 (not completely necessary) in setup, but it is used in the loop. If you don't feel this is relevant, please provide some more detail.

  • That was indeed a mistake, thank you. It is not the cause of the error though. I fixed it and included the circuit. – Merlin Nov 1 '14 at 11:03
0

If the button has stopped bouncing then (button_prev == button) and the inner if (button != button_prev) will fail. Basically you're trying to ensure the button is steady but then you're checking that it changed.

Try this:

    if ((millis() - lastDebounceTime) > debounceDelay) {

       if (button) {     // Check the button is still pressed
       ... code...
}
  • I tried that, so it looks like in the edit, but it doesn't work, I'll try your suggestion from the other question(a rhyme). – Merlin Nov 1 '14 at 12:04
  • @Merlin I mean that you should remove 'if (!button_prev && button)' – akellyirl Nov 1 '14 at 12:15
  • Don't I need the if (!button_prev && button) to determine if the button has changed its state? At least that's the solution for checking if the state changed which you procided in your answer from the other question. And I actually like your debounce solution from there way more when my initial approach. – Merlin Nov 1 '14 at 12:20
0

You've probably fixed this already but pin 8 is always grounded, regardless of the button state. Pin 8 should be connected between the button and the pull-down resistor.

  • It took me a bit, but now I get what you mean. I had it right all the time in my actual circuit, only the picture is wrong. But thank you a lot, I didn't mind that it is not indifferent where the pull down resistor sits as I made the picture. – Merlin Nov 2 '14 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.