c++ - 我正在编写 Arduino,但我对将 2 个传感器和 1 个伺服器与一个按钮结合起来感到困惑
问题描述
我正在编写 Arduino,但我对将 2 个传感器和 1 个伺服器与一个按钮结合起来感到困惑。我希望有一个人可以帮助我。
我已经一一制作了传感器编码并且它可以工作,但我想将它们组合成一个程序。
// 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;
}
解决方案
您的“ 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
}
}
这称为状态机。您可以扩展此逻辑以在一个循环中执行 4 件或更多件事情。
显然您不能使用delay
它,因为它会阻塞整个循环,从而阻止其他工作继续进行。所以你需要将前两个循环转换成与上面类似的结构。然后,您将能够将所有循环合并为一个循环。
推荐阅读
- c# - 使用 J2EE Web 服务签名请求但收到未签名响应
- java - 生产代码 + 测试模块信息 = 不可能?
- sql - 在视图结果中“创建主键”的方法
- java - 如何在火花数据集中创建一个新列
使用自定义类型
- vb6 - 如何在VB6中动态设置文本框值
- python - 为什么内存密集型递归函数的执行被抑制而没有错误?
- javascript - Uncaught (in promise) TypeError: $.templates is not a function
- regex - PySpark 中是否有类似于 python 的 re.findall() 函数的函数?
- c - 关于 C 中宏的数据类型
- sql-server - 从 Powershell 插入 SQL Server 数据库时,为什么我的(大)字符串会被截断?