0

I have an Android app that sends data to the arduino HC-06. It is however giving me colors i did not send to it. Eg. Red in place of green, white in place of red etc. Here is a snippet of my android code that sends the data via bluetooth

 public void onColorChanged(int color) {
    BluetoothV2 bluetooth = new BluetoothV2();
    int r,g,b;
    r = Color.red(color);
    g = Color.green(color);
    b = Color.blue(color);
    bluetooth.Message(r+","+g+","+b);
 }

Here is a snippet of my arduino code

void loop() {  

if(Serial.available()>0){

while(Serial.available()>0){
   char inChar = (char)Serial.read();
   inputString += inChar;       
}    
   int red = Serial.parseInt();
   int green = Serial.parseInt();
   int blue = Serial.parseInt();
   color(red,green,blue);
   inputString = "";
   Serial.println("R:"&& red &&"G:"&& green &&"B:"&& blue);
  }
}  

void color (unsigned char red, unsigned char green, unsigned char blue){   
   analogWrite(redPin, 255-red);  
   analogWrite(greenPin, 255-green);
   analogWrite(bluePin, 255-blue);
}

Terminal data

               2,,,,,,,,,,,,,2,,5,,,,,,,,,,,,,,,,,,,,,,,,,02725,1,502725,,,,,,,,,02514,5,602515     ,5,3,,,,,025

Is the problem how i get the data though Serial.read()? Or what am i missing

4
  • When you say "terminal data" where is it coming from? What do you see in the serial monitor of the arduino IDE?
    – Madivad
    Dec 14, 2015 at 17:19
  • Why read characters to inputString and then parseInt()? And the Serial.println("R:"&&...) is strange that it even compiled. Try printing each value separately. Dec 18, 2015 at 10:41
  • @Madivad i meant data from my serial monitor. Dec 19, 2015 at 23:43
  • @MikaelPatel i was doing parseInt() to separate the commas in between the data eg data received=255,255,255. inputString is equated to that data and parseInt() the first time gets me the first value before the comma and the second time the second value after the first comma to the next comma(next character thats not of int data type). However i solved this issue. Let me post my answer. Dec 19, 2015 at 23:49

2 Answers 2

1
int firstcomma = inputString.indexOf(',');
int secondcomma = inputString.indexOf(',',firstcomma+1);
String r = inputString.substring(0,firstcomma);
String g = inputString.substring(firstcomma+1,secondcomma);
String b =  inputString.substring(secondcomma+1);
red = r.toInt();
green = g.toInt();
blue = b.toInt();
Serial.print("R");
Serial.print(red);
Serial.print("G");
Serial.print(green);
Serial.print("B");
Serial.println(blue);   

This solved my problems.

0

Maybe you can try again with something like this to check if the android part is not correct. From your terminal output it seems that you receive values higher than 255.

void onColorChanged(int red, int green, int blue) //values from 0 - 255
int r,g,b;
r= red;
g= green;
b= blue;
bluetooth.Message(r+","+g+","+b);

The receive part in your arduino code is like my code for a similar project. Try setting your color in the arduino code like this to check if the example code from above works:

void color (int red, int green, int blue)
{   
analogWrite(redPin, red);  //the higher your send value the brighter the color
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.