Arduino伺服电机

时间:2020-03-22 18:45:53

标签: c++ arduino equation adafruit servo

我尝试获取从光敏电阻读取的值“ lightVal”

theta =(260/3)log(23/1023)(1- lightval / 1023),其中theta是 电机采取的步骤。然后我需要让伺服电机旋转 通过θ度。

//locate pins
int PhotoresistorPin = A0;

//Declare global variables
int lightVal;

void setup() {
  //Set photoresistor as input 
  pinMode(PhotoresistorPin, INPUT);
  //serial is used to communicate with the board.
  //Serial.begin() sets data rate in bits per second
  Serial.begin(9600);
}

void loop() {
  //read input from photoresistor
  //analogueRead function reads the voltage across the photoresistor
  lightVal = analogRead(PhotoresistorPin);
  //print input from photoresistor
  Serial.println(lightVal);
  delay(1000);

}

我被困在这里,现在该怎么办?基本上,每次我尝试写方程式时,都会告诉我“ theta未在此范围内声明”。谢谢!

编辑: 真的没有意义,但是在这里

{ Serial.begin(9600); 
for (int i =0; i<=180; i=i+180)
{ float angle = (260/3)log(23/1023,(1-(lightVal/1023); 
servo.write(angle); 
delay(5); 
}
 } 

1 个答案:

答案 0 :(得分:0)

查看您的lightVal变量。请注意,它分为两个部分。第一部分:

int lightVal;

定义变量。此行告诉编译器存在一个名为lightVal的变量,以及变量的类型(在这种情况下为int)。当您获得的变量“未在此范围内声明”时,通常意味着您尚未执行此步骤,或者已在其他范围内完成了该步骤。

第二部分是您在此行中为变量赋予值的地方:

lightVal = analogRead(PhotoresistorPin);

这就是您说的lightVal应该相等的地方。您从模拟传感器获取一个数字并将该值分配给变量。

有时您可以在同一行上同时执行两个步骤,但是告诉编译器这样一个变量存在的第一步很重要。在告诉编译器它存在之前,您不能尝试对它做某事。

相关问题