Hi There,
I have three 10cm Water Level Sensors Grove - Water Level Sensor | Seeed Studio Wiki I am having difficulty in getting them to communicate correctly when using a multiplexer.
The multiplexer sees them all when I run I2C scanner. And I can get each of them to read individually but not together.
I would appreciate any help, if someone has completed this already.
#include <Wire.h>
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
unsigned char low_data[8] = {0};
unsigned char high_data[12] = {0};
#define NO_TOUCH 0xFE
#define THRESHOLD 100
#define ATTINY1_HIGH_ADDR 0x78
#define ATTINY2_LOW_ADDR 0x77
// Select I2C BUS/////////
/////////////used this from TCA9548A adafruit online tutoroal/////////////////
void TCA9548A(uint8_t bus) {
Wire.beginTransmission(0x70);
Wire.write(1 << bus);
Wire.endTransmission();
Serial.print(bus);
}
///////////////////////////////////////////////////////////////
void setup() {
SERIAL.begin(115200);
Wire.begin();
TCA9548A(0);
TCA9548A(1);
}
void getHigh12SectionValue(void)
{
memset(high_data, 0, sizeof(high_data));
Wire.requestFrom(ATTINY1_HIGH_ADDR, 12);
while (12 != Wire.available());
for (int i = 0; i < 12; i++) {
high_data[i] = Wire.read();
}
delay(10);
}
void getLow8SectionValue(void)
{
memset(low_data, 0, sizeof(low_data));
Wire.requestFrom(ATTINY2_LOW_ADDR, 8);
while (8 != Wire.available());
for (int i = 0; i < 8 ; i++) {
low_data[i] = Wire.read(); // receive a byte as character
}
delay(10);
}
void check()
{
int sensorvalue_min = 250;
int sensorvalue_max = 255;
int low_count = 0;
int high_count = 0;
while (1)
{
uint32_t touch_val = 0;
uint8_t trig_section = 0;
low_count = 0;
high_count = 0;
getLow8SectionValue();
getHigh12SectionValue();
for (int i = 0; i < 8; i++)
{
if (low_data[i] >= sensorvalue_min && low_data[i] <= sensorvalue_max)
{
low_count++;
}
if (low_count == 8)
{
}
for (int i = 0; i < 12; i++)
{
if (high_data[i] >= sensorvalue_min && high_data[i] <= sensorvalue_max)
{
high_count++;
}
if (high_count == 12)
{
}
for (int i = 0 ; i < 8; i++) {
if (low_data[i] > THRESHOLD) {
touch_val |= 1 << i;
}
}
for (int i = 0 ; i < 12; i++) {
if (high_data[i] > THRESHOLD) {
touch_val |= (uint32_t)1 << (8 + i);
}
}
while (touch_val & 0x01)
{
trig_section++;
touch_val >>= 1;
}
TCA9548A(0);
SERIAL.print("water level1 = ");
SERIAL.print(trig_section * 5);
SERIAL.println("% ");
SERIAL.println(" ");
SERIAL.println("*********************************************************");
delay(1000);
TCA9548A(1);
SERIAL.print("water level2 = ");
SERIAL.print(trig_section * 5);
SERIAL.println("% ");
SERIAL.println(" ");
SERIAL.println("*********************************************************");
delay(1000);
}
}
}
}
///////////////////////////////////////////////////////////////
void loop(){
check();
}