ho creato anche una libreria basata sull'esempio http://arduino.cc/en/Tutorial/Debounce, in questo modo mi viene più semplice tenere pulito il codice creando un istanza della classe per pin che voglio utilizzare.
ne posto il codice che può essere utile anche a qualcun altro:
ClickButton.h
#ifndef ClickButton_H
#define ClickButton_H
#if (ARDUINO < 100)
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
#define CLICKBTN_PULLUP HIGH
class ClickButton
{
public:
ClickButton();
void setPin(uint8_t buttonPin, uint8_t ledPin);
void Update();
int buttonPin; // the number of the pushbutton pin
int ledPin; // the number of the LED pin
int ledState; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState; // 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; // the last time the output pin was toggled
long debounceDelay; // the debounce time; increase if the output flickers
};
#endif
ClickButton.cpp
#include "ClickButton.h"
ClickButton::ClickButton()
{
ledState = HIGH; // the current state of the output pin
buttonState; // the current reading from the input pin
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.
lastDebounceTime = 0; // the last time the output pin was toggled
debounceDelay = 50; // the debounce time; increase if the output flickers
buttonPin;
ledPin;
}
void ClickButton::setPin(uint8_t buttonPini, uint8_t ledPini)
{
buttonPin = buttonPini;
ledPin = ledPini;
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void ClickButton::Update()
{
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
Serial.println(reading);
// 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(ledPin, ledState);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
Come richiamare le istanze
#include "ClickButton.h"
ClickButton button1;
ClickButton button2;
void setup()
{
button1.setPin(32, 22); //primo pin è quello del pulsante (INPUT), il secondo è quello del relè (OUPUT)
button2.setPin(38, 28);
}
void loop()
{
// Update button state
button1.Update();
button2.Update();
}
Il problema che si presenta, comunque, è sempre lo stesso dell'esempio precendente:
Toccando un sensore monostabile si accende sia il relè associato che anche un'altro relè al quale non è collegato alcun sensore. non riesco a capire per quale motivo.
Qualcuno che abbia idea di quale possa essere il problema?