#include <ezButton.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH,SCREEN_HEIGHT,&Wire,-1);
ezButton button1(13);
ezButton button2(33);
ezButton button3(14);
ezButton button4(27);
ezButton button5(26);
bool goingup=true;
int currentfloor=1;
int buttonstate=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
button1.setDebounceTime(25);
button2.setDebounceTime(25);
button3.setDebounceTime(25);
button4.setDebounceTime(25);
button5.setDebounceTime(25);
if(!oled.begin(SSD1306_SWITCHCAPVCC,0x3c)){
Serial.println(F("failed to start SSD1306 Oled"));
for (;;);
}
delay(2000);
oled.clearDisplay();
displayMessage();
oled.clearDisplay();
Serial.println("Hello, ESP32!");
}
void loop() {
// put your main code here, to run repeatedly:
button1.loop();
button2.loop();
button3.loop();
button4.loop();
button5.loop();
if(button1.isPressed()){
Serial.println("button 1 is pressed");
if(currentfloor>1){
movedown(1,1);
}
}
else if(button2.isPressed()){
Serial.println("button 2 is pressed");
if(currentfloor<2){
moveup(2,25);
}
else if(currentfloor>2){
movedown(2,25);
}
}
else if(button3.isPressed()){
Serial.println("button 3 is pressed");
if(currentfloor<3){
moveup(3,50);
}
else if(currentfloor>3){
movedown(3,50);
}
}
else if(button4.isPressed()){
Serial.println("button 4 is pressed");
if(currentfloor<4){
moveup(4,75);
}
else if(currentfloor>4){
movedown(4,75);
}
}
else if(button5.isPressed()){
Serial.println("button 5 is pressed");
if(currentfloor<5){
moveup(5,100);
}
else if(currentfloor>5){
movedown(5,100);
}
}
}
void movedown(int floorno,int cursorposition){
while(currentfloor>=floorno){
oled.setTextSize(5);
oled.setTextColor(WHITE);
oled.setCursor(20,20);
oled.print(currentfloor);
oled.display();
Serial.println("going down");
oled.setCursor(60,15);
oled.write(25);
oled.display();
delay(1000);
currentfloor--;
oled.clearDisplay();
}
currentfloor=floorno;
displayMessage();
}
void moveup(int floorno,int cursorposition){
while(currentfloor<=floorno){
oled.setTextSize(5);
oled.setTextColor(WHITE);
oled.setCursor(20,20);
oled.print(currentfloor);
oled.display();
Serial.println("going up");
oled.setCursor(60,15);
oled.write(25);
oled.display();
delay(1000);
currentfloor++;
oled.clearDisplay();
}
currentfloor=floorno;
displayMessage();
}
void displayMessage(){
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(2,5);
oled.println("press");
oled.display();
oled.setCursor(2,25);
oled.println("floor");
oled.display();
oled.setCursor(2,45);
oled.println("number");
oled.display();
delay(1000);
oled.clearDisplay();
}