Skip to content

Instantly share code, notes, and snippets.

@rafitc
Last active May 4, 2017 08:55
Show Gist options
  • Save rafitc/fe049fb3b5018526e6698e92c2bcdb3c to your computer and use it in GitHub Desktop.
Save rafitc/fe049fb3b5018526e6698e92c2bcdb3c to your computer and use it in GitHub Desktop.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
#define lenght 16.0
unsigned int peace;
unsigned char b;
int count=0;
int aft=1;
int buz = 6;
int motor = 7; //motor switch
int trigPin = 8; //Trig - green Jumper
int echoPin = 9; //Echo - yellow Jumper
int duration, cm, inches;
//------------ //Two Function
void buzLow();
void buzFull();
//------------
byte p1[8] = {
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10};
byte p2[8] = {
0x18,
0x18,
0x18,
0x18,
0x18,
0x18,
0x18,
0x18};
byte p3[8] = {
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C};
byte p4[8] = {
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E};
byte p5[8] = {
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F};
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motor, OUTPUT);
pinMode(buz, OUTPUT);
lcd.createChar(0, p1);
lcd.createChar(1, p2);
lcd.createChar(2, p3);
lcd.createChar(3, p4);
lcd.createChar(4, p5);
lcd.begin(16, 2);
}
void loop()
{
delayMicroseconds(5); //Sensor
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2) / 29.1; // convert the time into a distance
int perc = ((100.-cm)/100.)*100.; //percentage
lcd.setCursor(0, 0); //lcd
lcd.print(perc);
lcd.setCursor(2, 0);
lcd.print('%');
lcd.setCursor(0, 1);
double a=lenght/100*perc;
// drawing black rectangles on LCD
if (a>=1)
{
for (int i=1;i<a;i++)
{
lcd.write(4);
b=i;
}
a=a-b;
}
peace=a*5;
switch (peace)
{
case 0:
break;
case 1:
lcd.print((char)0);
break;
case 2:
lcd.write(1);
break;
case 3:
lcd.write(2);
break;
case 4:
lcd.write(3);
break;
}
//clear
for (int i =0;i<(lenght-b);i++)
{
lcd.print(" ");
}
;
//Motor On
if (perc< 15)
{
digitalWrite(motor, HIGH);
lcd.setCursor(13, 0);
lcd.print("on");
while(count<=5)
{
buzLow();
count++;
}
}
//motor off
if(perc> 80)
{
lcd.setCursor(13, 0);
lcd.print("off");
digitalWrite(motor, LOW);
while(a<=5)
{
buzFull();
a++;
}
}
}
//////
void buzLow()
{
digitalWrite(buz, HIGH);
delay(500);
digitalWrite(buz, LOW);
delay(500);
}
void buzFull()
{
digitalWrite(buz, HIGH);
delay(500);
digitalWrite(buz, LOW);
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment