0

我有以下草图,并且 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());
  }
}
4

4 回答 4

0

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

于 2013-07-29T23:36:17.343 回答
0

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

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

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

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

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

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

Since numTicks is incremented by the interrupt routine count, as a matter of principal I would NOT test it unless some of kind of serialization was implemented. getFlow4() detaches the interrupt which is one way to serialize access to numTicks. Note. in theory code can update numTicks without serialization but any value returned is not necessarily accurate as the interrupt might have fired and increased numTicks.

It seems like your application is interested in knowing the number of ticks per second?? in which case you do NOT need to test numTicks prior to stopping the interrupts. All you might need is code that once every second checks numTicks and if you can live with dropping a count then zero out numTicks without even detaching the interrupt. However, sampling numTicks is more like polling which is what an interrupt is trying to avoid.

So, since the code is keeping track of intervals then divide the interval by numTicks to get a Hz value and don't zero out numTicks or reset the interval until they get close to rolling over.

于 2013-07-30T20:24:45.863 回答
0

The code ended up working, it was hardware issue with the connections from the flowmeter (>.>)

于 2013-07-30T22:15:47.633 回答
-1

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

于 2013-07-29T19:20:01.513 回答