I'm attempting to create a data exchange between sensors on an Ardunio (well actually a AtMega328PU based clone built on a breadboard, but I don't believe that that's the source of my problem) and a Processing script, and I'm getting some unexpected results. I was following the method of serial communication detailed here, but I get stuck with what appears to be no data actually traveling over the serial connection.
The Arduino code:
int buttonPin = 9, photodiodePin = A0;
char dataToSend;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, OUTPUT);
pinMode(photodiodePin, INPUT);
}
void loop() {
if (Serial.available() > 0)
{
dataToSend = Serial.read();
if (dataToSend == 'B')
{
Serial.println(digitalRead(buttonPin));
}
else if (dataToSend == 'L')
{
Serial.println(analogRead(photodiodePin));
}
}
}
And the relevant parts of the Processing code:
import processing.serial.*;
Serial myPort;
void setup()
{
size(1600,900);
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
}
void draw()
{
if(getButtonData() == 1)
{
//Do Stuff
}
}
int getLightData()
{
myPort.write('L');
println("L");
while(!(myPort.available() > 0)){
}
int lightValue = int(myPort.readStringUntil('\n'));
return lightValue;
}
int getButtonData()
{
myPort.write('B');
println("B");
while(!(myPort.available() > 0)){
println("stuck in here");
delay(500);
}
int buttonValue = int(myPort.readStringUntil('\n'));
return buttonValue;
}
And in Processing I get an output of:
B
stuck in here
stuck in here
stuck in here
stuck in here
...
Note: I know I have the correct port selected from the list, so that is not the issue.
I've tried debugging the issue and searching the internet for similar problems, both to no avail. Any help on this issue is greatly appreciated, Thanks!