Multiple touch input cap1188

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ninanas98
 
Posts: 5
Joined: Thu Oct 28, 2021 8:17 am

Multiple touch input cap1188

Post by ninanas98 »

Hi,

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
  {
  }
}

User avatar
dastels
 
Posts: 15817
Joined: Tue Oct 20, 2015 3:22 pm

Re: Multiple touch input cap1188

Post by dastels »

You need to flip your if ladder over and get rid of the "&&"s because it will now stop at the highest level it finds. I'd also extract the delay since they are all the same and because having duplicate code like that is error prone. You had 7 lines to change if you wanted to change the delay rather than 1. This part is optional.

Code: Select all

  if (touched & 0x40) {
    display.showNumberDec(7, false);
  } else if (touched & 0x20) {
    display.showNumberDec(6, false);
  } else if (touched & 0x10) {
    display.showNumberDec(5, false);
  } else if (touched & 0x08) {
    display.showNumberDec(4, false);
  } else if (touched & 0x04) {
    display.showNumberDec(3, false);
  } else if (touched & 0x02) {
    display.showNumberDec(2, false);
  } else if (touched & 0x01) {
    display.showNumberDec(1, false);
  }

  if (touched & 0x7F) {   // any of them triggered  
    delay(50);
  }
Dave

User avatar
ninanas98
 
Posts: 5
Joined: Thu Oct 28, 2021 8:17 am

Re: Multiple touch input cap1188

Post by ninanas98 »

Thanks Dave!!!

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”