Go Down

Topic: Send a value from processing to arduino (Read 555 times) previous topic - next topic


I want to send a value between 0 and 255 from processing to my arduino, I want to use this value to controll the brightness of a LED.
In processing I used the "serial.write" command to send the value.
In the arduino I used the "Serial.read" command to read the value.
Is this the right commands? Becuase I'dont get it to work.


Becuase I'dont get it to work.

Without any code, I wouldn't expect it to work. (Hint: Post your code)


Jun 04, 2012, 05:25 pm Last Edit: Jun 04, 2012, 05:27 pm by himym Reason: 1
This is the Processing code

Code: [Select]

import processing.serial.*;
Serial port;

int yValue = 318;
int midValue = 0;
int sendValue = 0;

void setup() {
 size(600, 400);
 port = new Serial(this, "COM6", 9600);

void draw() {

 if (mousePressed && mouseX >= 102 && mouseX <= 158 && mouseY >= 82 && mouseY <= 318) {
   yValue = mouseY;
 //the int yValue gets the value of mouseY if the mouse is being pressed inside of the right area.


 ellipse(500, 350, 40, 40);
 ellipse(535, 350, 40, 40);
 ellipse(500, 350, 28, 28);
 ellipse(535, 350, 28, 28);
 rect(495, 348, 505, 352);
 rect(530, 348, 540, 352);
 rect(533, 345, 537, 355);
 //Arduino logotype

 rect(100, 80, 160, 320);
 //First rect(the black one)
 rect(101, 81, 159, 319);
 //Second rect(white)
 rect(102, yValue, 158, 318);
 //the red rect, you can change the size with the mouse
 rect(102, 82, 158, yValue);

 midValue = (600 - yValue - 282) * 255 / 236;
 //instead of trying to map the yValue to a value between 0 and 255 you do a calculation
 text(midValue, 180, 85);

 //send the midValue to the arduino

This is the arduino code
Code: [Select]

int ledPin = 9;
int dimmerValue = 0;

void setup(){
 pinMode(ledPin, OUTPUT);

void loop(){
 dimmerValue = Serial.read();
 analogWrite(ledPin, dimmerValue);



Add this to the processing sketch. It will show the value of midvalue in the text window at the bottom of the processing IDE. It will help you look for the problem.

Code: [Select]
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com


Jun 04, 2012, 05:42 pm Last Edit: Jun 04, 2012, 05:52 pm by dxw00d Reason: 1
Also, in your Arduino sketch, loop() runs very fast, and you are reading a value from the serial input every time through. Almost every time through, there will be nothing to read, and dimmerValue will contain the value -1, which you are then passing to analogWrite(). I haven't read the analogWrite() source, so I don't know what it does with a negative value. Try using Serial.available() to make sure there is something to read, before you read it.

Edit: It seems the source just passes the negative value on to the timer.


Now it's working :) thanx for the replys!


And if you give the "analogWrite" a -1, the Led is HIGH all the time.

Go Up