Advertisement
itsJimmyXO

Project_V4

Nov 11th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. BH-MahmoudGHussein
  2. // Declerations
  3. // Constants
  4. const int trig = 7;
  5. const int echo = 8;
  6. const int buz = 6;
  7. // Variables
  8. unsigned int dist;
  9. long dua;
  10. int brightness;
  11. int ldr = A0;
  12. int x;
  13.  
  14.  
  15. void setup()
  16. {
  17.       Serial.begin(9600); // initialization.
  18.       pinMode(buz, OUTPUT);
  19.       pinMode(trig, OUTPUT);
  20.       pinMode(echo, INPUT);
  21.       pinMode(ldr, INPUT);
  22. }
  23.  
  24. void loop()
  25. {
  26.      check_brightness();
  27.      check_obas(trig,echo);
  28.      
  29. }
  30.  
  31. void check_obas(int trig,int echo)
  32. {
  33.       digitalWrite(trig, LOW); // clears the trigger pin
  34.       delayMicroseconds(2);
  35.       digitalWrite(trig, HIGH); // set the trigger pin High for 10us
  36.       delayMicroseconds(10);
  37.       digitalWrite(trig, LOW); // set the trigger pin Low.
  38.       dua = pulseIn(echo, HIGH); // read the echo pin and we get the duaration in us
  39.       dist = (dua/2) * 0.034; // "0.034" is speed of sound in cm/us
  40.       Serial.print(dist); // debugging
  41.       Serial.println("cm"); // debugging
  42.       if ( dist > 200 )
  43.         digitalWrite(buz,LOW);
  44.       else if ( dist < 200 && dist > 150)
  45.       {
  46.         buz3(dist,buz);
  47.       }
  48.       else if (dist < 150 && dist > 50)
  49.       {
  50.         buz2(dist,buz);
  51.       }
  52.      else if ( dist < 50 && dist > 0)
  53.      {
  54.       buz1(dist,buz);
  55.      }
  56. }
  57.  
  58.  
  59. void buz1(unsigned int Z, int Y)
  60. {
  61.           for (x= Z; x>0 ; x--)
  62.           {
  63.            digitalWrite(Y,HIGH);
  64.            delay(50);
  65.            digitalWrite(Y,LOW);
  66.            delay(50);
  67.           }
  68. }
  69.  
  70. void buz2(unsigned int Z, int Y)
  71. {
  72.           for (x= Z; x>0 ; x--)
  73.           {
  74.            digitalWrite(Y,HIGH);
  75.            delay(150);
  76.            digitalWrite(Y,LOW);
  77.            delay(150);
  78.           }
  79. }
  80.  
  81. void buz3(unsigned int Z, int Y)
  82. {
  83.           for (x= Z; x>0 ; x--)
  84.           {
  85.            digitalWrite(Y,HIGH);
  86.            delay(500);
  87.            digitalWrite(Y,LOW);
  88.            delay(500);
  89.           }
  90. }
  91.  
  92.  
  93. void check_brightness()
  94. {
  95.         int brightenss = analogRead(ldr);
  96.    
  97.         if (brightness < 100)
  98.         {
  99.           Serial.println("State: Dark");
  100.           buz1(brightness,buz);
  101.         }
  102.         else if (brightness < 250  && brightness > 100)
  103.         {
  104.           Serial.println("State: Dim Light");
  105.           buz2(brightness,buz);
  106.         }
  107.         else if (brightness < 650 && brightness > 250)
  108.         {
  109.           Serial.println("State: mid brightness");
  110.           buz3(brightness,buz);
  111.         }
  112.         else
  113.         {
  114.           Serial.println("State: V.bright");
  115.           digitalWrite(buz,LOW);
  116.         }
  117.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement