So,I'm planing a project and it should include a OLED display, 1 pushbutton,and some sensors.So I'm trying to make a program that will turn on my OLED when I press button 5 times fast,and when display is working I can hold button pressed for output on a digital pin.Any tips? Ideas? Examples?
Nikola19992:
So,I'm planing a project and it should include a OLED display, 1 pushbutton,and some sensors.So I'm trying to make a program that will turn on my OLED when I press button 5 times fast,and when display is working I can hold button pressed for output on a digital pin.Any tips? Ideas? Examples?
this is a multi-press example program:
#include "MultiPress.h"
#define LEDPIN 13
#define BUTTON_A 2
#define BUTTON_B 3
void A_ButtonActions(void); // Callback function definitions are required...
void B_ButtonActions(const int value);
SimplePress pushButtonSwitches[]= {
{BUTTON_A, 100, A_ButtonActions}, // pin, interval, callback-on-pressed
//{BUTTON_A, 100, [](){digitalWrite(LEDPIN, !digitalRead(LEDPIN));}}, // same as above but done with Lambda
{BUTTON_B, 500, B_ButtonActions},
};
void setup()
{
Serial.begin(115200);
pinMode(LEDPIN, OUTPUT);
Serial.println(SimplePress::getCount());
SimplePress::beginAll();
SimplePress::setDebounceAll(50);
}
void loop()
{
SimplePress::update();
}
void A_ButtonActions(void) // simple example of registering "Pressed"
{
digitalWrite(LEDPIN, !digitalRead(LEDPIN));
}
void B_ButtonActions(const int value) // example of registering Multi-Presses
{
Serial.print(F("Button B:\t"));
switch(value)
{
case -1:
Serial.println(F("Long Press"));
break;
case 1:
Serial.println(F("One Press"));
break;
case 2:
Serial.println(F("Two Presses"));
break;
case 3:
Serial.println(F("Three Presses"));
break;
default:
Serial.println(F("Whole Lotta Presses"));
break;
}
}
MultiPress.h file:
#ifndef MULTIPRESS_H
#define MULTIPRESS_H
#ifdef PARTICLE
#include "Particle.h"
#elif ARDUINO >= 100
#include "Arduino.h"
#endif
#define MAX_BUTTON_INSTANCES 8
#define DEFAULT_DEBOUNCE_MILLISECONDS 50
class SimplePress{
public:
SimplePress(int _pin, uint32_t _pressInterval, void(*_callBackWithArg)(const int value));
SimplePress(int _pin, uint32_t _pressInterval, void(*_callBack)(void));
bool begin();
int setDebounce(uint8_t dbounce);
static void setDebounceAll(uint8_t dbounce);
int8_t pressed();
static void update(void);
static bool beginAll();
static int getCount();
private:
byte pressCount;
byte lastState;
byte pin;
uint32_t lastMillis;
uint8_t debouncePeriod;
uint16_t pressInterval;
void(*callBackWithArg)(int value);
void(*callBack)();
static uint8_t instanceCount;
static SimplePress* instances[MAX_BUTTON_INSTANCES];
};
#endif
MultiPress.cpp file:
#include "MultiPress.h"
SimplePress* SimplePress::instances[MAX_BUTTON_INSTANCES];
uint8_t SimplePress::instanceCount = 0;
SimplePress::SimplePress(int _pin, uint32_t _pressInterval, void(*_callBack)(const int value))
{
callBackWithArg = _callBack;
pin = _pin;
debouncePeriod = DEFAULT_DEBOUNCE_MILLISECONDS;
pressInterval = _pressInterval;
instances[instanceCount] = this;
instanceCount++;
}
SimplePress::SimplePress(int _pin, uint32_t _pressInterval, void(*_callBack)(void))
{
callBack = _callBack;
pin = _pin;
debouncePeriod = DEFAULT_DEBOUNCE_MILLISECONDS;
pressInterval = _pressInterval;
instances[instanceCount] = this;
instanceCount++;
}
bool SimplePress::begin()
{
pinMode(pin, INPUT_PULLUP);
lastState = HIGH;
Serial.println(pin);
return true;
}
bool SimplePress::beginAll()
{
for (size_t button = 0; button < instanceCount; button++)
{
instances[button]->begin();
}
return true;
}
void SimplePress::update(void)
{
for (size_t button = 0; button < instanceCount; button++)
{
int8_t pressed = instances[button]->pressed();
if(pressed)
{
if(instances[button]->callBackWithArg)
{
instances[button]->callBackWithArg(pressed);
}
else if(instances[button]->callBack)
{
instances[button]->callBack();
}
}
}
}
int8_t SimplePress::pressed()
{
byte nowState = digitalRead(pin);
if(nowState != lastState)
{
//Serial.println(F("CHANGED STATE"));
if(millis() - lastMillis < debouncePeriod) return 0;
if(nowState == LOW)
{
lastMillis = millis();
pressCount++;
}
else
{
if (millis() - lastMillis > pressInterval) // a long press
{
lastState = nowState;
pressCount = 0;
return -1;
}
}
}
if(pressCount != 0)
{
if(millis() - lastMillis > pressInterval and nowState == HIGH)
{
int presses = pressCount;
pressCount = 0;
return presses;
}
}
lastState = nowState;
return 0;
}
int SimplePress::setDebounce(uint8_t dbounce)
{
debouncePeriod = dbounce;
return debouncePeriod;
}
void SimplePress::setDebounceAll(uint8_t dbounce)
{
for (size_t button = 0; button < instanceCount; button++)
{
instances[button]->setDebounce(dbounce);
}
}
int SimplePress::getCount()
{
return instanceCount;
}
This code looks a little complicated, but I'll look into it.
Thanks a lot
With this Library:
#define KEY_PIN 4
#define REACTION_TIME 500 //The 5 clicks have to be made within 500 milliseconds (½ sec)
#include "SwitchPack.h"
DoubleClick key(KEY_PIN, PULLUP, REACTION_TIME);
TimedClick timer(KEY_PIN, PULLUP);
bool oledOn = false;
//setup==========
void setup() {
key.begin();
timer.begin();
}
//loop=======================
void loop(){
if(key.clickCount() == 5) {
oledOn = true;
//Turn on OLED
}
if(oled_on && timer.clicktime() >= 1000) { //Pressed for more than a second
//Output on a digital pin
}
}
Thanks,this helps a lot