带有SoftWire(I2C Emulator)库的多个tcs34725 arduino颜色传感器
Multiple tcs34725 arduino color sensor with SoftWire (I2C Emulator) Library
我想在我的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-主
感谢此代码的开发人员!
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 人脸跟踪arduino代码的优化
- vscode下的Arduino代码出现意外编译错误
- Arduino从Txt读取整数
- Arduino C++在构造函数中用参数声明对象数组
- C++ Boost::asio串行通信与Arduino无法写入
- Arduino millis() - millis() 怎么能等于 0 以外的任何东西?
- 在Arduino中将PWM通道命名为LEDC是否有特定原因
- 'val' Arduino 错误代码之前的预期'('
- 我可以在 Arduino 的库文件夹之外安装库吗?
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 无法在 Arduino 中uint8_t数组转换为无符号长整型数组
- Arduino-C++ bool 不会从 false 变为 true
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- cmsis_compiler.h:在为 Arduino Due 编译 Arduino Tensorflow lite 库
- Arduino SocketIoClient - 没有用于调用的匹配函数
- Arduino无法编译名为SP:"Expected ')' before '(' token"的变量
- 带有SoftWire(I2C Emulator)库的多个tcs34725 arduino颜色传感器