Arduino UNO + RGB Sensor + BLE Shield

Hello,

I am currently building a project that involves a BLE shield (from RedBear Lab: http://redbearlab.com/bleshield/) and a RGB color sensor(from altas scientific: http://atlas-scientific.com/product_pages/sensors/env-rgb.html). The BLE shield is controlled by a iOS app. I had tested the components individually using the sample code that is given by the respective supplies and they work. However when i combine them together, it stop halfway.

This is my code: 

  #include <Arduino.h>
  #include <SPI.h>
  #include "ble.h"
  
  #include <SoftwareSerial.h>                                                    
  #define rxpin 2                                                                
  #define txpin 3 
  
  SoftwareSerial myserial(rxpin, txpin);
  
  String inputstring = "";                                                       
  String sensorstring = "";                                                      
  boolean input_stringcomplete = false;                                          
  boolean sensor_stringcomplete = false; 
  
  void setup()
  {
    SPI.setDataMode(SPI_MODE0);
    SPI.setBitOrder(LSBFIRST);
    SPI.setClockDivider(SPI_CLOCK_DIV16);
    SPI.begin();
    
    inputstring.reserve(5);                                                   
    sensorstring.reserve(30);  
    
    ble_begin();
    
    Serial.begin(57600);
  }
  
  void loop()
  {
    // when data is available from the iOS app to the ble shield//
    while ( ble_available() )
    {
      char inchar = (char)ble_read();
      inputstring += inchar;
      if(inchar =='\r'){
      Serial.print(inputstring);
      Serial.print("\n");
      Serial.print("Input successfully stored\n");
      input_stringcomplete=true;
      }
    }
    
    // send the data to the sensor//
    if(input_stringcomplete)
    {
     myserial.print(inputstring); 
     myserial.print('\r');
     Serial.print("Already send input to Sensor\n");
     inputstring="";
     input_stringcomplete=false;
    }
   

   // problem lies with this function //
   // when data come from the sensor//
   while(myserial.available())
   {
     Serial.print("Sensor has data\n");
     char inchar = (char)myserial.read();
     sensorstring += inchar;
     if(inchar == '\r')
     {
     sensor_stringcomplete = true;
     }
   }
    
    // write the reading to the ble shield//
    if (sensor_stringcomplete){                                                 
       
      for(int i=0; i<sensorstring.length(); i++)
      {
        ble_write(sensorstring.indexOf(i));
      }      
       sensorstring = "";       
       sensor_stringcomplete = false;        
      }  
    
    
    ble_do_events();
  }

Currently, I am able to see all of the serial print statement except for the last one which the program does not execute the while loop. I had attached the sketch for the sample codes, in which i had more or less modified the codes on.

I am stuck with this problem for a few days and any help will be very much appreciated.

Chat_Sketch.ino (634 Bytes)

altas_scientific_sample_code.ino (3.14 KB)

  String inputstring = "";                                                       
  String sensorstring = "";

This isn't a good start.

However when i combine them together, it stop halfway.

You'll really need to do a better job than this. Are you sure it isn't 47.9%?

You'll really need to do a better job than this. Are you sure it isn't 47.9%?

is there a problem with the way i declare the character string? Sorry for not making myself clearer, my main problem with the code is that the while loop using myserial.available() is never executed . I had tested the hardware and it is working fine..

is there a problem with the way i declare the character string?

Yes. You are using the String class. Get rid of it. Put your big boy pants on and learn to use char arrays.

the while loop using myserial.available() is never executed .

What is connected to it?

What is connected to it?

It is connected to the Atlas Scientific RGB sensor to provide serial communication between the Arduino UNO and my colour sensor.

Hi, is there anybody who can help me? I get rid of the string class and use character array as recommended, and i use the Arduino UNO serial port (digital pin 1 and 0) instead of using software serial but i still cannot get it to work.

  #include <Arduino.h>
  #include <SPI.h>
  #include "ble.h"
 
  unsigned char inputstring[16]={0};  
  unsigned char inlen = 0;
  unsigned char sensorstring[16]={0};   
  unsigned char sensorlen = 0;  
  boolean input_stringcomplete = false;                                          
  boolean sensor_stringcomplete = false; 
  
  void setup()
  {
    SPI.setDataMode(SPI_MODE0);
    SPI.setBitOrder(LSBFIRST);
    SPI.setClockDivider(SPI_CLOCK_DIV16);
    SPI.begin();
    
    ble_begin();
    
    Serial.begin(38400);
  }
  
 
  void loop()
  {
    while (ble_available())
    {
      char from_ble = (char)ble_read();
      inputstring[inlen++] = from_ble;
      if(from_ble =='\r')
      {
        input_stringcomplete=true;  
      }
    }
    
    if(input_stringcomplete)
    {
      for(int i=0;i<inlen;i++)
      {
        Serial.print(inputstring[i]);
      }
      
      for(int j=0;j<inlen;j++)
      {
        inputstring[j] = 0;
      }
      
      inlen=0;
      input_stringcomplete=false;
    }
    
   
   while(Serial.available())
   {
     char from_sensor = (char)Serial.read();
     sensorstring[sensorlen++] = from_sensor;
     if( from_sensor== '\r')
     {
       sensor_stringcomplete = true;
     }
   }
        
   if (sensor_stringcomplete)
   {
     for(int x=0; x<sensorlen; x++)
     {
       ble_write(sensorstring[x]);
     }
     

     for(int k=0; k<sensorlen; k++)
     {
       sensorstring[k] = 0;
     }
     sensorlen=0;
     sensor_stringcomplete = false;   
   }

  ble_do_events();
  }

but i still cannot get it to work.

The code does something. Care to explain what it actually does?

You want it to do something. Care to explain what that is?

      inputstring[inlen++] = from_ble;

It's a good idea to check that there is room in the array BEFORE adding to the array.

The name of the variable implies something that is not true. It implies that the data in the array is a string. It is not. It is an array of characters. A string is an array of characters that is NULL terminated. You array of characters is NOT NULL terminated. Therefore, it is not a string.

It isn't clear what the diddling with the SPI stuff is for, if you aren't using SPI to read from the sensor. If you are using SPI, then it isn't a serial device. If it is a serial device, then diddling with the SPI stuff is not necessary.

and i use the Arduino UNO serial port (digital pin 1 and 0) instead of using software serial but i still cannot get it to work.

Why? You should be using the software serial port, so that the hardware serial port can be used for debugging.

The quick start guide on the BLE shield page says that you need to be running some version of Firmata on the Arduino. That will mean that the Arduino is just a dumb device, and you will not be able to connect anything but dumb sensors to it. The color sensor is not a dumb sensor.

The code does something. Care to explain what it actually does?

You want it to do something. Care to explain what that is?

The code is to get send the data from the BLE shield to the RGB colour sensor to trigger the RGB colour sensor to return a set of colour signatures. The colour signatures are then returned back the the BLE shield.

So basically, when there is bytes available at the ble shield, it is read and stored into the character array inputstring and then set the input flag to true.

While the input flag is true, it will print the information to the RGB colour sensor, and when there are bytes available to read at the RGB colour sensor, the sensor flag will be set to true, and then writing it back to the ble shield.

Currently, I am able to receive information from the BLE shield and stored it in the char array, while i am unable to get the RGB colour sensor to return the colour signatures.

While using the initial sketch that i had posted, i printed myserial.available() on the serial monitor and it returns a value of 0.

It isn’t clear what the diddling with the SPI stuff is for, if you aren’t using SPI to read from the sensor. If you are using SPI, then it isn’t a serial device. If it is a serial device, then diddling with the SPI stuff is not necessary.

The SPI stuffs are for the setup of the BLE shield (http://redbearlab.com/bleshield/)

The code is to get send the data from the BLE shield to the RGB colour sensor to trigger the RGB colour sensor to return a set of colour signatures. The colour signatures are then returned back the the BLE shield.

Apparently, English is not your native language. Even so, this doesn't make sense. "is to get send"? The BLE library is expecting to talk to the Arduino using the Firmata protocol. That sketch is NOT a Firmata sketch.

You might be able to add a callback to the Firmata sketch, so that a specific SYSEX message triggers that callback. Then, in the callback, you would talk to the sensor, and wait for a reply, then forward that reply, in the appropriate format, back to the phone.

However, there are several problems with this approach. One is that the process for defining callbacks is not well documented. Another is that the protocol for replying to a SYSEX message is not well documented. The third is that it is unlikely that the iPhone app is designed to send SYSEX messages. And, the 4th, and probably most significant is that it does not appear that you have the technical skills to even determine if any of this is feasible, let alone to actually implement them.

I am sorry but what is a firmata sketch and how it is different from the other sketches? This is included in the sample code for the BLE shield and i am able to run this sketch with the BLE shield on my Arduino UNO board.

I had edited my sketch based on the coding on this sketch.

#include <Arduino.h>
#include <SPI.h>
#include "ble.h"

void setup()
{
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(LSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  SPI.begin();
  
  ble_begin();
  
  Serial.begin(57600);
}

unsigned char buf[16] = {0};
unsigned char len = 0;

void loop()
{
  while ( ble_available() )
    Serial.write(ble_read());

  while ( Serial.available() )
  {
    unsigned char c = Serial.read();
    if (c != 0xD)
    {
      if (len < 16)
        buf[len++] = c;
    }
    else
    {
      for (int i = 0; i < len; i++)
        ble_write(buf[i]);
      len = 0;
    }
  }
  
  ble_do_events();
}

Paul, I had already added a BLEfirmata sketch into my Arduino library.

I went through the sample code provided by the BLE shield vendor, and my impression is that a standard Arduino sketch works, which is why I edited the sample code given and thought that it might works.

Paul, I had already added a BLEfirmata sketch into my Arduino library.

I went through the sample code provided by the BLE shield vendor, and my impression is that a standard Arduino sketch works, which is why I edited the sample code given and thought that it might works.

All the work of dealing with the data coming to the Arduino from the BLE shield, and replying to that data, is handled in the ble_do_events() function. Feel free to hack that function, to enable the Arduino to do more, in response to specific directives from the iPhone.

Before you do that, though, explain how the iPhone will send a specific directive to tell the Arduino to read from your sensor. Also, explain how the iPhone will display the response from the Arduino to that directive.

This is included in the sample sketch that describe the codes in the sketch.

Connect your Arduino with BLE Shield to PC. Use Serial Monitor to type and receive data.

Serial Monitor settings:
-Carriage return
-57600 baud

Text typed from iPhone will be transferred to PC and vice versa.

The program first setup SPI pins and then it initials the BLE library by using ble_begin().

And then it enters to the loop() which doing some tasks repeatedly.

The loop first reads if any data comes from iOS App using ble_available() and ble_read() and if so, it will send it out via the Serial interface of Arduino.

And then it reads from Serial port to see any data from PC, if so, it transfers data using ble_write() to send to iOS App.

Currently, you can send up to 19 bytes at a time.

After all, it calls ble_do_events() so that to allow the low layer BLE library to do its house-keeping tasks.

What i am doing is instead of sending and receiving the data from the serial monitor, i am attempting to do the same with my RGB colour sensor.