【发布时间】:2014-07-23 03:29:45
【问题描述】:
大家下午好。当我谦卑地在我的桶上工作时,我来到了一个十字路口,当每个桶龙头倒出时,我必须中断。感谢https://www.carriots.com 的可爱的人们,我正在使用这个代码。但是,尝试从 2 个不同的流量传感器获取传感器读数时,它无法从 Keg1 读取数据。永远不会调用函数“checkKeg1()”。
我的下一个想法是我接线错误,但是如果我交换输入引脚,另一个传感器工作,证明这个假设是错误的。对于任何可以帮助我解决这个问题的人,并且是在波士顿地区的年龄和年龄,请随时通过完成项目的样本。
/*
Carriots.com
Created: January 13, 2014
Suggested Modifications from another approach: RMHayes Dec 27, 2013
*/
int flowPin = 2; // Arduino flowmeter pin number
int flowPin2 = 4;
// Declare variables
float pulsesCounter; // Main pulses counter
float pulsesCounter2;
float pulsesAux; // Auxiliary counter
float pulsesAux2;
float pulsesPrev; // Auxiliary counter to track pulses activity between loops
float pulsesPrev2;
/**
* Interrupt Service Routine for interrupt 0 (ISR0)
* ISR0 services an interrupt condition on Pin 2 - whenever voltage on that pin rises.
*/
void rpm ()
{
pulsesCounter++; // Every RISING pulse causes pulsesCounter to increase by one.
}
void rpm2 () {
pulsesCounter2++;
}
void setup()
{
pinMode(flowPin, INPUT); // Initialize the digital pin as an input
pinMode(flowPin2, INPUT);
Serial.begin(9600); // Start serial port
attachInterrupt(0, rpm, RISING); // Interrupt is attached to rpm function
attachInterrupt(0, rpm2, RISING);
sei(); // Enable interrupt 0 on Pin 2 for a RISING signal.
}
void checkKeg1()
{
cli(); // Disable interrupts to check the counter
pulsesAux = pulsesCounter; // Copy the ISR variable (main counter). Don't corrupt the counting process
sei(); // Enable interrupts
// If pulses are non-zero and doesn't change during a loop (delay time: ~1sec),
// send the data to the Serial port
if ( (pulsesAux != 0) && (pulsesPrev == pulsesAux) ) {
//Check Keg1 Level
Serial.print("Keg1:"); // Sending the string
Serial.println (pulsesAux, DEC); // Sending the pulses counter
cli(); // Disable interrupts
pulsesCounter = 0; // Reset main counter
sei(); // Enable interrupts
pulsesPrev = 0; // Reset previous counter
pulsesAux = 0; // Reset auxiliary counter
}
cli(); // Disable interrupts to copy the pulses count
pulsesPrev = pulsesAux;
sei(); // Enable interrupts
}
void checkKeg2()
{
cli(); // Disable interrupts to check the counter
pulsesAux2 = pulsesCounter2; // Copy the ISR variable (main counter). Don't corrupt the counting process
sei();
if ( (pulsesAux2 != 0) && (pulsesPrev2 == pulsesAux2) ) {
//Check Keg 2 Level
Serial.print("Keg2:");
Serial.println ( pulsesAux2, DEC); // Sending the pulses counter
cli(); // Disable interrupts
pulsesCounter2 = 0; // Reset main counter
sei(); // Enable interrupts
pulsesPrev2 = 0; // Reset previous counter
pulsesAux2 = 0; // Reset auxiliary counter
}
cli(); // Disable interrupts to copy the pulses count
pulsesPrev2 = pulsesAux2;
sei(); // Enable interrupts
}
void loop ()
{
Serial.print(myPin);
//checkKeg1();
// checkKeg2();
delay(800); // Delay time between loops 1sec
}
【问题讨论】: