为什么不能将 X 用作函数?
Why can't X be used as a function?
对于混乱的代码库,我们深表歉意。我是 C++ 新手。
我正在尝试将循环和功能结合在一起。 (1) 湿度传感器和 (2) 伺服系统,用于根据湿度打开和关闭控制杆。
我收到一条错误消息,指出 'servo' 不能用作函数。 我试过将伺服系统更改为其他名称。我没有在其他任何地方使用伺服,例如变量,所以这似乎不是问题。
还有人有其他建议吗?
#include "Servo.h"
Servo myservo; // create servo object to control a servo
int pos = 0; // servo position variable
#define SensorPin A0
float sensorValue = 0;
bool lever = 0; // soil moisture sensor variable; 0 = dry; 1 = wet;
bool pump = 0; // pump is set to 0, which = off
// servo function
void servo(bool);
void setup() {
Serial.begin(9600); // soil moisture sensor setup
myservo.attach(9); // servo setup
}
// soil moisture sensor loop
void loop() {
for (int i = 0; i <= 100; i++) {
sensorValue = sensorValue + analogRead(SensorPin);
delay(20);
}
sensorValue = sensorValue/100.0;
Serial.print("Moisture level: ");
Serial.print(sensorValue);
Serial.print(";");
if(sensorValue > 1000) {
lever = 0; // dry
}
if(sensorValue < 1000) {
lever = 1; // wet
}
switchOnOff(lever);
delay(1000);
}
// define the servo function
void switchOnOff(bool lever) {
Serial.print(" Lever: ");
Serial.print(lever);
Serial.print(";");
Serial.print(" Pump: ");
Serial.print(pump);
Serial.println(";");
if(lever == 0 && pump == 0) { // it is dry and pump is off, pump needs to be turned on
for (pos = 90; pos <= 180; pos++) { // goes from 0 degrees to 90 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
delay(4000);
for (pos = 180; pos >= 90; pos--) { // goes from 90 degrees to 0 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
pump = 1; // noting that pump is on
}
if(lever == 0 && pump == 1) { // it is dry and pump is on
return; // do nothing, return
}
if(lever == 1 && pump == 1) { // it is wet and pump is on, pump needs to be turned off
for (pos = 90; pos <= 180; pos++) { // goes from 90 degrees to 0 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
delay(4000);
for (pos = 180; pos >= 90; pos--) { // goes from 0 degrees to 90 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
pump = 0; // noting that pump is off
}
if(lever == 1 && pump == 0) { // it is wet and pump is off
return; // do nothing, return
}
}
谢谢。
问题在于您在正确声明之前尝试使用 servo()
函数。首先,您需要先声明一个函数,然后才能在您的代码中提及它。其次,您对 servo()
的定义不正确。 int servo(lever)
,其中 lever
不是类型。您应该查看以下内容:int servo(bool lever);
,其中 lever
是 servo()
函数采用的类型 bool
的参数。
然后,您的函数不会 return 任何东西,它只有一些 “副作用”,例如 myservo.write(pos);
。所以,应该是 void
.
试试下面的安排:
#include "Servo.h"
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
// Declare the function
void servo(bool);
int pos = 0; // variable to store the servo position
#define SensorPin A0
float sensorValue = 0;
bool lever = 0; // lever is global variable of type bool
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop() {
for (int i = 0; i <= 100; i++) {
sensorValue = sensorValue + analogRead(SensorPin);
delay(1);
}
sensorValue = sensorValue/100.0;
Serial.println(sensorValue);
delay(30);
if(analogRead(0 > 100)) {
lever = 1;
}
if(analogRead(0 < 100)) {
lever = 0;
}
servo(lever);
}
// Define the function
void servo(bool lever) {
if(lever == 1) { // On
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
}
}
if(lever == 0) { // Off
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
}
}
}
P.S。 setup()
在这里和那里重新定义也很混乱。
对于混乱的代码库,我们深表歉意。我是 C++ 新手。
我正在尝试将循环和功能结合在一起。 (1) 湿度传感器和 (2) 伺服系统,用于根据湿度打开和关闭控制杆。
我收到一条错误消息,指出 'servo' 不能用作函数。 我试过将伺服系统更改为其他名称。我没有在其他任何地方使用伺服,例如变量,所以这似乎不是问题。
还有人有其他建议吗?
#include "Servo.h"
Servo myservo; // create servo object to control a servo
int pos = 0; // servo position variable
#define SensorPin A0
float sensorValue = 0;
bool lever = 0; // soil moisture sensor variable; 0 = dry; 1 = wet;
bool pump = 0; // pump is set to 0, which = off
// servo function
void servo(bool);
void setup() {
Serial.begin(9600); // soil moisture sensor setup
myservo.attach(9); // servo setup
}
// soil moisture sensor loop
void loop() {
for (int i = 0; i <= 100; i++) {
sensorValue = sensorValue + analogRead(SensorPin);
delay(20);
}
sensorValue = sensorValue/100.0;
Serial.print("Moisture level: ");
Serial.print(sensorValue);
Serial.print(";");
if(sensorValue > 1000) {
lever = 0; // dry
}
if(sensorValue < 1000) {
lever = 1; // wet
}
switchOnOff(lever);
delay(1000);
}
// define the servo function
void switchOnOff(bool lever) {
Serial.print(" Lever: ");
Serial.print(lever);
Serial.print(";");
Serial.print(" Pump: ");
Serial.print(pump);
Serial.println(";");
if(lever == 0 && pump == 0) { // it is dry and pump is off, pump needs to be turned on
for (pos = 90; pos <= 180; pos++) { // goes from 0 degrees to 90 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
delay(4000);
for (pos = 180; pos >= 90; pos--) { // goes from 90 degrees to 0 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
pump = 1; // noting that pump is on
}
if(lever == 0 && pump == 1) { // it is dry and pump is on
return; // do nothing, return
}
if(lever == 1 && pump == 1) { // it is wet and pump is on, pump needs to be turned off
for (pos = 90; pos <= 180; pos++) { // goes from 90 degrees to 0 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
delay(4000);
for (pos = 180; pos >= 90; pos--) { // goes from 0 degrees to 90 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
pump = 0; // noting that pump is off
}
if(lever == 1 && pump == 0) { // it is wet and pump is off
return; // do nothing, return
}
}
谢谢。
问题在于您在正确声明之前尝试使用 servo()
函数。首先,您需要先声明一个函数,然后才能在您的代码中提及它。其次,您对 servo()
的定义不正确。 int servo(lever)
,其中 lever
不是类型。您应该查看以下内容:int servo(bool lever);
,其中 lever
是 servo()
函数采用的类型 bool
的参数。
然后,您的函数不会 return 任何东西,它只有一些 “副作用”,例如 myservo.write(pos);
。所以,应该是 void
.
试试下面的安排:
#include "Servo.h"
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
// Declare the function
void servo(bool);
int pos = 0; // variable to store the servo position
#define SensorPin A0
float sensorValue = 0;
bool lever = 0; // lever is global variable of type bool
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop() {
for (int i = 0; i <= 100; i++) {
sensorValue = sensorValue + analogRead(SensorPin);
delay(1);
}
sensorValue = sensorValue/100.0;
Serial.println(sensorValue);
delay(30);
if(analogRead(0 > 100)) {
lever = 1;
}
if(analogRead(0 < 100)) {
lever = 0;
}
servo(lever);
}
// Define the function
void servo(bool lever) {
if(lever == 1) { // On
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
}
}
if(lever == 0) { // Off
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
}
}
}
P.S。 setup()
在这里和那里重新定义也很混乱。