Go Down

Topic: problems with IF formula RESOLVED (Read 85 times) previous topic - next topic

jcd420

Aug 21, 2017, 11:27 pm Last Edit: Aug 21, 2017, 11:51 pm by jcd420
i get this error:

LDR_Simple.ino:18: error: 'LDRValue' cannot be used as a function

if (LDRValue()==1)Serial.println ("On");

             ^

exit status 1
'LDRValue' cannot be used as a function


my code:

int LDRPWR = 16;
int LDRPin = 5; // select the input pin for LDR
int LDRValue = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(LDRPWR, OUTPUT);
Serial.begin(9600); //sets serial port for communication
   digitalWrite(LDRPWR, LOW);
}
void loop() {
//LDRValue = analogRead(LDRPin); // read the value from the sensor
 digitalWrite(LDRPWR, HIGH);   // sets the LDR on
 delay(1000);                  // waits for a second
LDRValue = digitalRead(LDRPin); // read the value from the sensor
 digitalWrite(LDRPWR, LOW);    // sets the LDR off
//  delay(1000);                  // waits for a second

Serial.println(LDRValue); //prints the values coming from the sensor on the screen
if (LDRValue()==1)Serial.println ("On");
}

spycatcher2k

Quote
if (LDRValue()==1)Serial.println ("On");
Code: [Select]
if (LDRValue==1)Serial.println ("On");
-=-
http://www.uk-pcb.co.uk - UK Based PCB Fab & Assembly Company
Design & Assembly work undertaken - Arduino repairs & testing - PM for details. Arduino & C# PC programming.

PieterP

#2
Aug 21, 2017, 11:31 pm Last Edit: Aug 21, 2017, 11:32 pm by PieterP
By adding the parentheses to LDRValue, you are trying to evaluate it as a function. This is of course not possible, because it's a variable, not a function. Just delete the parentheses, and it should work.

Please remember to use [code][code] tags when posting code or error messages.

Pieter

MarkT

Its possible to evaluate a variable as a function of course, but only if its of function type - an int
typed variable cannot contain function references, only integers.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

jcd420


Go Up