rjbatista / tm1638-library

Automatically exported from code.google.com/p/tm1638-library
141 stars 75 forks source link

question on button bounce #46

Closed ve3sjk closed 5 years ago

ve3sjk commented 5 years ago

I am implementing your library for an application i am developing where the buttons will be turning on relays in the application. Once a button is pressed i need the relay to turn on and then a CAN bus message is sent after that happens. I am using the code below to test the basic operation and i find that the serial window is getting constant messages every time i press a button.

I need my app to do a sub routine call only once for a given button press and only send the serial or CAN message only once. How would implement a button routine that latches the buttons in a way so i only get a single operation happening each time.

If i run the code below the serial window shows a constant stream of data with the case (last button pressed) being printed. It starts at case 0 and changes as i press the buttons but it does constantly send the serial messages which is not the behavior i need.

`#include

include

define NO_MODULES 1

// define a regular module and a inverted module TM1638 module1(46, 47, 45); InvertedTM1638 module2(45, 47, 46); TM1638* modules[NO_MODULES] = { &module1, // &module2 }; byte modes[NO_MODULES];

unsigned long startTime;

void setup() {

Serial.begin(115200); startTime = millis();

for (int i = 0; i < NO_MODULES; i++) { modules[i]->setupDisplay(true, 7); modes[i] = 0; } }

void update(TM1638 module, byte mode) { byte buttons = module->getButtons(); unsigned long runningSecs = (millis() - startTime) / 1000;

// button pressed - change mode if (buttons != 0) { *mode = buttons >> 1; module->clearDisplay(); module->setLEDs(0); }

switch (*mode) { case 0: module->setDisplayToDecNumber(runningSecs, 1 << 7); Serial.println("case 0"); break; case 1: module->setDisplayToDecNumber(runningSecs, 1 << 6, false); Serial.println("case 1"); break; case 2: module->setDisplayToHexNumber(runningSecs, 1 << 5); Serial.println("case 3"); break; case 4: module->setDisplayToHexNumber(runningSecs, 1 << 4, false); Serial.println("case 4"); break; case 8: module->setDisplayToBinNumber(runningSecs, 1 << 3); Serial.println("case 8"); break; case 16: module->clearDisplayDigit((runningSecs - 1) % 8, 0); module->setDisplayDigit(runningSecs % 8, runningSecs % 8, 0); Serial.println("case 16"); break; case 32: char s[8]; sprintf(s, "Secs %03d", runningSecs % 999); module->setDisplayToString(s); Serial.println("case 32"); break; case 64: if (runningSecs % 2 == 0) { module->setDisplayToString("TM1638 "); } else { module->setDisplayToString("LIBRARY "); }

  module->setLED(0, (runningSecs - 1) % 8);
  module->setLED(1 + runningSecs % 3, runningSecs % 8);
  Serial.println("case 64");     
  break;
case 65:
  module->setDisplayToError();
  Serial.println("case 65"); 
  break;

} }

void loop() { for (int i = 0; i < NO_MODULES; i++) { update(modules[i], &modes[i]); } } `

maxint-rd commented 5 years ago

Hi Steve, I'm not sure if you already found your solution, but in my TM16xx library (which is based on this one) next to other additions and changes, I implemented a TM16xxButtons class, which (based on the OneButton library) implements different button events such as Clicked, DoubleClicked and LongPressed. Although I can't guarantee my library works perfect for your needs, feel free to read the documentation first, try the example and give it a go...