每隔60秒取电压C ++ Sketch

时间:2018-02-11 02:32:57

标签: c++

有人可以解释如何每60秒将此程序修改为“ReadAnalogVoltage”一次,永远。

现在,它每隔毫秒读取一次电压(我认为),永远。

正在生成太多信息。

任何建议都将不胜感激。

罗布。

    > /*
     ReadAnalogVoltage
     Reads an analog input on pin 0, converts it to voltage, and prints 
     the result to the serial monitor.
      Attach the center pin of a potentiometer to pin A0, and the outside 
     pins to +5V and ground.

     This example code is in the public domain.
   */

   // the setup routine runs once when you press reset:
   void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
   }

   // the loop routine runs over and over again forever:
   void loop() {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);
    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
    float voltage = sensorValue * (5.0 / 1023.0);
    // print out the value you read:
    Serial.println(voltage);
    }

1 个答案:

答案 0 :(得分:0)

有几种方法可以降低采样率。在您的循环内放置一个计数器,仅在计数器达到某个值时打印。请务必在打印后重置​​该值。

int counter = 0;
void loop() {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);
    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
    float voltage = sensorValue * (5.0 / 1023.0);
    // print out the value you read:
    if(counter >= 1000000){
        Serial.println(voltage);
        counter = 0;
    }
}
  1. 解决此问题的最佳方法是使用时间interrupt。设置中断的实际机制取决于您使用的硬件。