Pages: 1 [2]   Go Down
Author Topic: Splitting an string coming from Serial  (Read 1672 times)
0 Members and 1 Guest are viewing this topic.
Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1211
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahh.. your mixing up sending it as an ASCII and sending a binary byte.

The last attempt the Write(Value) sends a single byte with something between 0 and 255. This you pick up in the Arduino with the Read, and that fits nicely directly into the analogWrite.

In your earlier Processing you wrote arduinoPort.write("v"+value); - the Processing makes this into a string of several bytes, writing the value in decimal-ASCII like "A23" (which is what you wanted, but so far have been unable to decode at the receiving end)

I suggest you decide on a two byte format. Your Processing does two Arduinowrite; "A", "B" ... for which LED and then, the last bit that worked fine, Write(value).

Your Arduino does two (2) Serial.Read (each waiting for available() ). The first gets the letter A, B ... the second the value.
Then your code decides which LED and sends the value.

There is the potential problem that if you miss a byte somewhere the code will be out of synch (ie reading the wrong byte into the variables) at which point you have to press the reset button so it starts properly again. There are ways around that (using  a defined synch/terminator character) but maybe much later.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
Arduino as a way of life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi MSquare, your idea sounds good, but I tried and it doesn´t work (maybe because I don´t know how to do it)

My processing code is:
Code:
void draw () {
   int value = int((fader[n)umfader]*1000)/4); // this is to get the value for each slider and convert it from float to int.
   switch (numfader) {
       case 1 : // slider blue
             arduino.write('b');
             arduino.write(value);
             break;
       case 2 :  //slider red
             arduino.write('v');
             arduino.write(value);
             break;
       case 3 : //slider yellow
             arduino.write('g');
             arduino.write(value);
             break;
      }
}

And on arduino I have:
Code:
int messageNumber = 0;
int messageValue =0;
int ledNumber;
void setup () {
     Serial.begin (9600);
}
void loop () {
    if (Serial.available() > 0) {
        messageNumber = Serial.read();
          switch (messageNumber) {
               case 'b' :
                    ledNumber = 11;
                    break;
               case 'v' :
                    ledNumber = 10;
                    break;
               case 'v' :
                    ledNumber = 9;
                    break; 
        }
    }
    if (Serial.available() > 0) {
        messageValue = Serial.read();
        analogWrite(ledNumber,messageValue);
   }
}

When the program starts all the LEDs are off. Then when I press the first slider the  correct led lights but it doesn't do the fading. And it happens the same with the other LEDs.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int((fader[n)umfader]*1000)/4);
?

Take a look at your "loop" code.
What happens if after reading the first character, there is no second character available yet?
« Last Edit: February 12, 2011, 05:28:45 am by Groove » Logged

Per Arduino ad Astra

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
Arduino as a way of life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

int value = int ((fader[numfader]*1000)/4);

I've got the values inside a float array. But I can't send floats to arduino.write().I have to multiply per 1000 because I don't want to lose the values (the float goes from 0 to 1) and then divide the result by 4 to get a value between 0 and 250. And after this I convert the float to an integer.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
int value = int ((fader[numfader]*1000)/4);
That's not what you posted first time.
Logged

Per Arduino ad Astra

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
Arduino as a way of life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
Arduino as a way of life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
Arduino as a way of life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Processing:
Code:
void draw() {
  int value = int((fader[numfader]*1000)/16);
  switch (numfader) {
    case 1 :
         arduinoPort.write(value);
         break;
    case 2 :
         arduinoPort.write(63+value);
         break;
    case 3 :
         arduinoPort.write(126+value);
         break;
  }         
}

Arduino code:
Code:
//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 () {
  Serial.begin(9600);
}

void loop() {
 
  while(Serial.available() > 0) {
    message=Serial.read();
    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   smiley-lol

Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1211
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Pages: 1 [2]   Go Up
Jump to: