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!