带有SoftWire(I2C Emulator)库的多个tcs34725 arduino颜色传感器

Multiple tcs34725 arduino color sensor with SoftWire (I2C Emulator) Library

本文关键字:tcs34725 arduino 传感器 颜色 SoftWire I2C Emulator 带有      更新时间:2023-10-16

我想在我的Arduino mega上安装2个TCS34725颜色传感器。传感器使用I2c通信,因此我不能将它们放在相同的I2c引脚上,因为它们具有相同的地址。我提出的解决方案是使用模拟器Wire库"SoftWire"(https://github.com/felias-fogg/SoftI2CMaster)其可以将任意2个引脚模拟为SDA和SCL。这个库的用法完全相同,只是我必须创建一个实例并包含avr/io.h才能启动:

#include <SoftWire.h>
#include <avr/io.h>
SoftWire Wire = SoftWire();

我现在的问题是调整TCS34725库,使其与SoftWire一起工作,以下过程是我所做的,但它不起作用。这是原始库头文件的第一部分:

#ifndef _TCS34725_H_
#define _TCS34725_H_
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <Wire.h>

以下是访问完整库的链接:https://github.com/adafruit/Adafruit_TCS34725好吧,所以我所做的只是简单地更改

#include<Wire.h> 

#include<SoftWire.h>
#include<avr/io.h>
#define SDA_PORT PORTC
#define SDA_PIN 4
#define SCL_PORT PORTC
#define SCL_PIN 5

我做的另一件事是将Wire实例添加到头文件中的私有变量中:

private:
boolean _tcs34725Initialised;
tcs34725Gain_t _tcs34725Gain;
tcs34725IntegrationTime_t _tcs34725IntegrationTime;
>>>SoftWire Wire;<<<
void     disable(void);

我做的最后一件事是添加:

SoftWire Wire = SoftWire();

在库的cpp文件中包含之后。它看起来就像这样:

#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();

我保存了代码,并在草图上运行,这是代码:

#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);
}

此草图与原始WIRE.h完美配合,但现在无法与SoftWire.h配合使用。我是arduino和图书馆的新手。

问题解决了!找到了一个使用软i2c放置2个或多个tcs34725传感器的库。链接如下:Adafruit_TCS34725_SoftI2C-主

感谢此代码的开发人员!