简体   繁体   English

我正在编码 Arduino,但我对将 2 个传感器和 1 个伺服与按钮结合起来感到困惑

[英]I'm coding Arduino, but I'm confused about combining 2 sensors and 1 servo with a push button

I'm coding Arduino, but I'm confused about combining 2 sensors and 1 servo with a push button.我正在编码 Arduino,但我对将 2 个传感器和 1 个伺服与按钮结合起来感到困惑。 I hope someone can help me.我希望有一个人可以帮助我。

I have made one by one the sensor coding and it works, but I want to combine them into one program.我已经一一制作了传感器编码并且它可以工作,但我想将它们组合成一个程序。

// code void loop water temperatur sensor       
void loop(void`{ 
  sensors.requestTemperatures(); 
  Celcius = sensors.getTempCByIndex(0);
  Serial.print(Celcius);
  Serial.println(" C ");
  delay(1000);
}

// this code push button with servo
// code void servo with push button
void loop() {
  if (digitalRead(pushButtonPin) == LOW) {
    buttonPushed = 1;
    Serial.println("Servo ON");
    delay(1000);
  }
  if (buttonPushed) {
    // change the angle for next time through the loop:
    angle = angle + angleStep;
         
    // reverse the direction of the moving at the ends of the angle:
    if (angle >= maxAngle) {
      angleStep = -angleStep;
      if (type == 1) {
        buttonPushed =0;                   
      }
    }
             
    if (angle <= minAngle) {
      angleStep = -angleStep;
      if (type == 2) {
        buttonPushed =0;       
      }
    }
            
    myservo.write(angle); // move the servo to desired angle 
    delay(100); // waits for the servo to get there
  }
}
// Ph Sensor code
void loop(void) {
  static unsigned long samplingTime = millis();
  static unsigned long printTime = millis();
  static float pHValue, voltage;
  if (millis() - samplingTime > samplingInterval) {
    pHArray[pHArrayIndex++] = analogRead(SensorPin);
    if (pHArrayIndex==ArrayLenth)
      pHArrayIndex=0;
    voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024;
    pHValue = 3 * voltage + Offset;
    samplingTime=millis();
  }
  if (millis() - printTime > printInterval) { //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
    Serial.print("Voltage:");
    Serial.print(voltage, 2);
    Serial.print("    pH value: ");
    Serial.println(pHValue, 2);
    digitalWrite(LED, digitalRead(LED) ^ 1);
    printTime = millis();
  }
}

double avergearray(int* arr, int number){
  int i;
  int max, min;
  double avg;
  long amount = 0;
  if (number <= 0) {
    Serial.println("Error number for the array to avraging!/n");
    return 0;
  }
  if (number<5) { //less than 5, calculated directly statistics
    for (i=0; i<number; i++) {
      amount += arr[i];
    }
    avg = amount / number;
    return avg;
  } else {
    if (arr[0] < arr[1]) {
      min = arr[0];
      max = arr[1];
    } else {
      min = arr[1];
      max = arr[0];
    }
    for (i=2; i<number; i++) {
      if (arr[i] < min) {
        amount += min; //arr<min
        min = arr[i];
      } else {
        if (arr[i] > max) {
          amount += max; //arr>max
          max = arr[i];
        } else {
          amount += arr[i]; //min<=arr<=max
        }
      } //if
    } //for
    avg = (double)amount / (number - 2);
  } //if
  return avg;
}

Your " Ph Sensor code " demonstrates how to do 2 things at different time intervals in one loop.您的“ Ph 传感器代码”演示了如何在一个循环中以不同的时间间隔做两件事。

void loop() {
    if (/* time is right to do thing 1 */) {
         // do thing 1
    }
    if (/* time is right to do thing 2 */) {
         // do thing 2
    }
}

This is called a state machine .这称为state 机器 You can extend this logic to do 4 or more things in one loop.您可以扩展此逻辑以在一个循环中执行 4 件或更多件事情。

Obviously you can't use delay as it blocks the entire loop, preventing other work from continuing.显然,您不能使用delay ,因为它会阻塞整个循环,从而阻止其他工作继续进行。 So you need to convert the first two loops into the structure similar to the one above.所以你需要将前两个循环转换成与上面类似的结构。 Then you will be able to merge all the loops into a single one.然后,您将能够将所有循环合并为一个循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM