Photo transistors not working

I am trying to get my Arduino photo transistors to work with my code in Processing. I can’t get them to work. Earlier I got it to work but without a class, which I have added now. The computer tells me that it should work and at this point I have tried many different things. But for some reason the photo transistors won’t work. I have inserted the code below - any suggestions to fix it?


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

void setup() {
  // start serial port at 9600 bps:

  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 =;
    // read first analog input, divide by 4 to make the range 0-255:
    lightSensor1 = analogRead(A0) / 4;
    lightSensor2 = analogRead(A1) / 4;
    lightSensor3 = analogRead(A2) / 4;
    // delay 10ms to let the ADC recover:

    // send sensor values:

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


import processing.serial.*;

float 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)

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

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

void draw() {
  if (lightSensorRed>10) {
    r.display ();
    fill(255, 0, 0, col);
  if (lightSensorGreen>10) {
    r.display ();
    fill(0, 255, 0, col);
  if (lightSensorBlue>10) {
    r.display ();
    fill(0, 0, 255, col);

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

  if (firstContact == false) {
    if (inByte == 'A') {
      firstContact = true;
  } else {

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

    // 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:
      // Reset serialCount:
      serialCount = 0;

Processing class:

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() {

    rect(rectY, rectX, rectY2, rectX2);

Thank you in advance!

We kindly ask you to please format your code. This will make it easier for other community members to help you. If you need help you can check the FAQ section on code formatting. Thanks! (1)

Hi @Fritjof, Thanks for reformatting your code, that makes it very easy to try. I have it running in Ard+Processing. I don’t have the phototransistors, but getting whatever values happen to be there, 4,3,0. The comms is working fine, Processing is printing out the received values. I changed value in the 3 lines e.g. 'if (lightSensorRed>10) ’ to 1 to match my received values. Now I get a colour changing rect on screen. Not sure what it’s supposed to do, but it’s doing something.

Another way of looking at it. You say it was working, until you added the class, so it’s not a problem with the Ard or the phototransistors. Suggest you go back to the working version and find an incremental way to transform a copy of that to the class version.

1 Like

Good idea! I’m gonna try that :slight_smile: I also have a suspicion that some of my arduino parts might not be working, and I Think you just confirmed that, so thank you :smiley: