我想在我的Arduino mega上安装2个TCS34725颜色传感器。传感器使用I2c通信,因此我不能将它们放在相同的I2C引脚上,因为它们具有相同的地址。我提出的解决方案是使用一个模拟器线路库"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和图书馆的新手。
发布于 2017-12-25 08:58:30
问题解决了!找到一个库,用于放置2个或更多使用软i2c的tcs34725传感器。这里有个链接:Adafruit_TCS34725_SoftI2C-master
感谢这段代码的开发者!
https://stackoverflow.com/questions/46650433
复制相似问题