Calculating Signal Time - c

So i have rewritten my code. when I press the button connected on pin 2 it makes pin 13 HIGH and it sends a signal via a transceiver to a receiver (type of transceiver and receiver is irrelevant). I connected a wire from the receiver(where pin 13 makes it HIGH) to pin 7 on the arduino. Also I connected a LED to pin 8 to indicate when pin 7 is HIGH.
My main focus is to calculate the time it took from pressing the button until pin 7 is made HIGH on the Arduino. I am using a Arduino Leonardo( also irrelevant information).
This is my code :
int buttonState;
int buttonPin = 2;
int LbuttonState; // last button state
int pin13 = 13;
int pin7state;
int pin7 = 7;
int Lpin7state; // last pin 7 state
int pin8 = 8;
long startTimeKeeper;
long endTimeKeeper;
long elapsedTime;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(pin13, OUTPUT);
pinMode(pin7, INPUT);
pinMode(pin8, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH && LbuttonState == LOW) {
startTime(); // start the time
digitalWrite(pin13, HIGH);
LbuttonState = buttonState;
} else if(buttonState == HIGH && LbuttonState == LOW) {
digitalWrite(pin13, LOW);
} else if(buttonState == LOW && LbuttonState == HIGH) {
digitalWrite(pin13, LOW);
LbuttonState = buttonState;
} else//(buttonState == LOW && LbuttonState == LOW)
digitalWrite(pin13, LOW);
pin7state = digitalRead(pin7);
if(pin7state == HIGH && Lpin7state == LOW) {
stopTime(); // stop the time
digitalWrite(pin8, HIGH);
Lpin7state = pin7state;
} else if(pin7state == HIGH && Lpin7state == HIGH) {
digitalWrite(pin8, HIGH);
} else if(pin7state == LOW && Lpin7state == HIGH) {
digitalWrite(pin8, LOW);
Lpin7state = pin7state;
} else//(pin7state == LOW && Lpin7state == LOW)
digitalWrite(pin8, LOW);
}
void startTime() {
startTimeKeeper = millis();
}
void stopTime() {
endTimeKeeper = millis();`enter code here`
elapsedTime = endTimeKeeper - startTimeKeeper;
Serial.print(elapsedTime);
}

I would suggest using interrupts, especially since the Leonardo supports interrupts triggered on state changes of both of your chosen pins.
If I understand the core of the problem correctly, you want the time elapsed between a falling edge (HIGH to LOW) from your button press on pin 2 to a rising edge (LOW to HIGH) on pin 7. If I misinterpreted that and your button is actually active-high, just change the final parameter of attachInterrupt(interrupt, ISR, mode) to RISING.
After these are setup, our specified interrupt service routine (ISR) functions will be called whenever the specified state or state change occurs. We want to do minimal work in these ISRs since no other ISR can be triggered while one is running. Recording a start or stop time would be fine.
However, interrupts cannot use millis() or micros() directly because those functions themselves use interrupts. To work around that constraint, we'll toggle a simple flag of our own in each ISR--to indicate that it was triggered--then poll for that flag value in the main loop, where we'll do our timer start/stop actions. I subbed-in micros() for better accuracy since the time between a button press and a signal received should be small (never on the order of minutes, anyway).
#define ULONG_MAX 0xFFFFFFFFUL
unsigned long startTimeKeeper, stopTimeKeeper, elapsedTime;
volatile boolean buttonFlag = false, signalFlag = false;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(7, INPUT);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
// Int.2 corresponds to pin 2
attachInterrupt(2, buttonPressed, FALLING);
// Int.4 corresponds to pin 7
attachInterrupt(4, signalReceived, RISING);
}
void loop() {
// Loop until the buttonPressed ISR sets this flag
if (buttonFlag) {
// Record the start time
startTimeKeeper = micros();
// Do nothing until the signal flag is set by the ISR
while (!signalFlag);
// Record the end time
stopTimeKeeper = micros();
// Normal case - stop time is apparently after start time
if (stopTimeKeeper > startTimeKeeper)
elapsedTime = stopTimeKeeper - startTimeKeeper;
// Overflow case - stop time is apparently before start time
else
elapsedTime = stopTimeKeeper + (ULONG_MAX - startTimeKeeper);
Serial.print(elapsedTime);
signalFlag = buttonFlag = false;
}
}
// Very lightweight ISRs
void buttonPressed() {
buttonFlag = true;
}
void signalReceived() {
signalFlag = true;
}
Since we immediately begin waiting for the signalReceived() ISR to activate signalFlag after we register a button press, we don't have to worry too much about debouncing the switch in this case.
In general, you'll want to debounce your switches, either with physical circuitry or software counters. Check out this tutorial to get started with software, or look here for information on building debounce circuits.

Related

System Hangs by writing delay after Timer_init()

I am facing a strange issue while programming PIC18f45k22. I am initiating system modules and then I write a blocking delay for 3 seconds to wait for a touch screen to start (because I need to send some information to this screen). But when I do this I discovered that when I initiate timer before writing the delay, I discover that the system freezes.
This is the code that I used
NOTE: all the functions work fine without any problem.
void main(void) {
Sys_Init();
__delay_ms(3000);
while(1){
//Code
}
System initiation code
void Sys_Init(void) {
Delay_XTAL();
OSCILLATOR_Init();
DIGITAL_PIN_STATE();
ANALOG_PIN_STATE();
ADC_INIT(); //change clock select pins when oscillator changes
Spi_Init();
DHT22_init();
Uart_Init(9600); //initiate UART (change baud values when oscillator value changed)
Timer1_Init(); //initiate timer one (change prescaler and counter value if oscillator value changes)
}
Timer code
void Timer1_Init(void){ //for interrupt program use this function
//Prescaler 1:1; TMR1 Preload = 61536; Actual Interrupt Time : 1 ms
T1CON = 0x01;
TMR1IF = 0;
TMR1H = 0xF0;
TMR1L = 0x60;
TMR1IE = 1;
INTCON = 0xC0;
}
Interrupt code
void __interrupt() ISR (void){
Timer1();
}
void Timer1(void){ //for interrupt program
if (TMR1IF){
cnt1++;
TMR1H = 0xF0; //overflow every 1 ms
TMR1L = 0x60;
TMR1IF = 0;
}
}

How to update Serial.print statement in realtime using Arduino

I have the following sketch, https://github.com/ipatch/KegCop/blob/master/KegCop-Bluno-sketch.c that I'm running on Arduino UNO compatible microcontroller. However I am trying to print statements while the flowmeter is in use with the following function,
// flowmeter stuff
bool getFlow4() {
// call the countdown function for pouring beer
// Serial.println(flowmeterPin);
flowmeterPinState = digitalRead(flowmeterPin);
// Serial.println(flowmeterPinStatePinState);
volatile unsigned long currentMillis = millis();
// if the predefined interval has passed
if (millis() - lastmillis >= 250) { // Update every 1/4 second
// disconnect flow meter from interrupt
detachInterrupt(0); // Disable interrupt when calculating
// Serial.print("Ticks:");
Serial.print(numTicks);
// numTicks = 0; // Restart the counter.
lastmillis = millis(); // Update lastmillis
attachInterrupt(0, count, FALLING); // enable interrupt
}
if(numTicks >= 475 || valveClosed == 1) {
close_valve();
numTicks = 0; // Restart the counter.
valveClosed = 0;
return 0;
}
}
However, the print statements only get updated once the flowmeter has stopped operating. Any help would greatly be appreciated. As I would like the print statements to be updated while the flowmeter is in use.

for loop here is not working i am using bluetooth serial connection

void loop() // run over and over
{
while (!mySerial.available()); // stay here so long as COM port is empty
receivedChar = mySerial.read();
if (receivedChar == '1')
{
digitalWrite(LED, HIGH);
for (int i=0; i<500; i++)
{
digitalWrite(buz, HIGH);
delayMicroseconds(500);
digitalWrite(buz, LOW);
delayMicroseconds(500);
}
}// if it's a 1 turn LED on
if (receivedChar == '2')
{
digitalWrite(LED, LOW);
} // if it's a 2 turn LED off
} // if it is a 3 flash the LED
here "forloop" is not looping please help for this
EDIT with completely new answer:
Ok, I hope I understand your problem better. If you want to have the LED blinking until you receive a corresponding other command, you can do something like this (not explicitly tested):
Another edit: from your comment on the question I seem to understand that you want to toggle on and off the blinking whenever you are sending 1? If that that's the case, you can maybe add another boolean to the check which you toggle when you send a 1.
void loop() // run over and over
{
static bool active = false;
static char receivedChar = '0';
if (mySerial.available()) {
receivedChar = mySerial.read();
// if you received a 1, change the state of the 'active' boolean
if (receivedChar == '1')
{
active = !active;
}
}
// only perform the action on receiving 1
// only when also the boolean is set to the correct value
if (receivedChar == '1' && active)
{
digitalWrite(buz, HIGH);
delay(100);
digitalWrite(buz, LOW);
delay(100);
}// if it's a 1 turn LED on
if (receivedChar == '2')
{
digitalWrite(LED, LOW);
} // if it's a 2 turn LED off
} // if it is a 3 flash the LED
declaring receivedChar as static should keep its value the same for the next iteration of loop. This means that the loop will run and if you sent a 1 it will always enter the if (receivedChar == '1') condition and switch the LED on and off with whatever delay you choose and then simply repeat this until you send another character, e.g. 2 at which point he will enter condition if (receivedChar == '2'), switches off the LED and will simply go back to if (receivedChar == '2') until you send something else again.
Does this help?
Yet another edit:
with an arduino and a LED connected to pin 3, the following sketch does what you are asking:
Send 1 over the serial port, the LED starts blinking. Send another 1, the blinking stops. If you send a 2, the blinking also stops.
If this sketch does not show the behavior described above, then you are doing something in your code that you are not disclosing.
int LED = 3;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(LED, OUTPUT);
}
void loop() // run over and over
{
static bool active = false;
static char receivedChar = '0';
if (Serial.available()) {
receivedChar = Serial.read();
// if you received a 1, change the state of the 'active' boolean
if (receivedChar == '1')
{
active = !active;
}
}
// only perform the action on receiving 1
// only when also the boolean is set to the correct value
if (receivedChar == '1' && active)
{
digitalWrite(LED,HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
}// if it's a 1 turn LED on
if (receivedChar == '2')
{
digitalWrite(LED, LOW);
active = false;
} // if it's a 2 turn LED off
} // if it is a 3 flash the LED
I would recommend to use String and append the single characters and use Serial.available() > 0 with a short delay as loop condition. I hope that this is going to help you.
int led = 13;
void setup(){
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.flush();
}
void loop()
{
String receivedChars = "";
while (Serial.available() > 0){
receivedChars += (char) Serial.read();
delay(5);
}
if (receivedChars == "1"){
digitalWrite(led, HIGH);
}
else if (receivedChars == "2"){
digitalWrite(led, LOW);
}
}
EDIT Seeing the discusion in other answers, seems like you want to make a LED blink when received a 1 and stop blinking when received a 2. First you should edit the original question, because sometimes you write digitalWrite(LED, HIGH) and sometimes it's digitalWrite(buz, HIGH). That's confusing if you are using the same LED in the same pin.
Then I would suggest this code.
boolean blink = false;
byte state = 0;
void loop()
{
if (mySerial.available()) // there is some data to read
{
receivedChar = mySerial.read();
switch (receivedChar)
{
case '1':
blink = true;
break;
case '2':
blink = false;
digitalWrite(LED, LOW); //turn off LED
break;
}
}
if (blink)
{
state ^= 1; //switch bit using xor operator
digitalWrite(PED, state);
}
delay(500);
}
Hope it's easy to understand. If not, feel free to ask.
OLD POST: Are you sure it's not looping? May be if (receivedChar) condition is always false? Send some debugging info through serial port to check it.
Also, the loop() function is an infinite loop, there's no need to add one more. Just check if (mySerial.available()).
Seems like you want to make some sound while the LED is on, you can use the Tone function for that.
I would suggest something like this:
void loop()
{
if (mySerial.available()) // there is some data to read
{
receivedChar = mySerial.read();
if (receivedChar == '1')
{
mySerial.println("Received a 1"); // for debugging
digitalWrite(LED, HIGH);
tone(buz, 1000, 500);
}
if (receivedChar == '2')
{
mySerial.println("Received a 2"); // for debugging
digitalWrite(LED, LOW);
}
}
}

Not getting output to the serial monitor of the Arduino IDE from the getFlow function

So I hacked at a getFlow function for the Arduino sketch I am working on, and I am getting output of the function in the console of the iPhone, but when I hook up the computer to the Arduino, and open the serial monitor I don't see any output from the getFlow4 function. I am using a Swiss flow SF800 flowmeter / flowsensor.
/*
* kegboard-clone-4-KegCop
* This code is public domain
*
* This sketch sends a receives a multibyte String from the iPhone
* and performs functions on it.
*
* This Arduino sketch is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Public License
* along with this sketch. If not, see <http://www.gnu.org/licenses/>.
*
* Examples:
* http://arduino.cc/en/Tutorial/SerialEvent
* http://arduino.cc/en/Serial/read
* http://stackoverflow.com/questions/16532586/arduino-sketch-that-responds-to-certain-commands-how-is-it-done/
* http://davebmiller.wordpress.com/2011/01/18/arduino-flowmeter/
* http://forum.arduino.cc/index.php?topic=52003.0
* http://arduino.cc/en/Reference/AttachInterrupt
*
* TODO:
* - eventually get code working with the SF800 flow sensor / flowmeter
*
*/
// flow_A LED
int led = 4;
// relay_A
const int RELAY_A = A0;
// string / serial event variables
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
boolean valve_open = false;
// FLOWMETER SHIT
// flowmeter 0 pulse (input) = digital pin 2
// https://github.com/Kegbot/kegboard/blob/master/arduino/kegboard/kegboard_config.h
// which pin to use for reading the sensor? kegboard-mini shield has digital pin 2 allocated
// the SF800 outputs 5400 pulses per litre
// The hall-effect flow sensor (SF800) outputs approximately 5400 pulses per second per litre/minute of flow
int sensorInterrupt = 0; // changed from byte
int sensorPin = 2; // changed from byte
int sensorPinState = 0; // variable for storing state of sensor pin
// read RPM
int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
void setup() {
// initialize serial
// Serial.flush(); // flush the serial buffer on setup.
Serial.begin(115200); // open serial port, sets data rate to 9600bps
Serial.println("Power on test");
inputString.reserve(200);
valve_open = false;
// relay for solenoid cut off valve
pinMode(RELAY_A, OUTPUT);
// flowmeter shit
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH); // Need to set these HIGH so they won't just tick away
// The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
// Configured to trigger on a RISING state change (transition from HIGH
// state to LOW state)
attachInterrupt(0, rpm_fan, RISING);
}
void open_valve() {
digitalWrite(RELAY_A, HIGH); // turn RELAY_A on
valve_open = true;
}
void close_valve() {
digitalWrite(RELAY_A, LOW); // turn RELAY_A off
valve_open = false;
}
void flow_A_blink() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for one second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void flow_A_blink_stop() {
digitalWrite(led, LOW);
}
void flow_A_on() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
}
void flow_A_off() {
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
}
// flowmeter shit
void getFlow4() {
// Serial.println("im here");
// Serial.println(sensorPin);
sensorPinState = digitalRead(sensorPin);
// Serial.println(sensorPinState);
if (millis() - lastmillis >= 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz).
detachInterrupt(0);//Disable interrupt when calculating
rpm = rpmcount * 60; // Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.
Serial.print("RPM =\t"); //print the word "RPM" and tab.
Serial.print(rpm); // print the rpm value.
Serial.print("\t Hz=\t"); //print the word "Hz".
Serial.println(rpmcount); //print revolutions per second or Hz. And print new line or enter.
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan, RISING); //enable interrupt
}
if(sensorPinState == LOW) {
flow_A_off();
Serial.println("don't blink");
}
if(sensorPinState == HIGH) {
flow_A_on();
Serial.println("blink damnit");
}
if(stringComplete) {
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
return;
}
}
void rpm_fan(){
rpmcount++;
}
/*
* Main program loop, runs over and over repeatedly
*/
void loop() {
if(stringComplete) {
// Serial.println(inputString);
if(inputString.equals("{open_valve}\n")) {
// Serial.println("inputString equates :)");
open_valve();
}
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
if(valve_open) {
// Serial.println("valve_open = true");
inputString = "";
stringComplete = false;
getFlow4();
}
// clear the string:
inputString = "";
stringComplete = false;
}
//Serial.println("over and over");
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while(Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
// Serial.println(inputString.length());
}
}

Counting pulses using a swiss flow meter with an Arduino, how is it done?

So I'm trying to count the pulses of a swiss flow SF800 flow sensor / meter in my current Arduino project, but I'm currently not getting any data printed out to the console. My Arduino sketch looks like the following,
// global variables should be identified with _
// flow_A LED
int led = 4;
// relay_A
const int RELAY_A = A0;
// variables from sketch example
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
// FLOWMETER SHIT
// flowmeter 0 pulse (input) = digital pin 2
// https://github.com/Kegbot/kegboard/blob/master/arduino/kegboard/kegboard_config.h
// which pin to use for reading the sensor? kegboard-mini shield has digital pin 2 allocated
# define FLOWSENSORPIN 2
int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
void setup() {
// initialize serial
Serial.begin(9600); // open serial port, sets data rate to 115200bps
inputString.reserve(200);
pinMode(RELAY_A, OUTPUT);
// flowmeter shit
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH); // Need to set these HIGH so they won't just tick away
attachInterrupt(0, rpm_fan, FALLING); // interrupt is attached, is on pin two(2).
}
void open_valve() {
digitalWrite(RELAY_A, HIGH); // turn RELAY_A on
// Serial.println("Valve Open");
Serial.write("{valve_open}");
}
void close_valve() {
digitalWrite(RELAY_A, LOW); // turn RELAY_A off
// Serial.println("Vavle Closed");
Serial.write("{valve_close}");
}
void flow_A_blink() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for one second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void flow_A_blink_stop() {
digitalWrite(led, LOW);
}
// flowmeter shit
void getFlow() {
Serial.println("reached getFlow function");
if(millis() - lastmillis == 1000) { // Update every one second, this will be equal to reading frequency (Hz).
detachInterrupt(0); // Disable interrupt when calculating
rpm = rpmcount * 60; // Convert frequency to RPM, note: this works for one interruption per full rotation.
Serial.print("RPM =\t"); // print the word "RPM and tabl.
Serial.print(rpm); // print the rpm value
Serial.print("\t Hz=\t"); // print the word "Hz".
Serial.println(rpmcount); // print revolutions per second or Hz. And print new line or enter.
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Update lastmillis
attachInterrupt(0, rpm_fan, FALLING); // enable interrupt
}
}
void rpm_fan() { // this code will be executed every time the interrupt 0 (pin2) gets low.
rpmcount++;
}
/*
* Main program loop, runs over and over repeatedly
*/
void loop() {
if(stringComplete) {
// Serial.println(inputString);
if(inputString.equals("{open_valve}\n")) {
// Serial.println("opening valve.");
open_valve();
getFlow();
}
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while(Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
// Serial.println(inputString.length());
}
}
The following code changes seem to be printing the RPM and Hz values now.
// flow_A LED
int led = 4;
// relay_A
const int RELAY_A = A0;
// variables from sketch example
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
// FLOWMETER SHIT
// flowmeter 0 pulse (input) = digital pin 2
// https://github.com/Kegbot/kegboard/blob/master/arduino/kegboard/kegboard_config.h
// which pin to use for reading the sensor? kegboard-mini shield has digital pin 2 allocated
# define FLOWSENSORPIN 2
int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
void setup() {
// initialize serial
Serial.begin(9600); // open serial port, sets data rate to 115200bps
inputString.reserve(200);
pinMode(RELAY_A, OUTPUT);
// flowmeter shit
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH); // Need to set these HIGH so they won't just tick away
attachInterrupt(0, rpm_fan, FALLING); // interrupt is attached, is on pin two(2).
}
void open_valve() {
digitalWrite(RELAY_A, HIGH); // turn RELAY_A on
// Serial.println("Valve Open");
Serial.write("{valve_open}");
}
void close_valve() {
digitalWrite(RELAY_A, LOW); // turn RELAY_A off
// Serial.println("Vavle Closed");
Serial.write("{valve_close}");
}
void flow_A_blink() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for one second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void flow_A_blink_stop() {
digitalWrite(led, LOW);
}
// flowmeter shit
void getFlow() {
// Serial.println("reached getFlow function");
if(millis() - lastmillis >= 1000) { // Update every one second, this will be equal to reading frequency (Hz). Using >= should be safter
// Serial.println("reached inside if statement");
detachInterrupt(0); // Disable interrupt when calculating
rpm = rpmcount * 60; // Convert frequency to RPM, note: this works for one interruption per full rotation.
Serial.print("RPM =\t"); // print the word "RPM and tabl.
Serial.print(rpm); // print the rpm value
Serial.print("\t Hz=\t"); // print the word "Hz".
Serial.println(rpmcount); // print revolutions per second or Hz. And print new line or enter.
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Update lastmillis
attachInterrupt(0, rpm_fan, FALLING); // enable interrupt
}
}
void rpm_fan() { // this code will be executed every time the interrupt 0 (pin2) gets low.
rpmcount++;
}
/*
* Main program loop, runs over and over repeatedly
*/
void loop() {
if(stringComplete) {
// Serial.println(inputString);
if(inputString.equals("{open_valve}\n")) {
// Serial.println("opening valve.");
open_valve();
}
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
// clear the string:
inputString = "";
stringComplete = false;
}
getFlow();
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while(Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
// Serial.println(inputString.length());
}
}

Resources