Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal_SR_LCD3.h>
- #include <EEPROM.h>
- const int PIN_LCD_STROBE = 2;
- const int PIN_LCD_DATA = 3;
- const int PIN_LCD_CLOCK = 4;
- LiquidCrystal_SR_LCD3 lcd(PIN_LCD_DATA, PIN_LCD_CLOCK, PIN_LCD_STROBE);
- const int trigPin = 8;
- const int echoPin = 9;
- int button=7, push=0, ok=0;
- int buzzer=11;
- int led=6, led1=12, led2=13;
- void setup() {
- // initialize serial communication:
- Serial.begin(9600);
- pinMode(button, INPUT);
- pinMode(buzzer, OUTPUT);
- pinMode(led, OUTPUT);
- pinMode(led1, OUTPUT);
- pinMode(led2, OUTPUT);
- analogWrite(led, 0);
- lcd.begin(16,2);
- lcd.home();
- }
- void loop()
- {
- // establish variables for duration of the ping,
- // and the distance result in inches and centimeters:
- long duration, inches, cm;
- // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
- // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
- pinMode(trigPin, OUTPUT);
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Read the signal from the sensor: a HIGH pulse whose
- // duration is the time (in microseconds) from the sending
- // of the ping to the reception of its echo off of an object.
- pinMode(echoPin, INPUT);
- duration = pulseIn(echoPin, HIGH);
- // convert the time into a distance
- inches = microsecondsToInches(duration);
- cm = microsecondsToCentimeters(duration);
- while(digitalRead(button)==LOW){
- if(push==0 && ok==0){
- push=1;
- }else if(push==1 && ok==1){
- push=2;
- }else if(push==2 && ok==2){
- push=3;
- }else if(push==3 && ok==3){
- digitalWrite(led, LOW);
- digitalWrite(led1, LOW);
- digitalWrite(led2, LOW);
- push=4;
- }else if(push==4 && ok==4){
- push=1;
- }
- }
- if(push==0){
- ok=0;
- }
- if(push==1){
- digitalWrite(led, 0);
- ok=1;
- Serial.print(inches);
- Serial.print("in, ");
- Serial.print(cm);
- Serial.print("cm");
- Serial.println();
- lcd.print(cm);
- lcd.setCursor(5,0);
- lcd.print(" CM");
- delay(100);
- lcd.clear();
- }else if(push==2){
- ok=2;
- lcd.print(cm);
- lcd.setCursor(5,0);
- lcd.print(" CM");
- if(cm < 10){
- digitalWrite(buzzer, HIGH);
- lcd.setCursor(0,1);
- lcd.print(" BUZZER : ON");
- }else{
- digitalWrite(buzzer, LOW);
- lcd.setCursor(0,1);
- lcd.print(" BUZZER : OFF");
- }
- delay(100);
- lcd.clear();
- }else if(push==3){
- ok=3;
- lcd.print(cm);
- lcd.setCursor(5,0);
- lcd.print(" CM");
- int val = 255-(cm);
- if(val < 0){
- val=0;
- }
- Serial.println(val);
- analogWrite(led, val);
- delay(100);
- lcd.clear();
- }else if(push==4){
- ok=4;
- Serial.println(cm);
- lcd.print(cm);
- lcd.setCursor(5,0);
- lcd.print(" CM");
- if(cm < 10){
- lcd.setCursor(0,1);
- lcd.print("1");
- digitalWrite(led, HIGH);
- digitalWrite(led1, LOW);
- digitalWrite(led2, LOW);
- digitalWrite(buzzer, HIGH);
- delay(1000);
- digitalWrite(buzzer, LOW);
- }else if(cm < 20 && cm > 10){
- lcd.setCursor(2,1);
- lcd.print("1");
- digitalWrite(led, LOW);
- digitalWrite(led1, HIGH);
- digitalWrite(led2, LOW);
- }else if(cm < 30 && cm > 20){
- lcd.setCursor(4,1);
- lcd.print("1");
- digitalWrite(led, LOW);
- digitalWrite(led1, LOW);
- digitalWrite(led2, HIGH);
- }else if(cm > 40){
- digitalWrite(led, LOW);
- digitalWrite(led1, LOW);
- digitalWrite(led2, LOW);
- }
- delay(100);
- lcd.clear();
- digitalWrite(buzzer, LOW);
- }
- }
- long microsecondsToInches(long microseconds)
- {
- return microseconds / 74 / 2;
- }
- long microsecondsToCentimeters(long microseconds)
- {
- return microseconds / 29 / 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement