Basic Boolean Question

Hi,

I'm trying to create a button that adds 5 to an int. Then, when it reaches a certain stage (255) I want to change it around so instead of adding 5 it subtracts 5. I have done this once before but for some reason I can't get it to work this time. Does anyone see my mistake?

 if (color1 >= 255) {
      button5 = true;
    }
    if (color1 <= 0){
      button5 = false;
    }
  if (arduino.digitalRead(5) == Arduino.HIGH){ //a real life button connected to processing with the Arduino

    if (button5 = false){
     color1 = color1 + 10;
    }
    else{
      color1 = color1 - 10;
    }
      println("button 5");
      println(button5);
  }
Tagged:

Answers

  • Answer ✓

    A single = is an assignment statement.

    For comparisons, use the == operator.

    However, you don't have to compare booleans like that. Booleans are already booleans.

    boolean x = true;
    if(x == true){
    

    Can just be:

    boolean x = true;
    if(x){
    
  • edited June 2014 Answer ✓

    We can also switch true & false states this way:

    // forum.processing.org/two/discussion/5965/
    // basic-boolean-question
    
    boolean bool = true;
    println(bool); // it's true.
    
    bool ^= true;  // 1st method.
    println(bool); // changed to false.
    
    bool = !bool;  // 2nd method.
    println(bool); // changed back to true.
    
    exit();
    
Sign In or Register to comment.