Recieve serial bytes and if a particular char is recieved jump to function..?

Hi lovely peeple. :P In my quest to send slider data over bluetooth to Arduino (mega2560) I have partly succeeded!!

Basically I am sending graphical slider data from a Processing for Android Sketch (running on my device) over Bluetooth. The Arduino recieves the data and I can control a sine wave frequency with it. The processing send code is here:

void s1(int val)    // number returned by slider when moved
{
byte[] a= intToByteArray(val);  // excellent function for converting int to byte array (byte[]) which is what the Ketai bluetooth     //library sends.

  bt.broadcast(a);  // send (broadcast)  the bluetooth slider info

  //print(a);   // debug
}

and on the arduino side (i am using the blindingly FANTASTIC Mozzi library for the sound. XD ) :

void updateControl(){   // Mozzi part of program that deals with updating control info...
  // put changing controls in here
   while(mySerial.available())
  
 {
   int val=mySerial.read();   // read incoming serial data
  map(val,0,127,0,1000);   // map the value
    //Serial.println(val);   // debug
    aSin.setFreq(val*6);   // set frequency to recieved value
   // analogWrite(led,val); // more debug
}

}

Now, would anyone be able to help me work out how to write something that would read the serial port and if it recieved a particular character ( say a 'q') it would then jump to a function that would then read the slider data? The reason I want to do this is to be able to have several controls including sliders/knobs/buttons etc... to control sound produced by Mozzi. I can't quite understand how I would do this. I have seen examples showing an LED turning on or off depending upon which character is recieved BUT this does not involve continuous serial data. Would it involve splitting up the Serial information?? APU any help appreciated. Cheers, Deeble (mubase.)

Now, would anyone be able to help me work out how to write something that would read the serial port and if it recieved a particular character ( say a 'q') it would then jump to a function that would then read the slider data?

That's trivial.

void loop()
{
   if(Serial.available() > 0)
   {
       char letter = Serial.read();
       if(letter == 'q')
           readTheSlider();
   }
}

Hi Paul. Yep. I think I get that ie:

int readslider (int theValue)
{
  theValue =mySerial.read();
  return theValue;
}

and then in the loop:

 int a;
int frequency=readSlider(a);
aSin.setFreq(frequency);

but then wouldn't the slider also be sending back the character data as well?? If I am sending the identifier character from processing as:

void slider1(int theValue) 
{
byte[] character={'q'}
bt.broadcast (character);
byte[] a= intToByteArray(val); 
bt.broadcast(a);
}

Or am I way off... ??

Isn't readSlider() supposed to be reading from mySerial, not Serial?

oops. yep! Thanks, I've corrected my mistake (this was quick and on the fly!) I have tried this method though and it didnt work... Is there a way I could cut the identifier character out of the serial info recieved by the Arduino so I just have the slider numbers?? That way I could make a switch case for all my controls....

Is there a way I could cut the identifier character out of the serial info recieved by the Arduino so I just have the slider numbers??

Probably. But, I have no clue what "serial info" you are getting, so I have no idea how to do it. Hint, hint.

The Serial data I am recieving comes from a ControlP5 library "slider" object. Int values from 0 - 127. The slider value is sent whenever I move the slider (android touchscreen.):

void s1(int val)    // number returned by slider when moved
{
byte[] a= intToByteArray(val);  // excellent function for converting int to byte array (byte[]) which is what the Ketai bluetooth     //library sends.

  bt.broadcast(a);  // send (broadcast)  the bluetooth slider info

  //print(a);   // debug
}

If I print out the data received in the Arduino Serial window I get the slider int values (0-127) . The processing for Android Ketai library is used as this has the bluetooth capabilities... The slider data (int) needs to be converted to byte array as this is how the ketai bluetooth library works. When I try to send an identifier character from inside the slider function :

void s1(int val)    // number returned by slider when moved
{
byte[] a= intToByteArray(val);  // excellent function for converting int to byte array (byte[]) which is what the Ketai bluetooth     
//library sends.
byte[] b={'q'}; // sending the 'q' character as a byte[]
bt.broadcast(b);  // send identifier character
  bt.broadcast(a);  // send (broadcast)  the bluetooth slider info

  //print(a);   // debug
}

I get the slider values PLUS the number 113 wedged in the list of numbers as an int... Does this make sense??

I get the slider values PLUS the number 113 wedged in the list of numbers as an int...

You get this where, using what code?

gpud: I get the slider values PLUS the number 113 wedged in the list of numbers as an int... Does this make sense??

You will get the slider value plus 113 as 113 is the ascii value of "q" which you are also sending with bt.broadcast(b);

Hi again. I recognize the 113 is the ascii code for ‘q’. I’ve taken a snapshot of what COM3 is printing out when I move the slider (attachment.)
As for the recieve code it is just this:

void updateControl(){
  // put changing controls in here
   while(mySerial.available())
  
 {
   int val=mySerial.read();
  map(val,0,127,0,1000);
    Serial.println(val);
    aSin.setFreq(val*3);   
  
}

}

How can I seperate the 113 from the actual slider code?? :disappointed_relieved:

serial_recieve_bt.jpg

How can I seperate the 113 from the actual slider code?

It’s already separated, by a carriage return, a line feed, or both.

Do you mean “How do I distinguish the 113 from the slider value?”? If so, you have a real challenge. What if the slider value IS 113?

You really need to figure out how to make the sender send characters, not bytes. Send the delimiter as a ‘q’. Send the slider values as a string (‘1’, ‘3’, ‘8’, for instance). Send a delimiter AFTER the slider value, such as ‘;’. Then, it is easy to determine where a slider value starts (after the ‘q’)and where it ends (before the ‘;’) (Or my favorites, ‘<’ and ‘>’ instead of q and ;).

Until then, you are just guessing that 113 means q and the next byte is the slider value. While you might think that it is easy to see that this is the case, serial data delivery is not guaranteed. There can be corrupt bits, causing the byte to be discarded. If that happens to be the slider value of 113, you are hosed.

Hmmmm… Had a bit of success by making the slider values (processing side) range from 0-100 then doing this in Arduino:

void updateControl(){
  // put changing controls in here
   while(mySerial.available())
  
 {
  
   int val=mySerial.read();
   if (val==113) //if val recieved is 'q'
   {
   int f= slider1read(val2);    // make f the variable returned by slider1read
  
    aSin.setFreq(f*8);   
  
}
if (val==114)              // if val= 'r' (114)
{
  int f2=slider2read(val3);
  //aSin.setFreq(f2*12);
  vol=(map (f2,0,100,0,255));
}
}
 }

the slider read functions look like this:

int slider1read( int value)
{
 int valuesend=mySerial.read();    // read serial port
  if (valuesend<113)                          // if its not 'q' then load the variable with slider1 data
  {
    value=valuesend;    
  }
  return value;           // send it back 
}
int slider2read (int value)
{
 int valuesend=mySerial.read();
if(valuesend <114 && valuesend !=113)             
{
 value=valuesend;                  // if its not 'q' or 'r' then load the variable with the slider2 data
}
return value;
  
}

I’ve used slider1 for frequency and slider 2 for volume. They sort of work but with occasional lost data and a clicking noise which I’m guessing comes with the bluetooth …

I've used slider1 for frequency and slider 2 for volume.

And 'q' and 'r' for the delimiters. I can see that rational. Well, actually, I can't.

   if (val==113) //if val recieved is 'q'

Why not just say that?

   if(val == 'q')

No comment needed and no possibility of the comment and code getting out of sync.

Ah yes! Thanks for common sense. :~ ive resorted that.

Sorting out these clicks is going to be a challenge though. I need to find out if I can somehow smooth out the sound. Clicks on every movement of the sliders....