I installed the library Clickbutton.h (the demonstration program works correct).
Whithin a function "Mastercontroller()" I request the behavior of a button (keyLeft) only one time per second.
The return value "keyLeft.depressed" works fine.
"keyLeft.clicks" can deliver negative values (long click), but it does not return a POSITIVE number (i.e. a short click).
Why are my short pulses not detected???
I want to extend the program to four buttons at a scanrate of 6x/sec.
/*
Programname: Sunlogger)019.ino
Compiler: Arduino 1.6.1
The hardware circuit:
- Arduino MEGA 2560
- External LED's
- LCD 220*4 via I2C Address 0x27
- 1 keys to digital input 33
- 2 LEDs to digital outputs 23 and 24
Version: 01
Author: C.W.A. Baltus
Created: 20150605
Modified: 20150605
Blockdiagram: Ontwerp_Sunlogger.xlsm / Total operation
External reference: https://code.google.com/p/clickbutton/
*/
//=======================================================
//libraries
#include <Wire.h> //Enables communication to serial port
#include "ClickButton.h"
// the LED
#define errorLED 24 //H3, error LED
#define kWhpulsLED 23 //H2 kWh pulse
int ledState = 0;
// Initiate ClickButton objects in an array
ClickButton keyLeft(33, LOW, CLICKBTN_PULLUP);
//int kliks =0;
byte cnt1 = 1, cnt1_previous = 0; //Loopcounter for each 62.5 ms (0..16)
#define off 1
#define on 0
//=====================================================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // open the serial port at 9600 bps:
//Preparing outputs for LED's
pinMode(kWhpulsLED, OUTPUT); //kWh-LED
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
keyLeft.debounceTime = 20; // Debounce timer in ms
keyLeft.multiclickTime = 230; // Time limit for multi clicks
keyLeft.longClickTime = 900; // time until "held-down clicks" register
//keyLeft.depressed = false
//Initialize Timer1, preset to 16 Hz timetick
cli(); // disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
OCR1A = 15624; // set compare match register to desired timer count:
TCCR1B |= (1 << WGM12); // turn on CTC mode:
TCCR1B |= (1 << CS10); // Set CS10 and CS11 bits for 64 prescaler:
TCCR1B |= (1 << CS11);
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt:
interrupts(); // enable all interrupts
} //End setup
//=====================================================
void loop()
{
if (cnt1!=cnt1_previous){ //updates 16 times per second the mastercontroller
cnt1_previous = cnt1;
Mastercontroller(); //Calls the mastercontroller 16 times per second
} //End if
} //End loop
//=====================================================
ISR(TIMER1_COMPA_vect)
{
cnt1++; //Increment 62.5 ms counter
if (cnt1 == 17) {cnt1=1;} //Loopcounter 1..16
} //End ISR
//=====================================================
void Mastercontroller() {
//cnt1 is a loop counter 1..16 (changes each 62.5 ms); at each count, a task can be handled
Serial.print("Counter = "); Serial.println(cnt1);
if (cnt1==2){ //Scans each 2nd part of 1/16 second the buttons
//if (keyLeft.depressed != true) {keyLeft.Update();}
keyLeft.Update();
Serial.print(keyLeft.clicks); Serial.print("\t"); Serial.println(keyLeft.depressed);
if (keyLeft.clicks>=1){Serial.println( " One or more clicks per a second");}
} //End cnt1==2
if (cnt1==9) {
digitalWrite(kWhpulsLED,on); //Turn kWhpulsLED on
} //End cntl==9
if (cnt1==16) {
digitalWrite(kWhpulsLED,off); //turn kWhpulsLED off
} //End cntl==16
} //End mastercontrol function
Debounce_04.ino (3.66 KB)