I have a problem with my esp32 code. Basically I want to make a simple smartwatch that I can connect to my phone with bluetooth and control my music on there. And also I want to use a round display to see the time.
The code for The clock and the menu is already done and the code for the music control is working too. BUT if I try to use both at the same time, then the screen is not working, only the music control. Does someone know the solution?
this is my code:
#include <BleKeyboard.h>
BleKeyboard bleKeyboard;
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite img = TFT_eSprite(&tft);
// millis();
unsigned long previousMillis = 0;
long counter = 0;
const long interval = 60000; // How long is a minute
const long interval2 = 2553; // minute in delay
// Clock start state
int H2 = 0; // hour2
int H1 = 0; // hour1
// :
int M2 = 0; // minute2
int M1 = 0; // minute1
#define t1 digitalRead(19)
#define t2 digitalRead(32)
#define t3 digitalRead(21)
#define t4 digitalRead(33)
// Variables
int TAB_Amount = 3; // TAB amount (0.Menu; 1.Clock; 2.Settings;)
int TAB2_Amount = 4; // TAB2 amount (1.H2; 2.H1; 3.M2; 4.M1)
int TAB = 1;
int TAB2 = 0; // Settings-TABs
int TABc = 1; // Menu-TAB-Change (preview)
int TABs = 1; // Setting-TAB-Change (preview)
int TDelay = 150; // Delay of the buttons
bool TAB2open = false;
bool isScreenON = true;
bool isBegun = false;
void Clock();
void Settings();
void MediaControl();
void s1();
void s2();
void s3();
void s4();
// Those save the last state of the button
int b1 = LOW;
int b2 = LOW;
int b3 = LOW;
int b4 = LOW;
void setup() {
// put your setup code here, to run once:
tft.init();
tft.setRotation(3);
img.createSprite(240, 240);
pinMode(19, INPUT);
pinMode(32, INPUT);
pinMode(33, INPUT);
pinMode(21, INPUT);
Serial.begin(115200);
bleKeyboard.begin();
}
void loop() {
// put your main code here, to run repeatedly:
if (TABc == 0){
TABc = TAB_Amount;
}
if (TABc > TAB_Amount){
TABc = 1;
}
if (TAB == 0){
Menu();
}
if (TAB == 1){
Clock();
}
if (TAB == 2){
Settings();
}
if (TAB == 3){
MediaControl();
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval){// For the clock
previousMillis = currentMillis;
Serial.println(previousMillis);
// ClockTimer:
M1++;
//---
if (M1 > 9){
M1 = 0;
M2++;
}
//---
if (M2 >= 6){
M2 = 0;
H1++;
}
//---
if (H1 > 9 && H2 < 2){
H1 = 0;
H2++;
}
else if (H1 >= 4 && H2 >= 2) {
M1 = 0;
M2 = 0;
H1 = 0;
H2 = 0;
}
// isScreenON = false;
Serial.print("Time: ");
Serial.print(H2);
Serial.print(H1);
Serial.print(":");
Serial.print(M2);
Serial.println(M1);
}
}
void Menu(){
// TABs
if (t1 == HIGH && b1 == LOW){ // LEFT
Serial.println("t1");
b1 = HIGH;
TABc++;
if (TABc <= 0){
TABc = TAB_Amount;
}
if (TABc > TAB_Amount){
TABc = 1;
}
Serial.println("TABc: " && TABc);
delay(TDelay);
}
else if (t1 == LOW) {
b1 = LOW;
}
//---
if (t2 == HIGH && b2 == LOW){ // ENTER
Serial.println("t2");
b2 = HIGH;
isScreenON = true;
TAB = TABc;
if (TABc <= 0){
TABc = TAB_Amount;
}
if (TABc > TAB_Amount){
TABc = 2;
}
Serial.println("TABc: " && TABc);
delay(TDelay);
}
else if (t2 == LOW) {
b2 = LOW;
}
//---
if (t3 == HIGH && b3 == LOW){ // RIGHT
Serial.println("t3");
b3 = HIGH;
TABc--;
if (TABc <= 0){
TABc = TAB_Amount;
}
if (TABc > TAB_Amount){
TABc = 1;
}
Serial.println("TABc: " && TABc);
delay(TDelay);
}
else if (t3 == LOW) {
b3 = LOW;
}
if (TABc == 1){ //
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(60, 100);
img.setTextColor(TFT_WHITE);
img.setTextSize(4);
img.println("Clock");
img.pushSprite(0, 0);
}
else if (TABc == 2){ //
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_WHITE);
img.setTextSize(4);
img.println("Settings");
img.pushSprite(0, 0);
}
else if (TABc == 3){ //
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(60, 82);
img.setTextColor(TFT_WHITE);
img.setTextSize(4);
img.println("Media");
img.setCursor(40, 117);
img.setTextColor(TFT_WHITE);
img.setTextSize(4);
img.print("Control");
img.pushSprite(0, 0);
}
}
void Clock(){
if (t4 == HIGH && b4 == LOW){ // Taster4
Serial.println("t4");
b4 = HIGH;
isScreenON = true;
TAB = 0;
counter = 0;
delay(TDelay);
}
else if (t4 == LOW) {
b4 = LOW;
}
if (isScreenON == true){
counter++;
Serial.println(counter);
}
if (counter >= interval2){ // Standby mode
counter = 0;
isScreenON = false;
img.fillScreen(TFT_BLACK);
img.pushSprite(0, 0);
}
if (t2 == HIGH && b2 == LOW){ // Taster2
Serial.println("t2");
b2 = HIGH;
counter = 0;
isScreenON = true;
delay(TDelay);
}
else if (t2 == LOW) {
b2 = LOW;
}
if (isScreenON == true){
// Clock GUI:
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_WHITE);
img.setTextSize(6);
img.print(H2);
img.print(H1);
img.print(":");
img.print(M2);
img.print(M1);
img.pushSprite(0, 0);
}
}
void Settings(){
if (t4 == HIGH && b4 == LOW){ // Taster4
Serial.println("t4");
TAB = 0;
b4 = HIGH;
delay(TDelay);
}
else if (t4 == LOW) {
b4 = LOW;
}
//---
if (TAB2 == 1 && TAB2open == true){
s1();
}
if (TAB2 == 2 && TAB2open == true){
s2();
}
if (TAB2 == 3 && TAB2open == true){
s3();
}
if (TAB2 == 4 && TAB2open == true){
s4();
}
if (TAB2open == false){
if (t1 == HIGH && b1 == LOW){ // LEFT
Serial.println("t1");
b1 = HIGH;
TABs++;
if (TABs == 0){
TABs = TAB2_Amount;
}
if (TABs > TAB2_Amount){
TABs = 1;
}
Serial.println("TABs: " && TABs);
delay(TDelay);
}
else if (t1 == LOW) {
b1 = LOW;
}
//---
if (t2 == HIGH && b2 == LOW){ // ENTER
Serial.println("t2");
b2 = HIGH;
TAB2 = TABs;
TAB2open = true;
Serial.println("TABs: " && TABs);
delay(TDelay);
}
else if (t2 == LOW) {
b2 = LOW;
}
//---
if (t3 == HIGH && b3 == LOW){ // RIGHT
Serial.println("t3");
b3 = HIGH;
TABs--;
if (TABs == 0){
TABs = TAB2_Amount;
}
if (TABs > TAB2_Amount){
TABs = 1;
}
Serial.println("TABs: " && TABs);
delay(TDelay);
}
else if (t3 == LOW) {
b3 = LOW;
}
if (TABs == 1){
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_RED);
img.setTextSize(6);
img.print(H2);
img.setTextColor(TFT_WHITE);
img.print(H1);
img.print(":");
img.print(M2);
img.print(M1);
img.pushSprite(0, 0);
}
if (TABs == 2){
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_WHITE);
img.setTextSize(6);
img.print(H2);
img.setTextColor(TFT_RED);
img.print(H1);
img.setTextColor(TFT_WHITE);
img.print(":");
img.print(M2);
img.print(M1);
img.pushSprite(0, 0);
}
if (TABs == 3){
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_WHITE);
img.setTextSize(6);
img.print(H2);
img.print(H1);
img.print(":");
img.setTextColor(TFT_RED);
img.print(M2);
img.setTextColor(TFT_WHITE);
img.print(M1);
img.pushSprite(0, 0);
}
if (TABs == 4){
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_WHITE);
img.setTextSize(6);
img.print(H2);
img.print(H1);
img.print(":");
img.print(M2);
img.setTextColor(TFT_RED);
img.print(M1);
img.pushSprite(0, 0);
}
}
if (H1 == 4 && H2 == 2){
H1 = 0;
H2 = 0;
}
}
void MediaControl(){
if (t4 == HIGH && b4 == LOW){ // Taster4
Serial.println("t4");
b4 = HIGH;
isScreenON = true;
TAB = 0;
counter = 0;
delay(TDelay);
}
else if (t4 == LOW) {
b4 = LOW;
}
if(bleKeyboard.isConnected() && TAB == 3) {
if (t1 == HIGH && b1 == LOW){ // LEFT
Serial.println("t1");
b1 = HIGH;
Serial.println("Sending Previous_Track media key...");
bleKeyboard.write(KEY_MEDIA_PREVIOUS_TRACK);
delay(TDelay);
}
else if (t1 == LOW) {
b1 = LOW;
}
if (t2 == HIGH && b2 == LOW){ // Taster2
Serial.println("t2");
b2 = HIGH;
Serial.println("Sending Play/Pause media key...");
bleKeyboard.write(KEY_MEDIA_PLAY_PAUSE);
delay(TDelay);
}
else if (t2 == LOW) {
b2 = LOW;
}
if (t3 == HIGH && b3 == LOW){ // Taster
Serial.println("t3");
b3 = HIGH;
Serial.println("Sending Next_Track media key...");
bleKeyboard.write(KEY_MEDIA_NEXT_TRACK);
delay(TDelay);
}
else if (t3 == LOW) {
b3 = LOW;
}
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_WHITE);
img.setTextSize(3);
img.print("Connected");
img.pushSprite(0, 0);
}
else {
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_WHITE);
img.setTextSize(3);
img.print("Not connected");
img.pushSprite(0, 0);
}
}
void s1(){ // H2
if (t1 == HIGH && b1 == LOW){ // LEFT
Serial.println("t1");
b1 = HIGH;
H2++;
if (H2 > 2){
H2 = 0;
}
if (H2 < 0){
H2 = 2;
}
if (H1 > 4 && H2 == 2){
H1 = 0;
}
if (H1 < 0 && H2 == 2){
H1 = 4;
}
if (H1 > 9 && H2 < 2){
H1 = 0;
}
if (H1 < 0 && H2 < 2){
H1 = 9;
}
Serial.println("H2: " && H2);
delay(TDelay);
}
else if (t1 == LOW) {
b1 = LOW;
}
//---
if (t2 == HIGH && b2 == LOW){ // ENTER
Serial.println("t2");
b2 = HIGH;
TAB2 = TABs;
TAB2open = false;
Serial.println("TABs: " && TABs);
delay(TDelay);
}
else if (t2 == LOW) {
b2 = LOW;
}
//---
if (t3 == HIGH && b3 == LOW){ // RIGHT
Serial.println("t3");
b3 = HIGH;
H2--;
if (H2 > 2){
H2 = 0;
}
if (H2 < 0){
H2 = 2;
}
if (H1 > 4 && H2 == 2){
H1 = 0;
}
if (H1 < 0 && H2 == 2){
H1 = 4;
}
if (H1 > 9 && H2 < 2){
H1 = 0;
}
if (H1 < 0 && H2 < 2){
H1 = 9;
}
Serial.println("TABs: " && TABs);
delay(TDelay);
}
else if (t3 == LOW) {
b3 = LOW;
}
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_GREEN);
img.setTextSize(6);
img.print(H2);
img.setTextColor(TFT_WHITE);
img.print(H1);
img.print(":");
img.print(M2);
img.print(M1);
img.pushSprite(0, 0);
if (H1 == 4 && H2 == 2){
H1 = 0;
H2 = 0;
}
}
void s3(){ // M2
if (t1 == HIGH && b1 == LOW){ // LEFT
Serial.println("t1");
b1 = HIGH;
M2++;
if (M2 > 6){
M2 = 0;
}
if (M2 < 0){
M2 = 6;
}
Serial.println("M2: " && M2);
delay(TDelay);
}
else if (t1 == LOW) {
b1 = LOW;
}
//---
if (t2 == HIGH && b2 == LOW){ // ENTER
Serial.println("t2");
b2 = HIGH;
TAB2open = false;
Serial.println("M2: " && M2);
delay(TDelay);
}
else if (t2 == LOW) {
b2 = LOW;
}
//---
if (t3 == HIGH && b3 == LOW){ // RIGHT
Serial.println("t3");
b3 = HIGH;
M2--;
if (M2 > 6){
M2 = 0;
}
if (M2 < 0){
M2 = 6;
}
Serial.println("M2: " && M2);
delay(TDelay);
}
else if (t3 == LOW) {
b3 = LOW;
}
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_WHITE);
img.setTextSize(6);
img.print(H2);
img.print(H1);
img.print(":");
img.setTextColor(TFT_GREEN);
img.print(M2);
img.setTextColor(TFT_WHITE);
img.print(M1);
img.pushSprite(0, 0);
if (H1 == 4 && H2 == 2){
H1 = 0;
H2 = 0;
}
}
void s2(){ // H1
if (t1 == HIGH && b1 == LOW){ // LEFT
Serial.println("t1");
b1 = HIGH;
H1++;
if (H1 > 4 && H2 == 2){
H1 = 0;
}
if (H1 < 0 && H2 == 2){
H1 = 4;
}
if (H1 > 9 && H2 < 2){
H1 = 0;
}
if (H1 < 0 && H2 < 2){
H1 = 9;
}
Serial.println("H1: " && H1);
delay(TDelay);
}
else if (t1 == LOW) {
b1 = LOW;
}
//---
if (t2 == HIGH && b2 == LOW){ // ENTER
Serial.println("t2");
b2 = HIGH;
TAB2open = false;
Serial.println("TABs: " && TABs);
delay(TDelay);
}
else if (t2 == LOW) {
b2 = LOW;
}
//---
if (t3 == HIGH && b3 == LOW){ // RIGHT
Serial.println("t3");
b3 = HIGH;
H1--;
if (H1 > 4 && H2 == 2){
H1 = 0;
}
if (H1 < 0 && H2 == 2){
H1 = 4;
}
if (H1 > 9 && H2 < 2){
H1 = 0;
}
if (H1 < 0 && H2 < 2){
H1 = 9;
}
Serial.println("H1: " && H1);
delay(TDelay);
}
else if (t3 == LOW) {
b3 = LOW;
}
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_WHITE);
img.setTextSize(6);
img.print(H2);
img.setTextColor(TFT_GREEN);
img.print(H1);
img.setTextColor(TFT_WHITE);
img.print(":");
img.print(M2);
img.print(M1);
img.pushSprite(0, 0);
if (H1 == 4 && H2 == 2){
H1 = 0;
H2 = 0;
}
}
void s4(){ // M1
if (t1 == HIGH && b1 == LOW){ // LEFT
Serial.println("t1");
b1 = HIGH;
M1++;
if (M1 > 9){
M1 = 0;
}
if (M1< 0){
M1 = 9;
}
Serial.println("M1: " && M1);
delay(TDelay);
}
else if (t1 == LOW) {
b1 = LOW;
}
//---
if (t2 == HIGH && b2 == LOW){ // ENTER
Serial.println("t2");
b2 = HIGH;
TAB2open = false;
Serial.println(M1);
delay(TDelay);
}
else if (t2 == LOW) {
b2 = LOW;
}
//---
if (t3 == HIGH && b3 == LOW){ // RIGHT
Serial.println("t3");
b3 = HIGH;
M1--;
if (M1 > 9){
M1 = 0;
}
if (M1< 0){
M1 = 9;
}
Serial.println("M1: " && M1);
delay(TDelay);
}
else if (t3 == LOW) {
b3 = LOW;
}
img.fillScreen(TFT_BLACK);
img.fillCircle(120, 120, 119, TFT_RED);
img.fillCircle(120, 120, 110 , TFT_BLACK);
img.setCursor(30, 100);
img.setTextColor(TFT_WHITE);
img.setTextSize(6);
img.print(H2);
img.print(H1);
img.print(":");
img.print(M2);
img.setTextColor(TFT_GREEN);
img.print(M1);
img.pushSprite(0, 0);
if (H1 == 4 && H2 == 2){
H1 = 0;
H2 = 0;
}
}