【问题标题】:numTicks variable not incrementingnumTicks 变量不递增
【发布时间】:2013-07-29 19:13:39
【问题描述】:

我有以下草图,并且 numTicks 变量没有递增,草图可以很好地编译到 Arduino,但无论出于何种原因,变量“numTicks”没有递增。

/*
 * 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
 * https://github.com/just-kile/Zapfmaster2000/blob/master/src/zapfmaster2000-zapfkit-avr/draftkitAVR.ino
 *
 * 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
// SF800 default (5400 ticks/Liter == 5.4 ticks/mL == 1/5.4 mL/tick)
int flowmeterInterrupt = 0;  // changed from byte
int flowmeterPin = 2;        // changed from byte
int flowmeterPinState = 0;   // variable for storing state of sensor pin
// read RPM
int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0; 

// NEW GLOBALS - 29JUL13
// initial ticks on flow meter
volatile unsigned int numTicks = 0;
// interval for flow meter frequency
int interval = 250;
volatile long previousMillis = 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(flowmeterPin, INPUT);
  digitalWrite(flowmeterPin, 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(flowmeterInterrupt, count, 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);

  flowmeterPinState = digitalRead(flowmeterPin);

//  Serial.println(sensorPinState);

  volatile unsigned long currentMillis = millis();
  // if the predefined interval has passed
  if(currentMillis - previousMillis > interval) { // Uptade every 1/4 second, this will be equal to reading frecuency (Hz).

    // disconnect flow meter from interrupt
    detachInterrupt(flowmeterInterrupt); // Disable interrupt when calculating
    // check, whether any flow was detected
    if (numTicks >= 0) {
      // start message to computer with tick message symbol
      Serial.print("Ticks:");
      // send amount of ticks for last interval
      Serial.print(numTicks);
    }
    // clean buffer
    Serial.flush();
    // reset amount of ticks
    numTicks = 0;
    // set new start value for interval counter
    previousMillis = currentMillis;
    // reattach interrupt
    attachInterrupt(flowmeterInterrupt, count, RISING);
  }


  if(flowmeterPinState == LOW) {
    flow_A_off();
//    Serial.println("don't blink");
  }
  if(flowmeterPinState == HIGH) {
    flow_A_on();
//    Serial.println("blink damnit");
  }

  if(stringComplete) {
    if(inputString.equals("{close_valve}\n")) {
      //      Serial.println("close vavle.");
      close_valve();
    }
    return;
  }
}
// flow meter interrupt function
void count(){
  numTicks++;
} 

/*
 * 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;
    while(numTicks <= 1000) {
      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());
  }
}

【问题讨论】:

    标签: c embedded arduino avr


    【解决方案1】:

    numTicks 变量没有改变的原因可能是由于没有触发中断。您应该在 count() 中放置一个断点来确认这一点。然后你需要弄清楚为什么中断没有按预期触发,但这是另一个问题。

    【讨论】:

    • 对不起,我只是假设它确实如此。
    【解决方案2】:

    对不起我之前的回答。我没有完全理解这个问题。

    首先,您是否使用 Arduino UNO,如果是,请检查标有数字引脚 2 的板引脚映射为“int.0”,并确保流量计的中断线连接到此引脚。 (见:http://arduino.cc/en/Reference/AttachInterrupt)。

    根据上面 Chris 的 cmets,count() 例程是中断驱动的代码,而且它似乎编码正确:numTicks 被定义为 volatile;并且 count() 不会发出诸如 printf 之类的 I/O 命令;它不返回任何值。

    您提供的代码示例并未隔离或突出问题。我会写一个测试草图,它只是“打开”传感器的基本实现,然后感测来自流量计的中断并将其从主循环报告回控制台。如果你能得到从流量计中检测到一个中断的代码来工作,那么添加更多的代码来报告一秒内的中断数,然后是 1/2 秒,等等。

    最后,在您提供的代码中,您有片段:

        if(valve_open) {
             //    Serial.println("valve_open = true");
             inputString = "";
             stringComplete = false;
             while(numTicks <= 1000) {
                getFlow4();
             }
        }
    

    由于 numTicks 由中断例程计数递增,因此原则上我不会测试它,除非实现了某种序列化。 getFlow4() 分离中断,这是序列化对 numTicks 的访问的一种方法。笔记。理论上代码可以在没有序列化的情况下更新 numTicks,但返回的任何值都不一定准确,因为中断可能已经触发并增加了 numTicks。

    您的应用程序似乎有兴趣了解每秒的滴答数??在这种情况下,您不需要在停止中断之前测试 numTicks。您可能需要的只是每秒检查一次 numTicks 的代码,如果您可以忍受减少计数,则将 numTicks 清零,甚至无需分离中断。然而,采样 numTicks 更像是轮询,这是中断试图避免的。

    因此,由于代码会跟踪间隔,因此将间隔除以 numTicks 以获得 Hz 值,并且不要将 numTicks 归零或重置间隔,直到它们接近翻转。

    【讨论】:

      【解决方案3】:

      代码最终可以工作,这是流量计连接的硬件问题 (>.>)

      【讨论】:

        【解决方案4】:

        您从未真正调用过您的count() 方法。您应该将numTicks++; 插入到您希望增加计数的代码中(推荐方式),或者在您希望增加计数的地方调用count() 方法。这里 count() 方法只被定义而不被调用,但在代码中增加计数器会更有意义,因为这是你定义的方法唯一要做的事情。

        【讨论】:

        • 这一行呢,attachInterrupt(flowmeterInterrupt, count, RISING);
        • 没错,我错过了。其他一切正常吗?您可以尝试在count() 方法中添加控制台输出,以便确认该方法是否被调用。
        • @danielu13:中断处理程序中的控制台输出可能不是一个好主意 - 特别是如果输出本身是中断驱动的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-30
        相关资源
        最近更新 更多