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?

Arduino:

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

Processing:

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)
  background(255);

  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);
    delay(100);
  }
  if (lightSensorGreen>10) {
    r.display ();
    fill(0, 255, 0, col);
    delay(100);
  }
  if (lightSensorBlue>10) {
    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;
    }
  }
}

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() {
    smooth();
    noStroke();
    rectMode(CENTER);

    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!

ezgif.com-gif-maker (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: