104Views7Replies

Author Options:

Bluetooth, RGB sensor and Android app Answered

This code is supposed to convert information from a color sensor and display what ever color is shown to the sensor as RGB values on the android app. 
How can one incorporate bluetooth functionality to the following code? 
Any help will be more than greatly appreciated. Even a donation 
This is the code that I want to use:


#include <TimerOne.h>

#define S0     6
#define S1     5
#define S2     4
#define S3     3
#define OUT    2

int   g_count = 0;    // count the frequecy
int   g_array[3];     // store the RGB value
int   g_flag = 0;     // filter of RGB queue
float g_SF[3];        // save the RGB Scale factor
int unChar;

// Init TSC230 and setting Frequency.
void TSC_Init()
{
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(OUT, INPUT);

  digitalWrite(S0, LOW);  // OUTPUT FREQUENCY SCALING 2%
  digitalWrite(S1, HIGH);
}

   
// Select the filter color
void TSC_FilterColor(int Level01, int Level02)
{
  if(Level01 != 0)
    Level01 = HIGH;

  if(Level02 != 0)
    Level02 = HIGH;

  digitalWrite(S2, Level01);
  digitalWrite(S3, Level02);
}

void TSC_Count()
{
  g_count ++ ;
}

void TSC_Callback()
{
  switch(g_flag)
  {
    case 0:
         Serial.println("->WB Start");
         TSC_WB(LOW, LOW);              //Filter without Red
         break;
    case 1:
         Serial.print("->Frequency R=");
         Serial.println(g_count);
         g_array[0] = g_count;
         TSC_WB(HIGH, HIGH);            //Filter without Green
         break;
    case 2:
         Serial.print("->Frequency G=");
         Serial.println(g_count);
         g_array[1] = g_count;
         TSC_WB(LOW, HIGH);             //Filter without Blue
         break;

    case 3:
         Serial.print("->Frequency B=");
         Serial.println(g_count);
         Serial.println("->WB End");
         g_array[2] = g_count;
         TSC_WB(HIGH, LOW);             //Clear(no filter)  
         break;
   default:
         g_count = 0;
         break;
  }
}

void TSC_WB(int Level0, int Level1)      //White Balance
{
  g_count = 0;
  g_flag ++;
  TSC_FilterColor(Level0, Level1);
  Timer1.setPeriod(1000000);             // set 1s period
}

void setup()
{
  TSC_Init();
  Serial.begin(9600);
  Timer1.initialize();             // defaulte is 1s
  Timer1.attachInterrupt(TSC_Callback); 
  attachInterrupt(0, TSC_Count, RISING); 

  delay(4000);

  for(int i=0; i<3; i++)
    Serial.println(g_array[i]);

  g_SF[0] = 255.0/ g_array[0];     //R Scale factor
  g_SF[1] = 255.0/ g_array[1] ;    //G Scale factor
  g_SF[2] = 255.0/ g_array[2] ;    //B Scale factor

  Serial.println(g_SF[0]);
  Serial.println(g_SF[1]);
  Serial.println(g_SF[2]);

}

void loop()
{
   g_flag = 0;
   for(int i=0; i<3; i++)
    Serial.println(int(g_array[i] * g_SF[i]));
   delay(4000);

}








I have a code that works with bluetooth and the android app but it's not giving me accurate colors. The above code gives accurate colors but I cant seem to find a way to incorporate the bluetooth section 
If you want to take a look at it: 

#include <FreqCount.h> // Library to count frecuency from TCS3200 sensor
#include <LiquidCrystal.h> // Library to LCD Control

/////////////////////////////////////////////////////////////////////

int S0 = 6;//pinB
int S1 = 5;//pinA
int S2 = 4;//pinE
int S3 = 3;//pinF
int Out = 2;//pinC PARA EL ARDUINO MEGA SE CAMBIA POR EL PIN 47
int LED = 13;//pinD
int unChar;
word absoluto;
word ROJO;
word VERDE;
word AZUL;


void setup() {
Serial.begin(9600);
FreqCount.begin(10);


//initialize pins
pinMode(LED,OUTPUT); //LED pinD
   
//color mode selection
pinMode(S2,OUTPUT); //S2 pinE
pinMode(S3,OUTPUT); //s3 pinF
   
pinMode(Out, INPUT); //Frecuency Out pinC # 47 on Arduino Mega 2560
   
//communication freq (sensitivity) selection
pinMode(S0,OUTPUT); //S0 pinB
pinMode(S1,OUTPUT); //S1 pinA
}

void loop() {
  if (Serial.available() >= 0) {
  // escucha la llegada de nuevos datos serie:
  unChar = Serial.read();
  // imprime el caracter:
 
   
//////////////////////////////////////////////////////////////
    if (unChar == 'R') {
         digitalWrite(LED, LOW);
         digitalWrite(S0, LOW); //S0
         digitalWrite(S1, HIGH); //S1
         digitalWrite(S2, HIGH); // RED Filter select
         digitalWrite(S3, HIGH); // RED Filter select
         delay(250);
       
         absoluto = (FreqCount.read()/10)*2;
         ROJO = absoluto;
         if(absoluto>255)
           {
          
          
          
       
           Serial.print(255);
         }
         else
           {
          
         
           Serial.print(ROJO);
         }
        
    }
///////////////////////////////////////////////////////////
    else if (unChar == 'G') {   
         digitalWrite(LED, LOW);
         digitalWrite(S0, LOW); //S0
         digitalWrite(S1, HIGH); //S1
         digitalWrite(S2, LOW); // GREEN Filter select
         digitalWrite(S3, HIGH); // GREEN Filter select
         delay(250);
         absoluto = (FreqCount.read()/10)*2;
         VERDE = absoluto + 35;
         if(absoluto>255)
           {
          
         
           Serial.print(255);
         }
         else
           {
          
          
           Serial.print(VERDE);
         }
        
    }
/////////////////////////////////////////////////////////////
    else if (unChar == 'B') {    
         digitalWrite(LED, LOW);
         digitalWrite(S0, LOW); //S0
         digitalWrite(S1, HIGH); //S1
         digitalWrite(S2, LOW); // BLUE Filter select
         digitalWrite(S3, LOW);// BLUE Filter select
         delay (250);
         absoluto = (FreqCount.read()/10)*2;
         AZUL = absoluto - 45;
         if(absoluto>255)
           {
          
          
           Serial.print(255);
         }
         else
           {
          
         
           Serial.print(AZUL);
         }
        
    }
  }
}

Discussions

0
None
Nighter3D

3 years ago

Well. the question i would ask is HOW is the bluetooth module communicating to the App? Few devices talk the exact same way and what you need to figure out is how the devices you are using are communicating protocol-wise. For example your app: What kind of data does it need as input? Same goes for your Bluetooth module: Is it acting as a virtual wire where serial data that comes, comes out at the other side. or do you need to use a API to get it out.

I know it may sound hard, but understanding how any device talks is a important part of working with various devices and once you get the hang of figuring out how they talk you can use almost everything!

I'll take a quick look and see if i can figure out how your app works and how to combine. stay tuned for my next comment.

0
None
Nighter3DNighter3D

Answer 3 years ago

Ok. your first code simply makes the measurements and then pumps them out through the Serial. probably for quickly reading through a terminal.

Your second code waits for the app to ask for a Value and then responds by giving it that measurement as a value between 0-255 (if the value is over it limits it to 255). So the protocol looks simple (and the bluetooth is apparantly working like a virtual wire).

example at how it talks:

App sends 'R' to arduino, Arduino reads 'R', Arduino Performs measurement of Red, sends out the value as a Byte (0-255) out of the serial.

So to get your first code to work like that you would want to remove all existing serial communication, add the part that it checks for serial communication and that if it receives say "R" it sends out the red value.

Should note: I dont know what kind of value you get from your first code, if its a large number (larger then 255) you may want to use the map() function (check the arduino reference) to have the value within 0-255.

I would try to give you some example code, but comments don't really well for that ^^;. also idk what app or RGB sensor you are using.

0
None
CourageQNighter3D

Answer 3 years ago

Hey Nighter3D,

I really appreciate your care and detailed response. You sound really knowledgeable.

I actually tried to incorporate the bluetooth communication part. I did some research and it seems that code would be

void loop() {
if (Serial.available() >= 0) {
// escucha la llegada de nuevos datos serie:
unChar = Serial.read();
// imprime el caracter:

However, I just cant figure out how merge the two. The first code is very accurate so I wanted to use the filter code and just incorporate the bluetooth of the 2nd. Trust me, I tried and I just cant figure it out YET.

A sample code wouldn't be too bad, Nighter3D :)

Thanks in advance.

0
None
Nighter3DCourageQ

Answer 3 years ago

The part you have there is the part that checks for serial communication and then stores what it got into a character variable called "unChar". The code then checks if it's 'R','G' or 'B' and gives the app the result it wants.

Ok here is a sample code:

if (Serial.available() >= 0) {
unChar = Serial.read(); //Reads the input
if (unChar == 'R') { //Returns red if requested
byte tmp = byte( map( (g_array[0] * g_SF[0]),0,1023,0,255) ) ;
Serial.print(tmp); //Returns the value in red
}else if (unChar == 'G') {
byte tmp = byte( map( (g_array[1] * g_SF[1]),0,1023,0,255) ) ;
Serial.print(tmp);
}else if (unChar == 'B') {
byte tmp = byte( map( (g_array[2] * g_SF[2]),0,1023,0,255) ) ;
Serial.print(tmp);
}
}

What this does is that it checks for input from the app. When it gets something it puts that input in the char variable called unChar. It then checks if that char is R,G or B. if it's say R it then does the following:

It takes that RGB output you want from the first function (g_array[0] * g_SF[0]), Remaps the value from its original size into a byte size (should not that here it remaps a value from 0,1023 to 0,255. you may want to replace the 1023 by what that code normally gives as the maximum...i don't know what values you get...not Psychic!), Converts it into a Byte and then gives the value to your App

0
None
Nighter3DNighter3D

Answer 3 years ago

Oh right. Be sure that this part is the ONLY part where Serial.print exists. you don't want the code sending garbage data to the app. So be sure to remove every Serial.print and Serial.println.

0
None
CourageQNighter3D

Answer 3 years ago

Oh thank you very much. I tried it but it didn't work. I will. Work on it some more and let you know if we succeeded.

0
None
CourageQNighter3D

Answer 3 years ago

I can give you my email if you want.