Hi! I made a thermostat with an oled display and I would like to have a start screen where you need to press a button for it to move onto the main loop and start heating up. I am confused how to do this, I tried using a while loop as well as a void but have not been sucessful. If anyone could help that would be appreciated. Ive attached the code im working on ![]()
#include<Wire.h> //I2C com for base and OLED
#include <EEPROM.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define LOGO16_GLCD_HEIGHT 32 //oled height
#define LOGO16_GLCD_WIDTHÂ 128 //oled width
static const unsigned char PROGMEM boot[] = {
0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x1E, 0x08,
0x00, 0x00, 0x07, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x1E, 0x08,
0x00, 0x00, 0xFF, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x1E, 0x08,
0x00, 0x03, 0xFF, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xBE, 0x3E,
0x00, 0x07, 0xFF, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFE, 0x36,
0x00, 0x07, 0xFF, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0xE2, 0xFF,
0x00, 0x07, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x42, 0xC1,
0x00, 0x07, 0xFF, 0xF8, 0x78, 0x01, 0xF0, 0x3C, 0x39, 0xFF, 0x9C, 0x1C, 0x00, 0x08, 0x02, 0xC1,
0x00, 0x07, 0xFF, 0xF0, 0x78, 0x01, 0xF0, 0x3C,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81, 0xFF, 0xFC, 0x7F, 0x9E, 0x0F, 0x3C, 0x79, 0xFF, 0x83, 0xE6, 0x00, 0x07, 0xE0, 0x18,
0x0F, 0x81, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xCE, 0x00, 0x00, 0x00, 0x00,
0x0F, 0xC3, 0xFC, 0x00, 0x00, 0x07, 0xFF, 0xFC, 0x00, 0x00, 0x07, 0xCE, 0x00, 0x00, 0x00, 0x00,
0x0F, 0xEF, 0xF8, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFE, 0x00, 0x0F, 0x86, 0x00, 0x00, 0x00, 0x00,
0x0F, 0xFF, 0xF8, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x06, 0x00, 0x00, 0x00, 0x00,
0x0F, 0xFF, 0xF0, 0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x06, 0x00, 0x00, 0x00, 0x00,
0x07, 0xFF, 0xF0, 0x00, 0x0F, 0xFF, 0xC1, 0xFF, 0xFF, 0xFF, 0xFC, 0x06, 0x00, 0x00, 0x00, 0x00,
0x07, 0xFF, 0xF0, 0x00, 0x0F, 0xFF, 0xC1, 0xFF, 0xFF, 0xFF, 0xFC, 0x06, 0x00, 0x00, 0x00, 0x00,
0x03, 0xFF, 0xE0, 0x00, 0x0F, 0xF0, 0x00, 0x00, 0x3F, 0xFF, 0xF0, 0x0F, 0x00, 0x00, 0x00, 0x00,
0x01, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00,
0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#if (SSD1306_LCDHEIGHT != 32)
#error("Display Error.");
#endif
int temperatureWrite; //Global Temperature Setting
byte tempSave; //written to eeprom
int thermister = 0;
const int pwm = 5;
const int buttonPin = 6;//button!!!!!!!!!!!!
int buttonState = 0;
//Voltage divider
int raw = 0;
int Vin = 4;
float Vout = 0;
float R1 = 100;
float R2 = 0; //calculate this
int temperatureRead; //then solve for this (y=mx+b)
float buffer = 0;
//PID controller
float PID_error;
float previous_error;
float Totaltime, Time, Previoustime;
int PID_value = 0;
int temperatureBuffer;
int P = 20.0; int PID_P = 0;
int I = 0.3; int PID_I = 0;
int D = 2.0; int PID_D = 0;
void setup()
{
 //Display & UI Boot Up
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //OLED I2C address is 0x32
 display.clearDisplay();
 display.drawBitmap(0, 0, boot, 128, 32, 1);
 display.display();
 Serial.begin(9600);
 pinMode(pwm, OUTPUT);
 pinMode(buttonPin, INPUT);
tempSave = EEPROM.read(0); //read eeprom and assign to variable
delay(6000); //Logo Delay
 display.clearDisplay();
}
void loop()
{
Â
 int val = analogRead(0);
temperatureWrite = tempSave*25;
//temperature graph calculation
 raw = analogRead(thermister);
 buffer = raw * Vin;
 Vout = (buffer) / 1024.0;
 buffer = (Vin / Vout) - 1;
 R2 = R1 * buffer;
 //y=5(x)-220 math sucks
 temperatureRead = (5 * R2) - 220;
 //Calc P & I
 PID_error = temperatureRead - temperatureWrite + 10;
 PID_P = P * PID_error;
 if (-1 < PID_error < 1)
 {
  PID_I = PID_I + (I * PID_error);
 }
 //Calculate D
 Previoustime = Time;
 Time = millis();
 Totaltime = (Time - Previoustime) / 1000;
 PID_D = D * ((PID_error - previous_error) / Totaltime);
 PID_value = PID_P + PID_I + PID_D;
 //auto adjust
 if (PID_value < 0) {
  PID_value = 0;
 }
 if (PID_value > 255) {
  PID_value = 255;
 }
Â
 buttonState = digitalRead(buttonPin);
 if (buttonState == LOW) {
  display.clearDisplay();
  display.setCursor(20, 10);
  display.setTextSize(2);
 Â
  //display.drawBitmap(0, 0, boot, 128, 32, 1);
 display.display();
  display.display();
  delay(0);
  temperatureWrite = temperatureWrite + 25;
  display.clearDisplay();
  display.println(temperatureWrite);
  tempSave = temperatureWrite/25;
  EEPROM.update(0, tempSave);
 Â
 }
Â
if (tempSave > 20 && temperatureWrite > 500) { //set max temp
 display.clearDisplay();
 temperatureWrite = 0;
 tempSave = 0;
 display.setCursor(20, 10);
 display.setTextSize(2);
 display.println("OFF X.X");
  display.display();
 delay(1770);
 display.clearDisplay();
Â
 }
 if (temperatureWrite > 1 && temperatureWrite < 26) {
  temperatureWrite = 100;//set min temp
  tempSave = 4;
Â
}
//normal display data
 display.setTextSize(2);
 display.setTextColor(WHITE);
 display.setCursor(0, 12);
 display.println(temperatureRead);
 display.setCursor(40, 12);
 display.setTextSize(2);
 display.println("C");
 display.setCursor(65, 12);
 display.println(temperatureWrite);
 display.setCursor(68, 3);
 display.setTextSize(1);
 display.println("TARGET");
 display.display();
 delay(0);
 display.clearDisplay();
 analogWrite(pwm, 255 - PID_value);
 previous_error = PID_error;
delay(0);
}