Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

c++ - 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. 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;
}
question from:https://stackoverflow.com/questions/65884010/im-coding-arduino-but-im-confused-about-combining-2-sensors-and-1-servo-with

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your "Ph Sensor code" demonstrates how to do 2 things at different time intervals in one loop.

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. You can extend this logic to do 4 or more things in one loop.

Obviously you can't use delay as it blocks the entire loop, preventing other work from continuing. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...