0
votes

I try to get the value "lightVal" that's read from my photoresistor into the following equation:

theta = (260/3) log(23/1023)(1- lightval/1023), where theta is the steps taken by the motor. Then I need to get the servo motor to spin by theta degrees.

//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". 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); 
}
 } 
1
please post the code that you tried that gives you the error - bolov
it does not really make sense, but here it is - AKP
` { Serial.begin(9600); for (int i =0; i<=180; i=i+180){ angle = (260/3)log(23/1023,(1-(lightVal/1023); servo.write(angle); delay(5); } } ` - AKP
please edit the question to include the code, don't post the code in the comments - bolov
If you edit your code to be the code you're having trouble with, and not post the problem lines in comments or at the top of your question (devoid of surrounding context) we can pinpoint your error very quickly. - JohnFilleau

1 Answers

0
votes

Look at your lightVal variable. 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). 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. 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.