#include<Wire.h>
//#include<SPI.h>//Arduino
//#include<Adafruit_SH1106.h>//Arduino
//Adafruit_SH1106 OLED(-1);//Arduino
#include<Adafruit_GFX.h>//Wokwi
#include<Adafruit_SSD1306.h>//Wokwi
Adafruit_SSD1306 OLED(128,64,&Wire,-1);//Wokwi
#define s1 12
#define s2 11
#define trig 8
#define echo 7
long pul_echo;
float dis;
String mes2;
bool st1=false,st2=false;
void setup() {
OLED.begin(SSD1306_SWITCHCAPVCC,0x3c);//Wokwi
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(s1, INPUT_PULLUP);
pinMode(s2, INPUT_PULLUP);
Serial.println("hello");
}
void loop() {
OLED.clearDisplay();
OLED.setTextColor(WHITE,BLACK);
OLED.setCursor(10,15);
OLED.setTextSize(1);
OLED.print("Select Mode");
OLED.setCursor(10,45);
OLED.setTextSize(1);
OLED.print("Sw1 : cm");
OLED.setCursor(65,45);
OLED.setTextSize(1);
OLED.print("Sw2 : Inch");
OLED.drawRect(2,2,126,62,WHITE);
OLED.drawLine(2,32,126,32,WHITE);
if(digitalRead(s1)==0){
while(digitalRead(s1)==0){delay(10);}
st1=true;
st2=false;
}
if(digitalRead(s2)==0){
while(digitalRead(s2)==0){delay(10);}
st2=true;
st1=false;
}
if(st1==true){
digitalWrite(trig,0);
delay(10);
digitalWrite(trig,1);
delayMicroseconds(10);
digitalWrite(trig,0);
pul_echo = pulseIn(echo,1);
dis = (pul_echo/2.0)/29.2;
mes2 = "Distance = "+String(dis)+"\tcm";
Serial.println(mes2);
OLED.setCursor(10,45);
OLED.setTextSize(1);
OLED.print(" ");
OLED.setCursor(10,45);
OLED.setTextSize(0.5);
OLED.print("Dis= ");
OLED.print(dis);
OLED.print(" CM");
OLED.display();
}
if(st2==true){
digitalWrite(trig,0);
delay(10);
digitalWrite(trig,1);
delayMicroseconds(10);
digitalWrite(trig,0);
pul_echo = pulseIn(echo,1);
dis = ((pul_echo/2.0)/29.0)/2.54;
mes2 = "Distance = "+String(dis)+"\tinch";
Serial.println(mes2);
OLED.setCursor(10,45);
OLED.setTextSize(1);
OLED.print(" ");
OLED.setCursor(10,45);
OLED.setTextSize(0.5);
OLED.print("Dis= ");
OLED.print(dis);
OLED.print(" Inch");
OLED.display();
}
OLED.display();
}