Arduino: RGB Led with PMW

Hello,

I really just started to learn C++ in Openframeworks and can’t figure out how to program a RGB led on arduino in Openframeworks. I want to make a smooth transitions with the light of the led so that’s why i’m working with Pmw but so far I can’t even turn on the led anymore.
I’m using pin 9,10 and 11 on the arduino Uno and have Firmate uploaded to my arduino.

Can please someone see what I’m doing wrong?

#include "ofApp.h"
#define PIN_RED 11
#define PIN_GREEN 10
#define PIN_BLUE 9

void ofApp::setup(){
    ofAddListener(arduino.EInitialized, this, &ofApp::setupArduino);
    arduino.connect("COM3");
    arduino.sendFirmwareVersionRequest();
}

void ofApp::update() {
    arduino.update();
    arduino.sendDigital(PIN_RED, 255);
    arduino.sendDigital(PIN_GREEN, 255);
    arduino.sendDigital(PIN_BLUE, 0);
}

void ofApp::setupArduino(const int& version) {
    ofRemoveListener(arduino.EInitialized, this, &ofApp::setupArduino);

    arduino.sendDigitalPinMode(PIN_RED, ARD_PWM);
    arduino.sendDigitalPinMode(PIN_GREEN, ARD_PWM);
    arduino.sendDigitalPinMode(PIN_BLUE, ARD_PWM);

    ofAddListener(arduino.EDigitalPinChanged, this, &ofApp::digitalPinChanged);
    ofAddListener(arduino.EAnalogPinChanged, this, &ofApp::analogPinChanged);

}

Hello unlimitedKoala,

About your code: arduino.connect seems to be missing second parameter (baud rate) which very probably is vital for connection (strange that the compiler does not complain, but anyway). Compare code of the example I noted below:

// replace the string below with the serial port for your Arduino board
// you can get this from the Arduino application or via command line
// for OSX, in your terminal type "ls /dev/tty.*" to get a list of serial devices
ard.connect("/dev/cu.usbmodemfa131", 57600);

// listen for EInitialized notification. this indicates that
// the arduino is ready to receive commands and it is safe to
// call setupArduino()
ofAddListener(ard.EInitialized, this, &ofApp::setupArduino);

(of course you would not use “/dev/cu.usbmodemfa131” but “COM3”). The order is also different (first thing is to connect) although I don’t think this should be a problem.

You are calling the functions digitalPinChanged and analogPinChanged by setting a listener in setupArduino(). But I do not see the proper functions in your code?

Also in the example i list below there is a smart way of waiting until arduino is ready before sending the first commands (flag “bSetupArduino”). So they can not interfere.

I’d advise to go the other way round. First try the example in: openFrameworks/examples/communication/firmataExample/
If it works you can adjust it to your needs.

Hope this helps!
oe

Just as a heads-up: the second parameter (baud rate) in ofArduino::connect() is actually optional - it defaults to 57600 (so that’s why there’s no compile-error)

Update:

Thank you dasoe and edovino, it has indeed nothing to do with the baud rate.

I still can’t figure out what is wrong with this code but I rewrote the code in an other project and it works. I can’t find any differences with this project and if I open this project it still doesn’t work but also does not give any errors.
It boggles my mind, but the problem seems to be over.

Thanks for the help!