gpt4 book ai didi

c++ - 带有 SoftWire(I2C 仿真器)库的多个 tcs34725 arduino 颜色传感器

转载 作者:太空狗 更新时间:2023-10-29 23:12:24 24 4
gpt4 key购买 nike

我想在我的 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)变量中:

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-master

感谢此代码的开发者!

关于c++ - 带有 SoftWire(I2C 仿真器)库的多个 tcs34725 arduino 颜色传感器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46650433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com