Hi there,
I need a bit of help, as I am struggling with the below code:
I have tried several approaches in creating a gfx menu with 4 different icons at the base of an OLED display using the Adafruit libraries and a rotary encoder on an Arduino Pro Micro.
The OLED is connected via I2C and nicely showing the menu. I can switch between the 4 states using the rotary encoder, however I am unable to show the temperature above the icons while in case 0 of the switch case. I tried inserting the LM35(); in switch case 0, but this only briefly shows the temperature.
I now understand that this cannot be done using a switch case. I tried all kinds of if / else / do / while loops, using millis etc, but failed to grasp the concept on how to correctly do this. I still believe this is possible.
So in short, can anyone point me in the right direction on how show the LM35(); temperature together with a menu as shown in the image below?
Here is the code so far, without the LM35(); inserted in case 0: of the witch case.
Thank you all and kind regards,
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
byte mainMenuValue = 0;
byte lastMainMenuValue = 0;
// Setting up the rotary encoder switch
int val;
const int buttonPin = 7;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
// Setting up the rotary encoder
byte encoderPinDT = 6;
byte encoderPinCLK = 8;
byte encoderPos = 0;
byte encoderPosLast = LOW;
byte n = LOW;
// Setting up LM35 temperarure input pin
const byte tempPin = A3;
byte tempConfig = 0;
float tempC;
// Setting op the delay functions
unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
const int interval = 1000;
// The bitmap images for the menus
const unsigned char PROGMEM imgTime [] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x0F, 0xFF, 0xFF, 0xC0, 0x03, 0xFF,
0xFF, 0x07, 0xE0, 0xFF, 0xFE, 0x3F, 0xFC, 0x7F, 0xFC, 0x7E, 0x7E, 0x3F, 0xF8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0xFF, 0xFF, 0xFF,
};
const unsigned char PROGMEM imgTemp [] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0xFF, 0xFF, 0xFF, 0xC0, 0x3F, 0xFF,
0xFF, 0xCF, 0x3F, 0xFF, 0xFF, 0x9F, 0x98, 0x3F, 0xFF, 0x9F, 0x98, 0x3F, 0xFF, 0x9F, 0x9F, 0xFF,
0xFF, 0x99, 0x9F, 0xFF, 0xFF, 0x99, 0x98, 0x3F, 0xFF, 0x99, 0x98, 0x3F, 0xFF, 0x99, 0x9F, 0xFF,
0xFF, 0x99, 0x9F, 0xFF, 0xFF, 0x99, 0x98, 0x3F, 0xFF, 0x99, 0x98, 0x3F, 0xFF, 0x19, 0x8F, 0xFF,
0xFE, 0x39, 0xC7, 0xFF, 0xFE, 0x70, 0xE7, 0xFF, 0xFC, 0x60, 0x63, 0xFF, 0xFC, 0xC0, 0x33, 0xFF,
0xFC, 0xC0, 0x33, 0xFF, 0xFC, 0xC0, 0x33, 0xFF, 0xFC, 0xC0, 0x33, 0xFF, 0xFC, 0x60, 0x63, 0xFF,
0xFE, 0x70, 0xE7, 0xFF, 0xFE, 0x3F, 0xC7, 0xFF, 0xFF, 0x0F, 0x0F, 0xFF, 0xFF, 0x80, 0x1F, 0xFF,
0xFF, 0xE0, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
void setup() {
analogReference(INTERNAL);
pinMode (encoderPinDT, INPUT);
pinMode (encoderPinCLK, INPUT);
pinMode(buttonPin, INPUT);
pinMode(A0, INPUT);
Serial.begin (9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
}
void loop() {
mainMenu();
}
void mainMenu() {
int reading = digitalRead (buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
}
}
}
lastButtonState = reading;
n = digitalRead (encoderPinDT);
if ((encoderPosLast == HIGH) && (n == LOW)) {
if (digitalRead (encoderPinCLK) == LOW) {
if (encoderPos == 0) {
encoderPos = 3;
}
else {
encoderPos--;
}
}
else {
if (encoderPos == 3) {
encoderPos = 0;
}
else {
encoderPos++;
}
}
mainMenuValue = encoderPos;
}
encoderPosLast = n;
if (mainMenuValue != lastMainMenuValue) {
Serial.print ("mainMenuValue = ");
Serial.println (mainMenuValue);
lastMainMenuValue = mainMenuValue;
switch (encoderPos) {
case 0:
display.drawLine(0, 31, 127, 31, WHITE);
display.drawBitmap(0, 32, imgTemp, 32, 31, WHITE);
display.drawBitmap(32, 32, imgSun, 32, 31, BLACK, WHITE);
display.drawBitmap(65, 32, imgTime, 32, 31, BLACK, WHITE);
display.drawBitmap(98, 32, imgDate, 32, 31, BLACK, WHITE);
break;
case 1:
display.drawLine(0, 31, 128, 31, WHITE);
display.drawBitmap(0, 32, imgTemp, 32, 31, BLACK, WHITE)
display.drawBitmap(32, 32, imgSun, 32, 31, WHITE);
display.drawBitmap(65, 32, imgTime, 32, 31, BLACK, WHITE);
display.drawBitmap(98, 32, imgDate, 32, 31, BLACK, WHITE);
break;
case 2:
display.drawLine(0, 31, 128, 31, WHITE);
display.drawBitmap(0, 32, imgTemp, 32, 31, BLACK, WHITE);
display.drawBitmap(32, 32, imgSun, 32, 31, BLACK, WHITE);
display.drawBitmap(65, 32, imgTime, 32, 31, WHITE);
display.drawBitmap(98, 32, imgDate, 32, 31, BLACK, WHITE);
break;
case 3:
display.drawLine(0, 31, 128, 31, WHITE);
display.drawBitmap(0, 32, imgTemp, 32, 31, BLACK, WHITE);
display.drawBitmap(32, 32, imgSun, 32, 31, BLACK, WHITE);
display.drawBitmap(65, 32, imgTime, 32, 31, BLACK, WHITE);
display.drawBitmap(98, 32, imgDate, 32, 31, WHITE);
break;
default:
Serial.println("void mainMenu case error");
break;
}
display.display();
display.clearDisplay();
}
}