Advertisement
ZzzM8

Hc sensorius

May 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define RELAY1  2
  2. #define RELAY2  3
  3.  
  4.  
  5. int trigPin = 11;    // Trigger
  6. int echoPin = 12;    // Echo
  7. long duration, cm, inches;
  8. int laikas =0;
  9.  
  10. void setup() {
  11.   //Serial Port begin
  12.   Serial.begin (9600);
  13.   //Define inputs and outputs
  14.   pinMode(trigPin, OUTPUT);
  15.   pinMode(echoPin, INPUT);
  16.   pinMode(RELAY1, OUTPUT);  
  17.   digitalWrite(RELAY1, HIGH);
  18.   pinMode(RELAY2, OUTPUT);  
  19.   digitalWrite(RELAY2, HIGH);
  20.   delay(3000);
  21.  
  22. }
  23.  
  24. void loop() {
  25.   // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  26.   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  27.   digitalWrite(trigPin, LOW);
  28.   delayMicroseconds(5);
  29.   digitalWrite(trigPin, HIGH);
  30.   delayMicroseconds(10);
  31.   digitalWrite(trigPin, LOW);
  32.  
  33.   // Read the signal from the sensor: a HIGH pulse whose
  34.   // duration is the time (in microseconds) from the sending
  35.   // of the ping to the reception of its echo off of an object.
  36.   pinMode(echoPin, INPUT);
  37.   duration = pulseIn(echoPin, HIGH);
  38.  
  39.   // Convert the time into a distance
  40.   cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
  41.   inches = (duration/2) / 74;   // Divide by 74 or multiply by 0.0135
  42.  
  43.   Serial.print(inches);
  44.   Serial.print("in, ");
  45.   Serial.print(cm);
  46.   Serial.print("cm");
  47.   Serial.println();
  48.  
  49.  
  50.  
  51.   digitalWrite(RELAY1,LOW);   // Turns the Relay/Pump on
  52.   laikas++;
  53.  
  54.   if(laikas>500){
  55.     laikas=0;
  56.     digitalWrite(RELAY1,HIGH );  // Relay/Pump is off
  57.     delay(1000);
  58.     }
  59.  
  60.     digitalWrite(RELAY2, LOW);
  61.  
  62. if(laikas >20 && laikas <50 ){
  63.   laikas = 60;
  64.   digitalWrite(RELAY2, HIGH);
  65.   delay(2000);
  66. }
  67.  
  68.  
  69.  
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement