Go Down

Topic: Splitting an string coming from Serial (Read 1 time) previous topic - next topic


That's not what you posted first time.

I made a mistake writting it because I am working with 2 different computers (one for the forum the other for arduino) and I can't copy the code, so I have to write it myself.

What happens if after reading the first character, there is no second character available yet?

I understand what you mean but I don´t know how to fix it.


I´ve found the real problem now. arduino.write() can only send integers. So if I am sending 'b', the arduino Serial.read() recives 98 which is the ASCII number of 'b'. So I made this code:
Code: [Select]

void loop () {
    while (serial.available() >0) {
      message = Serial.read();
      if (message>= 0 && message <=250) {
          analogWrite(pinBlue, message);// This one works perfectly
      } else if (message>250 && message<502) {
          analogwrite (pinRed,message-250);//This one doesn't work. It lights the blue LED again....

In processing I am sending 0-250 for led blue; 251-501 for led red.


I´ve found the real problem now. arduino.write() can only send integers.

Wrong. arduino.write() can only send bytes. When you determine the difference between a byte and an integer, you'll know exactly what your current problem is, and why you need a completely new approach.


Thank you very much PaulS for your comment. It was just understand the difference between integer and byte. The project is working perfectly.
I leave you the code here for if anyone need help anytime.

Code: [Select]
void draw() {
  int value = int((fader[numfader]*1000)/16);
  switch (numfader) {
    case 1 :
    case 2 :
    case 3 :

Arduino code:
Code: [Select]
//Setting the PWM outputs
int pinBlue = 11;
int pinRed = 10;
int pinYellow = 9;
//Incomming values
int ledBlueval=0;
int ledRedval=0;
int ledYellowval=0;
//Var to hold the incomming data
byte message;

void setup () {

void loop() {
  while(Serial.available() > 0) {
    if (message>=0 && message <=62) {
      ledBlueval = map (message,0,62,0,255);
      analogWrite (pinBlue,ledBlueval);
    } else if (message>=63 && message <=125) {
        ledRedval = map (message,63,125,0,255);
        analogWrite (pinRed,ledRedval);
    } else if (message>125 && message <189) {
        ledYellowval = map (message,126,188,0,255);
        analogWrite (pinYellow,ledYellowval);

Thanks to everyone.

I am so happy   XD


Wow, I am happy for you, too.

Of course you realise that you have improved the communication so a single byte contains both the LED code and the value ... at the cost of only having 64 distinct values for the brightness. If that is sufficient then it is a great solution!

I am slightly baffled why my suggestion didnt work. Apart from the int((fader[n)umfader]*1000)/4); thing that should have worked. Unless there where other differences between your post and the code. :smiley-eek:

I also notice that sometimes you use arduinoPort.write() and sometimes arduino.write(). I suspect some confusion arises from that. I have not tried this processing interface to arduino so I done know what type they take and we're getting different casting/conversion of the types (which would explain a lot, too)

Well, seems I have to get a copy/install of the Processing extension to talk to the Arduino. (I use Processing for some standalone fun, and have done my own Delphi/Pascal based communication stuff for other projects)

Go Up