2014-07-23 35 views
0

下午好。在谦逊地工作的同时,我来到了一个十字路口,在每个小桶水龙头倾倒时我必须运行中断。感谢https://www.carriots.com可爱的人们,我有我正在使用的这段代码。但是,试图从2个不同的流量传感器读取传感器读数时,它无法读取Keg1中的数据。函数“checkKeg1()”永远不会被调用。Arduino 2用于Kegerator的流量传感器

我的下一个想法是它是我误接线,但是如果我交换输入引脚,另一个传感器工作,证明这个假设是错误的。对于任何能够帮助我解决这个问题的人来说,那是年龄和波士顿地区的问题,请随时参与完成项目的抽样。

/* 
    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 
} 

回答

0

您试图触发两个中断关闭一个引脚:

attachInterrupt(0, rpm, RISING); // Interrupt is attached to rpm function 
    attachInterrupt(0, rpm2, RISING); 

rpm永远不会说是因为你用rpm2替换它因此改变引脚分配的一个为1和线传感器针脚3(在这里看到variations per Arduino board

(我在波士顿;-)

+0

I'l我今晚给了一个镜头先生..非常感谢。让我们离线沟通,但你应该在我的书中获得6件装。 – ChrisBurns

+0

嘿,工作完美!先生,再次感谢你!打我离线:[email protected] – ChrisBurns