Niloy007

Arduino code

May 22nd, 2021
661
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Adafruit_NeoPixel.h>
  2. #include<LiquidCrystal.h>
  3. #include <Servo.h>
  4. Servo myservo;
  5.  
  6. int ledPin= 3;
  7. int ledNo= 12;
  8.  
  9. Adafruit_NeoPixel strip= Adafruit_NeoPixel(ledNo,ledPin,NEO_RGB+NEO_KHZ800);
  10.  
  11. LiquidCrystal lcd(12, 11, 5, 4, 9, 7);
  12.  
  13. int buzzerPin= 2;
  14. int echoPin= 6;
  15. int trigPin= 5;
  16. int minDistance = 100;
  17. int maxDistance = 300;
  18.  
  19. const int servo = 5;
  20. const int servoTrigPin = 10;
  21. const int servoEchoPin = 13;
  22.  
  23. long duration;
  24. int distance;
  25. int pos = 0;
  26.  
  27. void setup()
  28. {
  29.   lcd.begin(16, 2);
  30.   pinMode(buzzerPin, OUTPUT);
  31.   pinMode(trigPin, OUTPUT);
  32.   pinMode(echoPin, INPUT);
  33.   strip.begin();
  34.   for(int i = 0; i < ledNo; i++)
  35.   {
  36.    strip.setPixelColor(i,strip.Color(0,0,0));
  37.   }
  38.   strip.show();
  39.   pinMode(servoTrigPin, OUTPUT);
  40.   pinMode(servoEchoPin, INPUT);
  41.   myservo.attach(servo);
  42.   myservo.write(0);
  43.   Serial.begin(9600);
  44. }
  45.  
  46.  
  47. void socialDistance() {
  48.   int distance = calcDistance();
  49.   Serial.println(distance);
  50.   int ledsToGlow = map(distance, minDistance, maxDistance, ledNo, 1);
  51.   Serial.println(ledsToGlow);
  52.   if(ledsToGlow == 12)
  53.   {
  54.     digitalWrite(buzzerPin, HIGH);
  55.   }
  56.   else
  57.   {
  58.     digitalWrite(buzzerPin, LOW);
  59.   }
  60.   for(int i = 0; i < ledsToGlow; i++)
  61.   {
  62.     if(i < 4)
  63.     {
  64.       strip.setPixelColor(i,strip.Color(50,0,0));//green,red,blue
  65.     }
  66.     else if(i >= 4 && i < 8)
  67.     {
  68.       strip.setPixelColor(i,strip.Color(50,0,50));//green,red,blue
  69.     }
  70.     else if(i >= 8 && i < 12)
  71.     {
  72.       strip.setPixelColor(i,strip.Color(0,50,0));//green,red,blue
  73.     }
  74.   }
  75.   for(int i = ledsToGlow; i < ledNo; i++)
  76.   {
  77.     strip.setPixelColor(i,strip.Color(0,0,0));
  78.   }
  79.   strip.show();
  80.   delay(50);
  81. }
  82.  
  83.  
  84. void LED() {
  85.   lcd.setCursor(0,0);
  86.   lcd.print ("Unsafe Distance ");
  87.   delay (100);
  88.   lcd.setCursor(0,1);
  89.   lcd.print ("Be 3 Feet Apart!");
  90.   delay (100);
  91.   lcd.setCursor (0,0);
  92.   lcd.print ("COVID must stop!");
  93.   delay (500);
  94.   lcd.setCursor (0,1);
  95.   lcd.print ("Wash Your HAND!!");
  96.   delay (500);
  97. }
  98.  
  99. void handWash()
  100. {
  101.   digitalWrite(servoTrigPin, LOW);
  102.   delayMicroseconds(2);
  103.   digitalWrite(servoTrigPin, HIGH);
  104.   delayMicroseconds(10);
  105.   digitalWrite(servoTrigPin, LOW);
  106.   duration = pulseIn(servoEchoPin, HIGH);
  107.   distance = duration * 0.034 / 2;
  108.   Serial.print("Distance: ");
  109.   Serial.println(distance);
  110.  
  111.   if (distance < 15) {
  112.     myservo.write(45);
  113.     delay(100);
  114.     myservo.write(90);
  115.     delay(100);
  116.     myservo.write(135);
  117.     delay(100);
  118.     myservo.write(180);
  119.     delay(1000);
  120.     myservo.write(0);
  121.     delay(1000);
  122.   }
  123. }
  124.  
  125. void loop()
  126. {
  127.   socialDistance();
  128.   LED();
  129.   handWash();
  130. }
  131.  
  132. int calcDistance()
  133. {
  134.   long distance,duration;
  135.   digitalWrite(trigPin, LOW);
  136.   delayMicroseconds(2);
  137.   digitalWrite(trigPin, HIGH);
  138.   delayMicroseconds(10);
  139.   digitalWrite(trigPin, LOW);
  140.   duration = pulseIn(echoPin, HIGH);
  141.  
  142.   distance = duration/29/2;
  143.   if(distance >= maxDistance)
  144.   {
  145.     distance = maxDistance;
  146.   }
  147.   if(distance <= minDistance)
  148.   {
  149.     distance = minDistance;
  150.   }
  151.   return distance;
  152. }
RAW Paste Data