简体   繁体   English

伺服电机,Arduino

[英]Servo Motor, Arduino

I try to get the value "lightVal" that's read from my photoresistor into the following equation:我尝试将从我的光敏电阻读取的值“lightVal”转换为以下等式:

theta = (260/3) log(23/1023)(1- lightval/1023), where theta is the steps taken by the motor. theta = (260/3) log(23/1023)(1- lightval/1023),其中 theta 是电机采取的步数。 Then I need to get the servo motor to spin by theta degrees.然后我需要让伺服电机旋转 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);

}

I got stuck here, what do I do now?我卡在这里了,我现在该怎么办? Basically, each time I try to write the equation, it tells me that "theta was not declared in this scope".基本上,每次我尝试编写等式时,它都会告诉我“未在此范围内声明 theta”。 Thanks!谢谢!

Edit: it does not really make sense, but here it is编辑:它没有意义,但在这里

{ 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); 
}
 } 

Look at your lightVal variable.看看你的 lightVal 变量。 Notice that it comes in two parts.请注意,它分为两部分。 There is the first part:有第一部分:

int lightVal;

that defines the variable.定义变量。 This line tells the compiler that a variable called lightVal exists and what type of variable it is (an int in this case).这一行告诉编译器存在一个名为 lightVal 的变量以及它是什么类型的变量(在本例中为 int)。 When you get a variable is "not declared in this scope" then it generally means that you haven't taken this step or that you've done it in a different scope.当您得到一个变量“未在此范围内声明”时,通常意味着您尚未执行此步骤或已在不同的范围内完成。

The second part is where you gave your variable a value on this line:第二部分是您在此行上为变量赋值的地方:

lightVal = analogRead(PhotoresistorPin);

That's where you're saying what lightVal should equal.这就是你所说的 lightVal 应该等于什么。 You're getting a number from an analog sensor and assigning that value to your variable.您从模拟传感器获取一个数字并将该值分配给您的变量。

You can do both steps on the same line sometimes, but that first step of telling the compiler that such a variable exists is important.有时您可以在同一行上执行这两个步骤,但告诉编译器存在此类变量的第一步很重要。 You can't try to do something with a variable before you tell the compiler that it exists.在告诉编译器它存在之前,您不能尝试对变量执行某些操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM