Bool functions?

Hi, I'm trying to set up a boolean function to check whether something is true, but the environment (Tinkercad) won't accept it. Is anything missing? Thanks in advance.

The sensor value error is fixed


int light = 0;
int camera = 0;
int action = 0;

bool checkLight(light){
  
  if (light <= 100) {
    return true; 
  }
  else {
    return false;
  }
  
}

void setup()
{
  pinMode(A0, INPUT);
  Serial.begin(9600);
}

void loop()
{
  light = analogRead(A0);
  Serial.print("Light: ");
  Serial.println(light);
}

Hello pickanapple

try

if (light <=100) 

1 Like

Thank you, this didn't fix it but would have caused problems later probably (I don't know why it wasn't flagged!) Maybe you can tell that I am used to python?...

Post the sketch to see how we can help.

1 Like

Although the screenshot is useful for those not familiar with Tinkercad, it's terrible as a way of presenting the code and the errors - please also post the code in the normal manner, as described in the 'How to get the best out of this forum' post; in particular, under ' Posting code and common code problems'

One of the key difficulties with images is the we can't copy & paste from them.

This if has the opening parenthesis in the wrong place:
image

It needs to be

if( light <= 100 ) {
1 Like

Posted

It's posted

Unrelated to the problem, but note that (light<=100) is itself a boolean expression - so you could just write:

bool checkLight( light )
{
   return light <= 100;
}
1 Like

Thank you

Please don't edit your post once its already been commented on - it makes a nonsense of the thread for people trying to follow it subsequently.

You've lost all the indentation from your code - please re-post in a reply.

Thanks.

int light = 0;
int camera = 0;
int action = 0;

bool checkLight(light){
  
  light <= 100;
  
}

void setup()
{
  pinMode(A0, INPUT);
  Serial.begin(9600);
}

void loop()
{
  light = analogRead(A0);
  Serial.print("Light: ");
  Serial.println(light);
}

Thanks.

So what does Tinkercad say to that now?

What do you get if you try to compile that in the Arduino IDE?

OK I see you did. THX.

And use the <CODE/> button in the message composition window tool bar please, so it looks like code

void setup() {

}

a7

1 Like

See the difference to @awneil's #8.

bool checkLight(light){
  
  light <= 100;
  
}

doesn't do a thing for you.

a7

1 Like

It didn't like it, IDE output:

Thanks, I changed it but the error still exists

Post the code. Post the error messages like they were code, not as screenshots.

It looks like you now have two copies of the function, or are using the function incorrectly or you have another error yet.

a7

1 Like
int light = 0;
int camera = 0;
int action = 0;

bool checkLight(light){
  
  light <= 100;
  
}

void setup()
{
  pinMode(A0, INPUT);
  Serial.begin(9600);
}

void loop()
{
  light = analogRead(A0);
  Serial.print("Light: ");
  Serial.println(light);
}
Error messages: error: redefinition of 'bool checkLight'
 bool checkLight(light){
                      ^
 note: 'bool checkLight' previously declared here
 bool checkLight(light){
      ^~~~~~~~~~

exit status 1

Compilation error: redefinition of 'bool checkLight'

Again, please post the errors as text - in the same way as for code.
Copy & Paste.

The errors say that you have duplicate definitions of checkLight - so there must be some more code that you're not showing us.

We can only work from what you show.

I just copied the whole thing from the IDE