I am using cap1188 to detect the water level in a kettle. I got to the point where the water level was seen in the sense that the leads on the board lit up. Now I want to code it so that if the water level is detected by C1 and C2 that a display shows "2". if the water level is at C3 (also touching C1 & C2) it says "3" and so on.
I tried using an if statement combining the different inputs with "&&" but this doesn't seem to work (see below). How can I fix this issue? It worked fine when I used 2 boards and used this type of coding between the 2 boards.
Thank you in advance!
Code: Select all
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_CAP1188.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 4
#define DIO 5
TM1637Display display(CLK, DIO);
// Use I2C, no reset pin!
Adafruit_CAP1188 cap = Adafruit_CAP1188();
void setup() {
if (!cap.begin()) {
//Serial.println("CAP1188 not found");
while (1);
}
Serial.println("CAP1188 found!");
}
void loop() {
display.setBrightness(0x0f);
uint8_t touched = cap.touched();
for (uint8_t i = 0; i < 8; i++) {
if (touched & (1 << i)) {
Serial.print("C"); Serial.print(i + 1); Serial.print("\t");
}
}
if (touched & 0x01) {
display.showNumberDec(1, false);
delay(50);
}
else if ((touched & 0x01) && (touched & 0x02)) {
display.showNumberDec(2, false);
delay(50);
}
else if ((touched & 0x01) && (touched & 0x02) && (touched & 0x04)) {
display.showNumberDec(3, false);
delay(50);
}
else if ((touched & 0x01) && (touched & 0x02) &&(touched & 0x04) && (touched & 0x08)) {
display.showNumberDec(4, false);
delay(50);
}
else if ((touched & 0x01) && (touched & 0x02) &&(touched & 0x04) &&(touched & 0x08) && (touched & 0x10)) {
display.showNumberDec(5, false);
delay(50);
}
else if ((touched & 0x01) && (touched & 0x02) &&(touched & 0x04) &&(touched & 0x08) &&(touched & 0x10) && (touched & 0x20)) {
display.showNumberDec(6, false);
delay(50);
}
else if ((touched & 0x01) && (touched & 0x02) &&(touched & 0x04) &&(touched & 0x08) &&(touched & 0x10) &&(touched & 0x20) && (touched & 0x40)) {
display.showNumberDec(7, false);
delay(50);
}
else
{
}
}