Handling Inputs

Hi, I am a newbie and I am attempting to achieve the following using a UNO R3: My Uno is connected as follows: 1). Wireless receiver connected to D5 - set as input with pull-up resistor. 2). Buzzer connected to D7 - set to output

When the button on the remote is pressed, the wireless receiver pulls D5 LOW for 1 second. This is set in the wireless receiver and cannot be changed.

Using the Debounce Example, this all works and when the button on the wireless remote is pressed, the state of the output D7 changes and the buzzer switches ON. On the next press of the wireless remote, the state of the output D7 changes and the buzzer switches OFF.

What I need to achieve is to have three separate functions that can be performed depending on the number of button presses received by the wireless receiver connected to D5.

For instance: 1). One button press on wireless remote: Buzzer beeps once. 2). Two button presses on wireless remote: Buzzer beeps two times. 3). Three button presses on wireless remote: Buzzer beeps three times.

My Code:

/*
 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 remotePin = 5;    // the number of the remote receiver pin
const int piezoPin = 7;      // the number of the buzzer pin

// Variables will change:
int piezoState = LOW;         // the current state of the buzzer output pin
int receiverState;             // the current reading from the remote receiver input pin
int lastReceiverState = HIGH;   // the previous reading from the remote receiver 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 = 50;    // the debounce time; increase if the output flickers

void setup() {
  Serial.begin(9600);
  pinMode(remotePin, INPUT);
  pinMode(piezoPin, OUTPUT);

  // set initial Piezo pin state
  digitalWrite(piezoPin, piezoState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(remotePin);

  // 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 remote receiver pin changed:
  if (reading != lastReceiverState) {
    // 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 != receiverState) {
      receiverState = reading;

      // only toggle the Buzzer if the new button state is LOW
      if (receiverState == LOW) {
        piezoState = !piezoState;
        Serial.println("Received from Wireless Remote");
      }
    }
  }

  // set the Buzzer:
  digitalWrite(piezoPin, piezoState);

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

If each press of the remote causes D7 to stay low for 1 second it will be very inconvenient to implement what you want. You will need to write a program that counts the button presses and waits for maybe 5 or 6 seconds before deciding that the count is complete - even if the user only intends a single button press.

…R

@Robin2 Thanks for your reply. This is used very seldom, maybe only once a day, so would not be a great inconvenience to the user. How would I count the button presses for say 5 seconds?

Something like this (not tested)

buttonPressed = digitalRead(buttonPin);
if (buttonPressed == 0) { // assumes 0 = pressed
    if (waitingForFirstPress == true) {
        firstPressMillis = millis();
        waitingForFirstPress = false;
    }
    buttonCount ++;
}
if (millis() - firstPressMillis >= 5000) { // 5 seconds - might need to be changed
    numButtonPresses = buttonCount;
    buttonCount = 0;
    waitingForFirstPress = true;
}

I have not included anything for debouncing - I think you already know how to do that.

...R