【问题标题】:Arduino 2 Flow sensors for Kegerator用于 Kegerator 的 Arduino 2 流量传感器
【发布时间】: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
}

【问题讨论】:

    标签: arduino interrupt flow


    【解决方案1】:

    您正试图从一个引脚触发两个中断:

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

    rpm 永远不会被调用,因为您将其替换为 rpm2 因此将其中一个引脚分配更改为 1 并将传感器连接到引脚 3(请参阅此处以获取 variations per Arduino board

    (我在波士顿 ;-)

    【讨论】:

    • 今晚我会试一试,先生。非常感谢。让我们离线交流,但我的书中应该有 6 件装。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多