Go Down

Topic: Problem with counting Button Pushes (Read 175 times) previous topic - next topic

stregoi

Feb 07, 2018, 02:51 pm Last Edit: Feb 07, 2018, 02:52 pm by stregoi
Hi, I am trying to finde a way to gather statistics about the use of an "alcohol dispenser", The surgeon need to rub his/her hands for 2 min with alcohol, to get his hands sterile.
So i made a "button" that is connected to the alcohol dispenser, when the surgeon presses the handle :

- A display countdown timer, switches on, so the surgen kan keep track about the 2 minuttes.
- The button is then disabled, so the counter can not be reset while it is not counting.
- The number of Button pushes is counted.
- When the 2 minutes is over, statistics including number of button pushes are syncronised through wifi.

I have this code :

Code: [Select]
void loop() {

    buttonState = digitalRead(Button);

    int count = (start_num - ((millis()/1000)- start_ms));

      if (buttonState == HIGH && count > 0) {

displayNumber(count);

      }

      else {

         // turn the display off when it finishes counting

         lightNumber(10);

      }
   
      if (buttonState == HIGH && count < 0) {

        delay(200);

        int (start_num = Time);

        start_ms = millis()/1000;

        buttonPushCounter++;

      }

      if (buttonState == HIGH && count = 0) {

        delay(200);

// the 2:00 is over => retrieve

// buttonPushcounter variable and send it through wifi
      }
}code]

Unfortunatly i just cannot get the counting of my button pushes right, i guess so weird numbers in the serial monitor. I am new to programming, please any suggestion ?

ardly

You have written a lot of code, but you don't really know what it does.

Start with a really simple loop that does nothing but count the number of button pushes and print the result.
Forget about delays or anything else.

Once you get your simple counting working start adding extra functionality.
"Facts do not cease to exist because they are ignored" - Aldous Huxley

stregoi

Thanks for the answer, that was a good hint !! .. so i started from scratch and rewrote my code

Code: [Select]

#include "SevSeg.h"

SevSeg sevseg;
int buttonPin = 2;
unsigned long timer = millis();
int deciSeconds = 2000;
int buttonPushCounter = 1;
volatile unsigned long lastButtonPress = 0;
volatile int buttonPressed = 0;
int timerActive = 0;

void setup() {
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonPressInterrupt, CHANGE);
  pinMode(buttonPin, INPUT_PULLUP);

  byte numDigits = 4;   
  byte digitPins[] = {9, 10, 11, 12};
  byte segmentPins[] = {A0, 13, 3, 4, 5, 6, 7, 8};

  sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins);
  sevseg.setBrightness(20);
  sevseg.setNumber(2000, 1);
}

void loop() {

  if (buttonPressed) {
    if (deciSeconds == 2000) {
       timerActive = 1;
    }
    buttonPressed = 0;
    timer = millis();
    buttonPushCounter++;
  }
 
  if (timerActive && millis() >= timer) {
    deciSeconds--;
    timer += 100;
    if (deciSeconds == 1999)
    {
      deciSeconds=1599;
    }
    if (deciSeconds == 999)
    {
      deciSeconds=599;
    }
    if (deciSeconds == 0)
    {
      timerActive = 0;
      delay(1000);
      deciSeconds=2000;
    }
    sevseg.setNumber(deciSeconds, 1);
  }
  sevseg.refreshDisplay();
}

void buttonPressInterrupt() {
  if (millis() - lastButtonPress >= 300) {
    buttonPressed = 1;
    lastButtonPress = millis();
    Serial.println(buttonPushCounter);
  }
}


It is working as it should now. Though i have a little problem i cant figure it out. How can i turn The display off when it finishes counting ?

Code: [Select]
    if (deciSeconds == 0)
    {
      timerActive = 0;
      delay(1000);
      deciSeconds=2000; //<= what do i change ?
    }

stregoi

I figured that out !. for others that could have the same problem. i used "SevSeg.blanc()"

Go Up