/* Arduino UNO with SSD1306 128x64 I2C OLED display
* Buttons and function test
* Martijn May 2024
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Define button pins
#define UP_BUTTON 46
#define DOWN_BUTTON 50
#define LEFT_BUTTON 52
#define RIGHT_BUTTON 48
//Define menu counts
#define menuoptions 4
#define menuitemlen 15
#define lcd_lines 2
const menuitem menu_lines[menuoptions]=
{{"First menu lin", 0, 1},
{"Second menu li", 0, lcd_lines},
{"Third menu lin", 1, lcd_lines},
{"Fourth menu li", 2, lcd_lines}};
menuitem *menu_lines_ptr;
signed char menu_position;
char user_choice;
int pressed;
void show() { // Often used sequence - Function to simplify code
display.display(); delay(2000); display.fillScreen(SSD1306_BLACK);
}
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Old Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Initialise variables
int x0 = 5; // 3 pairs of co-ordinates
int y0 = 5;
int x1 = 120;
int y1 = 23;
int x2 = 55;
int y2 = 60;
int w = 105; // width
int h = 55; // height
int r = 25; // radius
int cw = SSD1306_WHITE; // colour white
int cb = SSD1306_BLACK; // colour black
int wait = 2000; // 2 second delay
// Title screen
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(cw);
display.setCursor(00,00);
display.println("HOME");
display.setCursor(33,00);
display.println("MEN1");
display.setCursor(66,00);
display.println("MEN2");
display.setCursor(99,00);
display.println("MEN3");
display.display(); delay(wait);
}
void loop() {
// put your main code here, to run repeatedly:
//Button state
{
bool update_needed = false;
unsigned long time = millis();
static bool up_state = false;
static bool down_state = false;
static bool left_state = false;
static bool right_state = false;
up_state |= (digitalRead(UP_BUTTON) == LOW);
down_state |= (digitalRead(DOWN_BUTTON) == LOW);
left_state |= (digitalRead(LEFT_BUTTON) == LOW);
right_state |= (digitalRead(RIGHT_BUTTON) == LOW);
}
if(digitalRead(1) == true){
//vul hier de code in die moet draaien wanneer de knop niet is ingedrukt
}else{
//vul hier de code in die moet draaien wanneer de knop wel is ingedrukt
}
{
pressed = Number_of_Button();
if (pressed > 0) user_choice = User_Menu(pressed);
switch (user_choice)
{
case 1:
//actie 1
break;
case 2:
//actie 2
break;
case 3:
//actie 3
break;
case 4:
//actie 4
break;
}
}