Main.ino 1.91 KB
Newer Older
ssshroff's avatar
ssshroff committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

const int trigPin = 12;
const int echoPin = 11;

const int trigPin2 = 8;
const int echoPin2 = 9;

const int motor = 3;

const int button = 13;

const int potentiometer = A0;

long duration2;
long duration;

int distance;
int distance2;

int volume;
Int volumeScaled;

void setup() {

//US1
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input

//US2
pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin2, INPUT); // Sets the echoPin as an Input

pinMode(potentiometer, INPUT);
pinMode(button, INPUT_PULLUP);
pinMode(motor, OUTPUT);

Serial.begin(9600); // Starts the serial communication
}

  void loop() {

  if(digitalRead(button)== HIGH) {
      // Clears the trigPin
      digitalWrite(trigPin2, LOW);
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      
      // Sets the trigPin on HIGH state for 10 micro seconds
      digitalWrite(trigPin2, HIGH);
      digitalWrite(trigPin, HIGH);
      
      delayMicroseconds(10);
      digitalWrite(trigPin2, LOW);
      digitalWrite(trigPin, LOW);
      
      // Reads the echoPin, returns the sound wave travel time in microseconds
      duration2 = pulseIn(echoPin2, HIGH);
      duration = pulseIn(echoPin, HIGH);

      //Potentiometer
      volume = analogRead(potentiometer);
      volumeScaled = volume* 0.25;
      
      // Calculating the distance
      distance = duration * 0.034/2;
      distance2 = duration2 * 0.034/2;

      if (distance2 > 30) {
        Serial.println("No output");
      }111    
 
      if (distance2 < 30)   {
             if (distance < 75) {
                      Serial.println(distance);
                      analogWrite(motor, volumeScaled);  
                      delay(distance * 15);
          }

      else{
      }
  }

  else{
    digitalWrite(motor,LOW);
  }
 
  }
 
  if(digitalRead(button)== LOW) {
    digitalWrite(motor, LOW);
    }
    
  }