TTP223 use case

Hey everyone,

Just doing a quick edit to fix spelling mistakes and complete my post as my laptop battery died :upside_down_face:

I am struggling to understand how cases work for different pages using a TTP223 captive touch button to change between multiple "pages". The default value for my specific TTP223 is LOW and once you touch it, the state changes to HIGH.

I know buttons and menus have been discussed to death, but I have not seen a setup similar to mine of what I want to achieve, most use multiple buttons and sub menus etc. etc.

This is also my first time attempting this and after about a week of struggling I decided to ask the community for assistance or guidance :slightly_smiling_face:

I want to make a small project for now where I display different sensor data. Displaying the data is not the issue and the easy part for me lol. I am able to read and display the sensor data on a single page but now I want to have multiple pages. The main page should be a "home" page displaying a summary of the sensors, when you touch the button it's state changes to "HIGH" and should change to the next page displaying the first sensors data and when pressed again, go to the next sensors page and so on until it gets back to the "home" screen.

I commented out where I was able to change the screen color, but that was just the start of my project.

For now I am just trying to figure out how to use the TTP223 to change between multiple pages and have it round robin between them. Also want to save the last viewed page to EEPROM and have it load up and display on power on but that is another discussion all together.

With that out of the way, here's my code so far, I would really appreciate any inputs.

I do not have a schematic as of yet, just plugged everything onto my breadboard using a waveshare 1.5" RGB OLED display and the TTP223 sensor (red board) and been trying to get the pages to function correctly.

#include <EEPROM.h>
#include <SPI.h>
#include <Wire.h>
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>

#define touchSW       9     //TTP223 touch button
#define SCREEN_WIDTH  128
#define SCREEN_HEIGHT 128

const PROGMEM uint16_t ADDR_PAGE_COUNTER = 0;
byte pageCounter, type;
bool pageChanged = false;

// The SSD1351 is connected like this (plus VCC plus GND)
const uint8_t   OLED_pin_scl_sck        = 13;
const uint8_t   OLED_pin_sda_mosi       = 11;
const uint8_t   OLED_pin_cs_ss          = 10;
const uint8_t   OLED_pin_res_rst        = 8;
const uint8_t   OLED_pin_dc_rs          = 7;

// SSD1351 color definitions
const uint16_t  OLED_Color_Black        = 0x0000;
const uint16_t  OLED_Color_Blue         = 0x001F;
const uint16_t  OLED_Color_Red          = 0xF800;
const uint16_t  OLED_Color_Green        = 0x07E0;
const uint16_t  OLED_Color_Cyan         = 0x07FF;
const uint16_t  OLED_Color_Magenta      = 0xF81F;
const uint16_t  OLED_Color_Yellow       = 0xFFE0;
const uint16_t  OLED_Color_White        = 0xFFFF;

// declare the display
Adafruit_SSD1351 oled = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, OLED_pin_cs_ss, OLED_pin_dc_rs, OLED_pin_res_rst);

unsigned long currentMillis;

void setup() {
  // put your setup code here, to run once:
  pinMode(touchSW, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  delay(250);   // Let the Screen bootup
  pageCounter = EEPROM.read(ADDR_PAGE_COUNTER);

  attachInterrupt(digitalPinToInterrupt(2), ButtonState, RISING);
  
  // initialise the SSD1351
  oled.begin();
  oled.setFont();
}

void loop() {
  // The below code works great changing the display color

/*  if (digitalRead(9) == HIGH){
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println(F("LED ON"));
    Serial.println(F("Button Touched"));
    // yes! toggle display visibility
   // isDisplayVisible = false;
   oled.fillScreen(OLED_Color_Black);

    // apply
    oled.enableDisplay(isDisplayVisible);
  }
  else {
    digitalWrite(LED_BUILTIN, LOW); 
    Serial.println(F("LED OFF"));
    Serial.println(F("Button not touched"));
    // yes! toggle display visibility
    //isDisplayVisible = true;
   oled.fillScreen(OLED_Color_White);

    // apply
    oled.enableDisplay(isDisplayVisible);
  }*/

  currentMillis = millis();

  if (pageCounter > 0)
   switch (pageCounter) {
    case 0:
      pageOne();
      pageTwo();
      pageThree();
      pageFour();
      break;

    case 1:
     pageOne();
     break;

   case 2:
    pageTwo();
    break;

  case 3:
    pageThree();
    break;

  case 4:
    pageFour();
    break;
   }

   savePageChange();
   pageChanged = false;
}

  
void pageOne(){
  switch (type) {
    case 1:      
      oled.fillScreen(OLED_Color_Blue);
      oled.setTextColor(OLED_Color_White);
      oled.setTextSize(2);
      oled.setCursor(0, 50);
      oled.print("Page 1");  
  }
}

 void pageTwo(){
  switch (type) {
    case 2:      
      oled.fillScreen(OLED_Color_Red);
      oled.setTextColor(OLED_Color_Black);
      oled.setTextSize(2);
      oled.setCursor(0, 50);
      oled.print("Page 2");  
  }
}

void pageThree(){
  switch (type) {
    case 3:      
      oled.fillScreen(OLED_Color_Yellow);
      oled.setTextColor(OLED_Color_Black);
      oled.setTextSize(2);
      oled.setCursor(0, 50);
      oled.print("Page 3");  
  }
}

void pageFour(){
  switch (type) {
    case 4:      
      oled.fillScreen(OLED_Color_Black);
      oled.setTextColor(OLED_Color_White);
      oled.setTextSize(2);
      oled.setCursor(0, 50);
      oled.print("Page 4");  
  }
}

void ButtonState() {
  pageCounter++;
  pageChanged = true;
  if (pageCounter >= 5) {
    pageCounter = 0;
  }
}

void savePageChange() {
  if (pageChanged) {
    EEPROM.write(ADDR_PAGE_COUNTER, pageCounter);
  }
}

Hey Arduino community,

So I have cracked it lol.

This has been bugging me endlessly, it cannot be this difficult, so today at work I had the thought of why try to over complicate things, just go back to basics and not try to over complicate the sketch to test my "pages" project by means of the TTP223.

Below is my simple code that is working as I was planning, it shows a "splash screen" and then goes directly to the first page, when I touch the TTP223 it goes over to the next page until it reaches the last page, then jumps back to the first page, aka round robin.

I was at the point yesterday where I wanted to see if a debounce statement would work on the TTP, but luckily I did not go down that rabbit hole.

Code as promised:

#include <EEPROM.h>
#include <SPI.h>
#include <Wire.h>
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>

#define touchSW       9     //TTP223 touch button
#define SCREEN_WIDTH  128
#define SCREEN_HEIGHT 128

// The SSD1351 is connected like this (plus VCC plus GND)
const uint8_t   TFT_pin_scl_sck        = 13;
const uint8_t   TFT_pin_sda_mosi       = 11;
const uint8_t   TFT_pin_cs_ss          = 10;
const uint8_t   TFT_pin_res_rst        = 8;
const uint8_t   TFT_pin_dc_rs          = 7;

// SSD1351 color definitions
const uint16_t  tft_Black        = 0x0000;
const uint16_t  tft_Blue         = 0x001F;
const uint16_t  tft_Red          = 0xF800;
const uint16_t  tft_Green        = 0x07E0;
const uint16_t  tft_Cyan         = 0x07FF;
const uint16_t  tft_Magenta      = 0xF81F;
const uint16_t  tft_Yellow       = 0xFFE0;
const uint16_t  tft_White        = 0xFFFF;

int selector = 0, pageCounter = 0;
boolean isPressed = false;

// declare the display
Adafruit_SSD1351 tft = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, TFT_pin_cs_ss, TFT_pin_dc_rs, TFT_pin_res_rst);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(touchSW, INPUT);
  tft.begin();
  tft.setFont();
  tft.fillScreen(tft_White);
  tft.setTextColor(tft_Black, tft_White);
  tft.setTextSize(2);
  tft.setCursor(4, 50);
  tft.print("TEST CASE");
  delay(2000);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(touchSW) == LOW &&  isPressed == false){
    isPressed = true;
    doSwitchStatement();
    selector++;
    if (selector > 4) {
      selector = 0;
    } 
  }
  else if (digitalRead(touchSW) == HIGH){
    isPressed = false;
  }
}

void doSwitchStatement(){
    switch (selector) {
    case 0:
      Serial.println(F("Case 0"));
      tft.fillScreen(tft_Black);
      tft.setTextColor(tft_White);
      tft.setTextSize(1);
      tft.setCursor(0, 10);
      tft.println("Page 1 Case 0");
      break;

    case 1:
      Serial.println(F("Case 1"));
      tft.fillScreen(tft_Black);
      tft.setTextColor(tft_White);
      tft.setTextSize(1);
      tft.setCursor(0, 30);
      tft.println("Page 2 Case 1");
      break;

    case 2:
      Serial.println(F("Case 2"));
      tft.fillScreen(tft_Black);
      tft.setTextColor(tft_White);
      tft.setTextSize(1);
      tft.setCursor(0, 50);
      tft.println("Page 3 Case 2");
      break;
    
    case 3:
      Serial.println(F("Case 3"));
      tft.fillScreen(tft_Black);
      tft.setTextColor(tft_White);
      tft.setTextSize(1);
      tft.setCursor(0, 70);
      tft.println("Page 4 Case 3");
      break;

    case 4:
      Serial.println(F("Case 4"));
      tft.fillScreen(tft_Black);
      tft.setTextColor(tft_White);
      tft.setTextSize(1);
      tft.setCursor(0, 90);
      tft.println("Page 5 Case 4");
      break;      
    }
}

Reading through this again now, I see I have a few variables declared that are not used in this code, just ignore those. I'll be using them and a few more for my final project.

I think we can agree that the TTP223 is - being a logic device designed for the purpose - de-bounced perfectly already. :+1:

1 Like

Yes exactly, but it being my first ever use of any touch based anything, I was at my witts end to say the least.

I did not know what to expect or how to use it, but now I know for future and it being such a great addition to most projects I will definitely use it for a lot more to come.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.