Arduino serial reading from oF

I would like to create a program that changes the brightness of the LED by entering a number from 0 to 9 on the keyboard.

When I press the keyboard, something goes to Arduino. (Aduino’s lights flash)
But I don’t think Arduino gets the number I type and LED is not responding.

I’m frustrated because I don’t know what the problem is…

here is arduino code

char message;
int ledPin = 9;

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

void loop() {
  
  
  if(Serial.available()){
    message = Serial.read();

    if (message=='0')analogWrite(ledPin,0);
    if (message=='1')analogWrite(ledPin,20);
    if (message=='2')analogWrite(ledPin,40);
    if (message=='3')analogWrite(ledPin,60);
    if (message=='4')analogWrite(ledPin,80);
    if (message=='5')analogWrite(ledPin,100);
    if (message=='6')analogWrite(ledPin,120);
    if (message=='7')analogWrite(ledPin,140);
    if (message=='8')analogWrite(ledPin,160);
    if (message==1)analogWrite(ledPin,180);
   
  } 
}

here is main.cpp

#include "ofMain.h"
#include "ofApp.h"

//========================================================================
int main() {
	ofSetupOpenGL(500, 500, OF_WINDOW);			// <-------- setup the GL context

	// this kicks off the running of my app
	// can be OF_WINDOW or OF_FULLSCREEN
	// pass in width and height too:
	ofRunApp(new ofApp());

}

ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup() {
	serial.setup("COM3", 19200);

}

//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
	serial.writeByte((char)key);
}

ofApp.h

#ifndef _OF_SEND_SERIAL
#define _OF_SEND_SERIAL
#include "ofMain.h"

class ofApp : public ofBaseApp {

public:
	void setup();

	void keyPressed(int key);
	ofSerial serial;

};
#endif

I look forward to your help.

Hi, you should set the baudrate to 9600 in OF :slight_smile:

Oh, my God. You helped me solve the problem in three seconds that I couldn’t solve in two nights. Thank you very much. You are the best.

1 Like