I would like to have 2 TCS34725 color sensor on my Arduino mega. The sensors use I2c communication, therefore I can't have them on the same I2C pins since they have identical addressess. The solution I came up was to use an emulator Wire library "SoftWire"(https://github.com/felias-fogg/SoftI2CMaster) that can emulate any 2 pins as SDA and SCL. The usage of this library is exactly the same except I have to create an instance and include avr/io.h for it to start:
#include <SoftWire.h>
#include <avr/io.h>
SoftWire Wire = SoftWire();
The problem for me now is to adjust the TCS34725 Library for it to work with SoftWire, the following proccess is what I've done, but it's not working. Here's the first part of the header file of the original library:
#ifndef _TCS34725_H_
#define _TCS34725_H_
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <Wire.h>
Here's the link to access full library:https://github.com/adafruit/Adafruit_TCS34725 Okay, So what I do is simply change the
#include<Wire.h>
to
#include<SoftWire.h>
#include<avr/io.h>
#define SDA_PORT PORTC
#define SDA_PIN 4
#define SCL_PORT PORTC
#define SCL_PIN 5
Another thing I did was to add the Wire instance into the private variables inside the header file:
private:
boolean _tcs34725Initialised;
tcs34725Gain_t _tcs34725Gain;
tcs34725IntegrationTime_t _tcs34725IntegrationTime;
>>>SoftWire Wire;<<<
void disable(void);
The last thing I did was to add:
SoftWire Wire = SoftWire();
after the includes in the cpp file of the library. It looks something just like this:
#ifdef __AVR
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#endif
#include <stdlib.h>
#include <math.h>
#include "Adafruit_TCS34725.h"
SoftWire Wire = SoftWire();
I saved the code and I ran it on a sketch, Here's the code:
#include "SoftWire.h"
#include "avr/io.h"
#include "Adafruit_TCS34725.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
void setup() {
Serial.begin(9600);
Serial.println("Color View Test!");
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1); // halt!
}
}
void loop() {
uint16_t clear, red, green, blue;
tcs.setInterrupt(false); // turn on LED
delay(60); // takes 50ms to read
tcs.getRawData(&red, &green, &blue, &clear);
tcs.setInterrupt(true); // turn off LED
Serial.print("C:\t"); Serial.print(clear);
Serial.print("\tR:\t"); Serial.print(red);
Serial.print("\tG:\t"); Serial.print(green);
Serial.print("\tB:\t"); Serial.print(blue);
}
This sketch works perfectly with the original WIRE.h, but it doesn't work now with SoftWire.h
. I am a newbie to arduino and libraries.