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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…