Re: Arduino - Hjälp att räkna pulser under 36s intervaller
Postat: 18 oktober 2018, 22:13:00
Jag byggde en varvräknare till båtmotorn, se http://elektronikforumet.com/forum/view ... 1&start=30 hur jag hanterar interrupt.
Svenskt forum för elektroniksnack.
https://elektronikforumet.com/forum/
Kod: Markera allt
#include <elapsedMillis.h>
elapsedMillis timer0;
const byte interruptPin = 2; // pin number Arduino Uno (2 or 3)
int timePeriod ; // tid som pulsmätning skall ske i ms.
volatile int antPuls = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(interruptPin), addPuls, RISING);
timePeriod = 36000 ;// mS
}
void loop() {
// put your main code here, to run repeatedly:
checkTimer();
}
void addPuls() {
antPuls++ ;
}
void checkTimer() {
if (timer0 > timePeriod) {
timer0 -= timePeriod ;
// Nollställ pulsräkningen här och skriv ut resultat.
Serial.print("Antal pulser: ");
Serial.println(antPuls);
antPuls = 0;
}
}