Hey everyone,
Just doing a quick edit to fix spelling mistakes and complete my post as my laptop battery died ![]()
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 ![]()
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);
}
}