Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Lars Bo Wassini
#70871 Complete (WORKING!) code here:

Code: Select all
const int pinButton = 0;
const int pinLed = 15;
const int pinFlowmeter = 5;

unsigned int totalCount = 500;

volatile unsigned int intCounter = 0;
bool buttonPressed = false;
int debugmeter = 0;

void setup() {
  pinMode(pinLed, OUTPUT);
  pinMode(pinButton, INPUT_PULLUP);
  pinMode(pinFlowmeter, INPUT);

  digitalWrite(pinLed, LOW);

  Serial.begin(115200);
  while (!Serial) {}
  Serial.println();
  Serial.println("Ready");

}

void loop() {

  if (intCounter > ((totalCount/10) * (debugmeter+1))) {
    debugmeter++;
    Serial.println(String(debugmeter*10) + "%");
  }

  if (intCounter >= totalCount) {
    detachInterrupt(digitalPinToInterrupt(pinFlowmeter));
    Serial.println();
    Serial.println("Complete: " + String(intCounter));
    intCounter = 0;
    buttonPressed = false;
    digitalWrite(pinLed, LOW);
  }

  if (!buttonPressed && digitalRead(pinButton) == LOW) {
    Serial.println();
    pressButton();
  }

  delay(10);
}

void ICACHE_RAM_ATTR getcount() { // IMPORTANT!!! Have to be in RAM!
  intCounter++;
}

void pressButton() {
  if (!buttonPressed) {  // prevent calling this several times
    attachInterrupt(digitalPinToInterrupt(pinFlowmeter), getcount, FALLING);
    buttonPressed = true;
    digitalWrite(pinLed, HIGH);
    Serial.println("Button pressed");
    debugmeter = 0;
  }
}