Debounce, Void function werkt niet

Projecten die niet passen in bovenstaande onderwerpen
Berichten: 1
Geregistreerd: 20 Nov 2017, 21:03
Woonplaats: Usa

Debounce, Void function werkt niet

Berichtdoor THawinkels » 20 Nov 2017, 21:09

Beste Leden,

Ik heb de onderstaande code geschreven met Void functions maar wanneer ik 1x de pulsfunction in de void loop zet werkt hij maar wanneer ik deze vaker er in zet werkt het niet meer.. Iemand een idee wat het probleem kan zijn?

Alvast bedankt!


Code: Alles selecteren
/*
 Debounce

 Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 a minimum delay between toggles to debounce the circuit (i.e. to ignore
 noise).

 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached from pin 2 to +5V
 * 10K resistor attached from pin 2 to ground

 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.


 created 21 November 2006
 by David A. Mellis
 modified 30 Aug 2011
 by Limor Fried
 modified 28 Dec 2012
 by Mike Walters

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Debounce
 */

// constants won't change. They're used here to
// set pin numbers:
const int button1 = 2;    // Green button
const int button2 = 3;    // Red button
const int pin1 = 9;       // relay 1
const int pin2 = 10;      // relay 1
const int pin3 = 11;      // relay 1
const int pin4 = 12;      // relay 1

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 10;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
 
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
 
  // set initial LED state
  digitalWrite(pin1, ledState);
  digitalWrite(pin2, ledState);
  digitalWrite(pin3, ledState);
  digitalWrite(pin4, ledState);
}

void loop() {
  pulsFunction(pin1,button1);
  pulsflashFunction(pin2,button1,1000);
  pulsFunction(pin3,button2);
  pulsFunction(pin4,button2);
}

void pulsFunction(int pin,int button) {
    // read the state of the switch into a local variable:
  int reading = digitalRead(button);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(pin, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

void pulsflashFunction(int pin,int button,int flashDelay) {
    // read the state of the switch into a local variable:
  int reading = digitalRead(button);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(pin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(flashDelay);              // wait for a second
  digitalWrite(pin, LOW);    // turn the LED off by making the voltage LOW
  delay(flashDelay);              // wait for a second

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}


Advertisement

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: Debounce, Void function werkt niet

Berichtdoor Koepel » 22 Nov 2017, 10:29

Hallo, zit je nog op een antwoord te wachten ?

Het voorbeeld op de website van Arduino vind ik al niet duidelijk: https://www.arduino.cc/en/Tutorial/Debounce.
Je sketch vind ik niet duidelijk.
Je vraag is voor mij niet helemaal duidelijk.
En ik begrijp niet wat de uiteindelijke bedoeling is.

Voor debouncen gebruik ik bij voorkeur de Bounce2 library. Die zit in de bibliotheekbeheerder in de Arduino IDE.

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Debounce, Void function werkt niet

Berichtdoor shooter » 22 Nov 2017, 18:12

Het heeft te maken met onder andere dat je lastdebouncetime global definieert, waardoor alle functies dezelfde waarde gebruiken, en dus gaat het mis als je 2 dingen met 1 var doet.
Goed in de gaten houden wat elke variabele doet, en de 'scoop' aanpassen (dus binnen een functie pas definieren), of telkens een andere naam geven of in een array zetten
paul deelen
shooter@home.nl

Terug naar Overige projecten

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 7 gasten