1
votes

I have been working through the Arduino starter kit examples and recently, I hit in issue in one where a potentiometer is used to change the color of a logo on the computer screen. When the logo initially appears, it is at the correct color as set by the potentiometer, but then the color does not change as I move the potentiometer.

I tried just outputting the values of the potentiometer to the serial monitor and they change properly, but the values as read by the Processing code do not change when output to the serial monitor.

Here is the Arduino code:

void setup() {
  // initialize serial communication
  Serial.begin(9600);
}

void loop() {
  // read the value of A0, divide by 4 and 
  // send it as a byte over the serial connection
  Serial.write(analogRead(A0)/4);
  delay(1);
}

And here is the Processing code:

 // import the serial library
 import processing.serial.*;

 // create an instance of the serial library
 Serial myPort;

 // create an instance of PImage
 PImage logo;

 // a variable to hold the background color
 int bgcolor = 0;

 void setup() {
 // set the color mode to Hue/Saturation/Brightness
 colorMode(HSB, 255);

 // load the Arduino logo into the PImage instance
 logo = loadImage("http://arduino.cc/en/pub/skins/arduinoWide/img/logo.png");

 // make the window the same size as the image
 size(logo.width, logo.height);

 // print a list of available serial ports to the 
 // Processing staus window
 println("Available serial ports:");
 println(Serial.list());

 // Tell the serial object the information it needs to communicate
 // with the Arduno. Change Serial.list()[0] to the correct 
 // port corresponding to your Arduino board.  The last
 // parameter (e.g. 9600) is the speed of the communication.  It
 // has to correspond to the value passed to Serial.begin() in your
 // Arduino sketch.  
 myPort = new Serial(this, Serial.list()[0], 9600);

 // If you know the name of the port used by the Arduino board, you
 // can specify it directly like this.
 // port = new Serial(this, "COM1", 9600);  

 }

 void draw() {
 background(255);
 // if there is information in the serial port
 if ( myPort.available() > 0) {
 // read the value and store it in a variable
 bgcolor = myPort.read();

 // print the value to the status window
 println(bgcolor); 
 }

 // Draw the background. the variable bgcolor
 // contains the Hue, determined by the value
 // from the serial port
 background(bgcolor, 255, 255);

 // draw the Arduino logo
 image(logo, 0, 0);
 }

So I think the issue lies within either the Serial.write or Serial.read methods, but it could be something entirely different.

2
could you format your code to be a little more readable using tabs?ladislas

2 Answers

1
votes

I had the same issue; I just increased the delay to 50 in same original Arduino code (i.e no SerialEvent or parseInt, just set delay(50) ). This solved the issue.

It seems depending on your PC, processing can be slower in reading/parsing the serial buffer, so tell Arduino to slow down!

0
votes

I think you need something called serialEvent in your processing code.

It will look like that:

void serialEvent(Serial myPort) {
   String inString = myPort.readStringUntil('\n');

   //do something with your string.
}

Hope it helps!