2014-07-23 34 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