// #include<LiquidCrystal.h>
// LiquidCrystal lcd (2, 3, 4, 5, 6, 7);/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define ldr 4
int val;
int val2;
String duration;
void testfillrect(void) {
display.clearDisplay();
for(int16_t i=0; i<display.height()/2; i+=3) {
// The INVERSE color is used so rectangles alternate white/black
display.fillRect(i, i, display.width()-i*2, display.height()-i*2, SSD1306_INVERSE);
display.display(); // Update screen with each newly-drawn rectangle
delay(1);
}
delay(2000);
}
void displayInfo(String oledText) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.println(F("LiFi Project"));
// Add some spacing
display.println();
display.println();
display.setTextSize(2); // 2X-scale text
display.setTextColor(SSD1306_WHITE);
// Display the message
display.print("MSG: ");
display.println(oledText); // Display the message text
display.display();
}
void setup() {
// put your setup code here, to run once:
pinMode(ldr, INPUT_PULLUP);
Wire.begin();
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Draw a single pixel in white
display.drawPixel(10, 10, SSD1306_WHITE);
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(2); // Adjust text size as needed
display.setTextColor(SSD1306_WHITE);
testfillrect();
displayInfo("Welcome");
}
void loop() {
int val = digitalRead(ldr);
while(val == 0)
{
int val2 = digitalRead(ldr);
duration += val2;
Serial.println(duration);
if(duration == "001")
{
Serial.println("Received message: hi");
// lcd.clear();
// lcd.print("hi");
displayInfo("hi");
}
if(duration == "0001")
{
Serial.println("Received message: hello");
// lcd.clear();
// lcd.print("hello");
displayInfo("hello");
}
if(duration == "00001")
{
Serial.println("Received message: how are you?");
// lcd.clear();
// lcd.print("how are you?");
displayInfo("how are you?");
}
if(duration == "000001")
{
Serial.println("Received message: I am fine");
// lcd.clear();
// lcd.print("I am fine");
displayInfo("I am fine");
}
if(duration == "0000001")
{
Serial.println("Received message: ok");
// lcd.clear();
// lcd.print("ok");
displayInfo("ok");
}
if(duration == "00000001")
{
Serial.println("Received message: good morning");
// lcd.clear();
// lcd.print("good morning");
displayInfo("good morning");
}
if(duration == "000000001")
{
Serial.println("Received message: good afternoon");
// lcd.clear();
// lcd.print("good afternoon");
displayInfo("good afternoon");
}
if(duration == "0000000001")
{
Serial.println("Received message: good evening");
// lcd.clear();
// lcd.print("good evening");
displayInfo("good evening");
}
if(duration == "00000000001")
{
Serial.println("Received message: thank you");
// lcd.clear();
// lcd.print("thank you");
displayInfo("thank you");
}
if(duration == "000000000001")
{
Serial.println("Received message: sorry");
// lcd.clear();
// lcd.print("sorry");
displayInfo("sorry");
}
if(val2 == 1)
{
duration = "";
break;
}
delay(200);
}
}