Pages: [1]   Go Down
Author Topic: Send a value from processing to arduino  (Read 310 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello.
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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3441
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the Processing code

Code:
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.


  background(#18674B);
  noStroke();
  smooth();

  fill(250);
  ellipse(500, 350, 40, 40);
  ellipse(535, 350, 40, 40);
  fill(#18674B);
  ellipse(500, 350, 28, 28);
  ellipse(535, 350, 28, 28);
  fill(250);
  rectMode(CORNERS);
  rect(495, 348, 505, 352);
  rect(530, 348, 540, 352);
  rect(533, 345, 537, 355);
  //Arduino logotype

  fill(0);
  rectMode(CORNERS);
  rect(100, 80, 160, 320);
  //First rect(the black one)
  fill(250);
  rect(101, 81, 159, 319);
  //Second rect(white)
  fill(#D81F25);
  rect(102, yValue, 158, 318);
  //the red rect, you can change the size with the mouse
  fill(250);
  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);

  port.write(midValue);
  //send the midValue to the arduino
}

This is the arduino code
Code:
int ledPin = 9;
int dimmerValue = 0;

void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

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



}
« Last Edit: June 04, 2012, 10:27:26 am by himym » Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1330
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 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:
println(midValue);
Logged


Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 04, 2012, 10:52:01 am by dxw00d » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now it's working smiley thanx for the replys!
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: