O'Reilly logo

Internet of Things Programming with JavaScript by Ruben Oliva Ramos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reading and counting pulses with Arduino

In this part of the code, we explain that it counts the signals from the sensor using an interrupt, executes, and we have configured it as RISING, so it counts the pulses from digital signal zero to digital signal one:

int pin = 2; 
volatile unsigned int pulse; 
constintpulses_per_litre = 450; 
 
void setup() 
{ 
Serial.begin(9600); 
 
pinMode(pin, INPUT); 
attachInterrupt(0, count_pulse, RISING); 
} 
 
void loop() 
{ 
pulse=0; 
interrupts(); 
delay(1000); 
noInterrupts(); 
 
Serial.print("Pulses per second: "); 
Serial.println(pulse); 
} 
 
voidcount_pulse() 
{ 
pulse++; 
} 

Open the Arduino Serial Monitor, and blow air through the water flow sensor using your mouth. The number of pulses per second will be printed on the Arduino Serial ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required