Let's Make Robots!

Trouble with arduino/processing

I've been trying to teach myself Arduino and Processing, and I've been hitting a few problems. I had a previous post Here, but it was getting a bit crowded with code, so I thought I'd start a new post.

I've been trying to send an integer value from Processing to Arduino via serial, to set the brightness of the on-board LED. The arduino sketch works, and I've tested it with the serial monitor. When I try to control it with my Processing sketch, though, it only adjusts between 47-60 rather than the full range of 0-100. I'm using the ControlP5 and serial libraries in processing, and the softPWM library on the arduino to PWM the onboard LED.

Code:

Arduino;

#include <SoftPWM.h>
#include <SoftPWM_timer.h>
int LED = 13;
int dim = 0;
void setup() {
  Serial.begin(2400);
    SoftPWMBegin();
  SoftPWMSet(LED,0);
}
void loop() {
  while (Serial.available() > 0) {
    int dim = Serial.parseInt();
    if (Serial.read() == '\n') {
      SoftPWMSetPercent(LED,dim);
    }
  }
}

Processing (The reason the value is called servo is because I'd intended to control a servo, but didn't get round to it);

import processing.serial.*;
Serial myPort;
import controlP5.*;
ControlP5 cp5;
int Servo = 0;
int out = 0;
Knob controller;
void setup() {
  size (200,200);
  myPort = new Serial(this, "COM6", 2400);
  smooth();
  noStroke();
  cp5 = new ControlP5(this);
  controller = cp5.addKnob("Servo")
               .setRange(47,60)
               .setValue(0)
               .setPosition(50,50)
               .setRadius(50)
               .setDragDirection(Knob.HORIZONTAL)
               ;
}

void draw() {
  background(5);
    myPort.write(Servo);
    delay(15);
    myPort.write('\n');
}

Any Ideas?

PS; can the arduino provide MIDI over USB?

 

Thanks in advance.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bdk6's picture

After digging into the (horrible) processing documentation for 5 minutes or so, it seems that your line myPort.write(Servo); is actually sending the character whose ASCII (?) code is the number servo, not a string of digits representing that number.  The Arduino, however, is expecting a string of digits to assemble into an integer (parseint).  If you convert the number Servo to a string of digits then pass that to .write( string );  that should work.  I don't use processing so I don't know how to tell you to do that.

EDIT:  It appears the str() function will do what you need:  http://processing.org/reference/strconvert_.html

something like this myPort.write(str(Servo));  

Thanks! Just tried this, and it's worked perfectly. I'm getting smooth control from 0 to 100 from the sketch.

bdk6's picture

Have fun!

Chris the Carpenter's picture

I would have to say the 47,60 in the range variable is something to look at, but it should be noted that you are sending an INT via serial and it does not look like you are taking it apart (into bytes) first. If you are only using a 0-100 range (or even a 0-255 range), I would use a byte for your "servo" variable instead.

Thans for the reply. I've given using byte instead of int a shot, but had no luck. The 47,60 range variable was origeonally 0,100, but I changed it to see how smooth it was within those values. It's just the range for the knob. I've also tried changing the port settings (parity etc.), but it didn't help, so I set it back. Do you have any idea how to monitor serial activity? I was just wondering if it's possible to use a bit of software to compare what the arduino serial terminal and the processing sketch were sending, so I could have a better guess as to what's wrong.

bdk6's picture

I try to stay well away from anything that speaks Java (like Processing), but the statement "it only adjusts between 47-60 " certainly draws suspicion to this line of code:

.setRange(47,60)

 

 

Thanks for the reply. I forgot to re-adjust that, as it was origeonally (0,100). That sets the range for the on-screen control. I just changed it so that I could see how precise it was within that range.