# Cannot convert int to boolean

**URL:** <https://discourse.processing.org/t/cannot-convert-int-to-boolean/40540>\
**Category:** Electronics (Arduino, etc.)\
**Tags:** homework\
**Created:** [January 10, 2023, 3:40pm UTC](https://discourse.processing.org/t/cannot-convert-int-to-boolean/40540 "2023-01-10T15:40:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fritjof](https://avatars.discourse-cdn.com/v4/letter/f/9d8465/32.png) [@Fritjof](https://discourse.processing.org/u/Fritjof)\
**Post date:** [January 10, 2023, 3:40pm UTC](https://discourse.processing.org/t/cannot-convert-int-to-boolean/40540/1 "2023-01-10T15:40:15Z")

</div>

I keep getting the error message: cannot convert int to boolean. Any ideas how to fix it?

Processing code with error message:

```auto
import processing.serial.*;

boolean lightSensorRed, lightSensorGreen, lightSensorBlue;
float col;

Serial myPort;
int[] serialInArray = new int[4]; // Where we'll put what we receive
int serialCount = 0;
boolean firstContact = false;

Rectangle r;

void setup() {

  fullScreen(); //size(500, 500)
  background(255);

  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);

  r = new Rectangle ();
  col = random(50, 200);
}

void draw() {
  if (lightSensorRed == true) {
    r.display ();
    fill(255, 0, 0, col);
    delay(100);
  }
  if (lightSensorGreen == true) {
    r.display ();
    fill(0, 255, 0, col);
    delay(100);
  }
  if (lightSensorBlue == true) {
    r.display ();
    fill(0, 0, 255, col);
    delay(100);
  }
}

void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte = myPort.read();

  if (firstContact == false) {
    if (inByte == 'A') {
      myPort.clear();
      firstContact = true;
      myPort.write('A');
    }
  } else {

    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;

    // If we have 2 bytes:
    if (serialCount > 2 ) {
      lightSensorBlue = serialInArray[0];
      lightSensorRed = serialInArray[1];
      lightSensorGreen = serialInArray[2];

      // print the values (for debugging purposes only):
      println("lightSensorRed: " + lightSensorRed + "\t" +"lightSensorGreen: "+lightSensorGreen + "\t" +"lightSensorBlue: "+lightSensorBlue);

      // Send a capital b to request new sensor readings:
      myPort.write('b');
      // Reset serialCount:
      serialCount = 0;
    }
  }
}

```

Class rectangle:

```auto
class Rectangle {
  float rectY;
  float rectX;
  float rectY2;
  float rectX2;

  Rectangle() {
    rectY = random(width);
    rectX = random(height);
    rectY2 = random(width);
    rectX2 = random(height);
  }

  void display() {
    smooth();
    noStroke();
    rectMode(CENTER);

    rect(rectY, rectX, rectY2, rectX2);
  }
}

```

What I have as my Arduino code:

```auto
boolean lightSensor1 = 0; // first analog sensor
boolean lightSensor2 = 0; // second analog sensor
boolean lightSensor3 = 0; // third analog sensor
boolean inByte = 0; // incoming serial byte

void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);

  establishContact(); // send a byte to establish contact until receiver responds
}

void loop() {
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    // read first analog input, divide by 4 to make the range 0-255:
    lightSensor1 = analogRead(A0) / 4;
    delay(10);
    lightSensor2 = analogRead(A1) / 4;
    delay(10);
    lightSensor3 = analogRead(A2) / 4;
    // delay 10ms to let the ADC recover:
    delay(10);

    // send sensor values:
    Serial.write(lightSensor1);
    Serial.write(lightSensor2);
    Serial.write(lightSensor3);
  }
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A'); // send a capital A
    delay(300);
  }
}

```

---

<div class="post-metadata">

**Author:** ![FSXAC](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fsxac/32/3995_2.png) [@FSXAC](https://discourse.processing.org/u/FSXAC)\
**Post date:** [January 10, 2023, 4:43pm UTC](https://discourse.processing.org/t/cannot-convert-int-to-boolean/40540/2 "2023-01-10T16:43:39Z")

</div>

I would “wrap” the integer in a comparison:

```auto
lightSensorBlue = (serialInArray[0] != 0);

```

---

<div class="post-metadata">

**Author:** ![Fritjof](https://avatars.discourse-cdn.com/v4/letter/f/9d8465/32.png) [@Fritjof](https://discourse.processing.org/u/Fritjof)\
**Post date:** [January 10, 2023, 6:34pm UTC](https://discourse.processing.org/t/cannot-convert-int-to-boolean/40540/3 "2023-01-10T18:34:45Z")

</div>

Now the code works, but it’s not creating rectangles when I run the program…?
