My code is here:
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <SPI.h>
#include <Wire.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define CLK 2
#define DT 3
#define SW 4
//Adafruit_SSD1306 display(-1);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir = "";
int ButtonState;
int btn;
//int btn1;
unsigned long lastButtonPress = 0;
int current_val;
int prev_val;
int x1 = 17;
int y1 = 26;
int z1 = 34;
int t1 = 43;
int frame = 0;
void setup() {
// initialize with the I2C addr 0x3C
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Clear the buffer.
display.clearDisplay();
// Set encoder pins as inputs
pinMode(CLK, INPUT_PULLUP);
pinMode(DT, INPUT_PULLUP);
pinMode(SW, INPUT_PULLUP);
// Setup Serial Monitor
Serial.begin(9600);
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
// Call updateEncoder() when any high/low changed seen
// on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
void loop() {
button();
Serial.println(btn);
frame = counter;
if (frame < 0) {
frame = abs(frame % 4);
if (frame == 1) frame = 4;
else if (frame == 2) frame = 3;
else if (frame == 3) frame = 2;
else if (frame == 0) frame = 1;
}
else if (frame < 0 && frame % 4 == 0) frame = 1;
else if (frame > 4 && frame % 4 != 0) frame = frame % 4;
else if (frame > 4 && frame % 4 == 0) frame = 4;
if (btn == 0) {
switch (frame) {
case 0:
frame0();
break;
case 1:
frame1();
break;
case 2:
frame2();
break;
case 3:
frame3();
break;
case 4:
frame4();
break;
}
}
else if (btn == 1) {
switch (frame) {
case 0:
frame0();
btn = 0;
break;
case 1:
subFrame1();
break;
case 2:
subFrame2();
break;
case 3:
subFrame3();
break;
case 4:
subFrame4();
break;
}
}
}
void frame0() {
//display.clearDisplay();
header();
display.setCursor(0, x1); display.print (" Generated matrix");
display.setCursor(0, y1); display.print (" Time delay");
display.setCursor(0, z1); display.print (" Project info");
display.setCursor(0, t1); display.print (" Help");
display.display();
}
void frame1()
{
//display.clearDisplay();
header();
display.setCursor(0, x1); display.print (">Generated matrix");
display.setCursor(0, y1); display.print (" Time delay");
display.setCursor(0, z1); display.print (" Project info");
display.setCursor(0, t1); display.print (" Help");
display.display();
}
void frame2()
{
//display.clearDisplay();
header();
display.setCursor(0, x1); display.print (" Generated matrix");
display.setCursor(0, y1); display.print (">Time delay");
display.setCursor(0, z1); display.print (" Project info");
display.setCursor(0, t1); display.print (" Help");
display.display();
}
void frame3()
{
//display.clearDisplay();
header();
display.setCursor(0, x1); display.print (" Generated matrix");
display.setCursor(0, y1); display.print (" Time delay");
display.setCursor(0, z1); display.print (">Project info");
display.setCursor(0, t1); display.print (" Help");
display.display();
}
void frame4()
{
//display.clearDisplay();
header();
display.setCursor(0, x1); display.print (" Generated matrix");
display.setCursor(0, y1); display.print (" Time delay");
display.setCursor(0, z1); display.print (" Project info");
display.setCursor(0, t1); display.print (">Help");
display.display();
}
void subFrame1()
{
//display.clearDisplay();
header();
display.setCursor(0, x1); display.print (" Generated matrix");
display.setCursor(0, y1); display.print (" Matrix");
display.display();
//duzelt
}
void subFrame2()
{
//display.clearDisplay();
header();
display.setCursor(0, x1); display.print (" Choose time delay");
display.setCursor(0, y1); display.print (" Time delay");
display.display();
//duzelt
}
void subFrame3()
{
//display.clearDisplay();
header();
display.setCursor(0, x1); display.print (" This device generates bla bla bla");
display.display();
}
void subFrame4()
{
//display.clearDisplay();
header();
display.setCursor(0, x1); display.print (" Help");
display.setCursor(0, y1); display.print (" For more information please visit");
display.display();
}
void header() {
display.setTextSize(1); // text size
display.setTextColor(WHITE); // text color
display.setCursor(0, 0); // position to display
display.println("Any header"); // text to display
display.display(); // show on OLED
}
void button() {
ButtonState = digitalRead(SW);
if (ButtonState == 0) { //if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 80) {
Serial.println("Button pressed!");
btn = (btn + 1) % 2;
display.clearDisplay();
}
lastButtonPress = millis();
}
delay(100);
}
void updateEncoder() {
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK !=lastStateCLK && currentStateCLK == 1) {
display.clearDisplay();
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
counter --;
currentDir = "CCW";
} else {
// Encoder is rotating CW so increment
counter ++;
currentDir = "CW";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
}
I want menu item selection like this:
But sometimes I get this:
It selects both items. How can I solve it?
Thanks in advance.