Cannot convert int to boolean

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

Processing code with error message:

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:

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:

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);
  }
}

I would “wrap” the integer in a comparison:

lightSensorBlue = (serialInArray[0] != 0);
1 Like

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