#include <Wire.h>
#include <U8g2lib.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // initialization for the used OLED display
// 'icons8-bitcoin-24', 24x24px
const unsigned char bitcoinIcon [] PROGMEM =
{
0x00, 0x7e, 0x00, 0x03, 0xff, 0xc0, 0x07, 0x81, 0xe0, 0x0e, 0x00, 0x70, 0x18, 0x28, 0x18, 0x30,
0x28, 0x0c, 0x70, 0xfc, 0x0e, 0x60, 0xfe, 0x06, 0x60, 0xc7, 0x06, 0xc0, 0xc3, 0x03, 0xc0, 0xc7,
0x03, 0xc0, 0xfe, 0x03, 0xc0, 0xff, 0x03, 0xc0, 0xc3, 0x83, 0xc0, 0xc1, 0x83, 0x60, 0xc3, 0x86,
0x60, 0xff, 0x06, 0x70, 0xfe, 0x0e, 0x30, 0x28, 0x0c, 0x18, 0x28, 0x18, 0x0e, 0x00, 0x70, 0x07,
0x81, 0xe0, 0x03, 0xff, 0xc0, 0x00, 0x7e, 0x00
};
int i = 0;
char buffer1[32];
char buffer2[32];
#define ENC_A 4
#define ENC_B 5
unsigned long _lastIncReadTime = micros();
unsigned long _lastDecReadTime = micros();
int _pauseLength = 25000;
int _fastIncrement = 10;
volatile int counter = 0;
#define LED1 2
#define pushButton 6
int SS1 = 0;
void setup()
{
pinMode(pushButton, INPUT);
pinMode(LED1, OUTPUT);
// Set encoder pins and attach interrupts
pinMode(ENC_A, INPUT_PULLUP);
pinMode(ENC_B, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENC_A), read_encoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENC_B), read_encoder, CHANGE);
Serial.begin(115200);
u8g2.begin();
}
void loop()
{
static int lastCounter = 0;
// If count has changed print the new value to serial
if(counter != lastCounter)
{
Serial.println(counter);
lastCounter = counter;
}
SS1 = digitalRead(pushButton);
if(counter == 1)
{
void menu1();
}
else if (counter == 2)
{
void menu2();
}
else
{
void intro();
}
if(counter > 2)
{
counter = 0;
}
}
void read_encoder()
{
// Encoder interrupt routine for both pins. Updates counter
// if they are valid and have rotated a full indent
static uint8_t old_AB = 3; // Lookup table index
static int8_t encval = 0; // Encoder value
static const int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; // Lookup table
old_AB <<=2; // Remember previous state
if (digitalRead(ENC_A)) old_AB |= 0x02; // Add current state of pin A
if (digitalRead(ENC_B)) old_AB |= 0x01; // Add current state of pin B
encval += enc_states[( old_AB & 0x0f )];
// Update counter if encoder has rotated a full indent, that is at least 4 steps
if( encval > 3 ) { // Four steps forward
int changevalue = 1;
if((micros() - _lastIncReadTime) < _pauseLength) {
changevalue = _fastIncrement * changevalue;
}
_lastIncReadTime = micros();
counter = counter + changevalue; // Update counter
encval = 0;
}
else if( encval < -3 ) { // Four steps backward
int changevalue = -1;
if((micros() - _lastDecReadTime) < _pauseLength) {
changevalue = _fastIncrement * changevalue;
}
_lastDecReadTime = micros();
counter = counter + changevalue; // Update counter
encval = 0;
}
}
void intro()
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setBitmapMode(1);
u8g2.setFontMode(1);
u8g2.drawFrame(10, 30, 106, 17);
u8g2.drawBox(12, 32, i, 13);
u8g2.setFont(u8g2_font_4x6_tr);
sprintf(buffer1, "Counter: %d",counter);
u8g2.drawStr(5, 8, buffer1);
u8g2.drawLine(2, 11, 125, 11);
u8g2.setFont(u8g2_font_5x8_tr);
sprintf(buffer2, "Intro: %d%%",i);
u8g2.drawStr(11, 57, buffer2);
if(i > 100)
{
i = 0;
}
i++;
u8g2.sendBuffer(); // transfer internal memory to the display
delay(10); // one second delay, but we are only displaying a static image, so it makes no difference
SS1 = digitalRead(pushButton);
if(SS1 == 1)
{
void intro();
}
}
void menu1()
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setBitmapMode(1);
u8g2.setFontMode(1);
u8g2.drawFrame(10, 30, 106, 17);
u8g2.drawBox(12, 32, i, 13);
u8g2.setFont(u8g2_font_4x6_tr);
sprintf(buffer1, "Menu 1 Counter: %d",counter);
u8g2.drawStr(5, 8, buffer1);
u8g2.drawLine(2, 11, 125, 11);
u8g2.setFont(u8g2_font_5x8_tr);
sprintf(buffer2, "Progress 2: %d%%",i);
u8g2.drawStr(11, 57, buffer2);
if(i > 100)
{
i = 0;
}
i++;
u8g2.sendBuffer(); // transfer internal memory to the display
delay(10); // one second delay, but we are only displaying a static image, so it makes no difference
SS1 = digitalRead(pushButton);
if(SS1 == 1)
{
void execute1();
}
}
void menu2()
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setBitmapMode(1);
u8g2.setFontMode(1);
u8g2.drawFrame(10, 30, 106, 17);
u8g2.drawBox(12, 32, i, 13);
u8g2.setFont(u8g2_font_4x6_tr);
sprintf(buffer1, "Menu 2 Counter: %d",counter);
u8g2.drawStr(5, 8, buffer1);
u8g2.drawLine(2, 11, 125, 11);
u8g2.setFont(u8g2_font_5x8_tr);
sprintf(buffer2, "Progress 2: %d%%",i);
u8g2.drawStr(11, 57, buffer2);
if(i > 100)
{
i = 0;
}
i++;
u8g2.sendBuffer(); // transfer internal memory to the display
delay(10); // one second delay, but we are only displaying a static image, so it makes no difference
SS1 = digitalRead(pushButton);
if(SS1 == 1)
{
void execute2();
}
}
void execute1()
{
digitalWrite(LED1, HIGH);
delay(2000);
digitalWrite(LED1, LOW);
delay(2000);
digitalWrite(LED1, HIGH);
delay(2000);
digitalWrite(LED1, LOW);
}
void execute2()
{
digitalWrite(LED1, HIGH);
delay(2000);
digitalWrite(LED1, LOW);
}