-
Notifications
You must be signed in to change notification settings - Fork 0
/
UTS-IRCom(1)
93 lines (89 loc) · 2.7 KB
/
UTS-IRCom(1)
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
#include <SoftwareSerial.h>
SoftwareSerial ESPserial(2, 3); // RX | TX
int led1=4;
int led2=5;
int led3=6;
int led4=7;
int inches = 0;
int cm = 0;
long readUltrasonicDistance(int pin)
{
pinMode(pin, OUTPUT); // Clear the trigger
digitalWrite(pin, LOW);
delayMicroseconds(2);
// Sets the pin on HIGH state for 10 micro seconds
digitalWrite(pin, HIGH);
delayMicroseconds(10);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
// Reads the pin, and returns the sound wave travel time in microseconds
return pulseIn(pin, HIGH);
}
void setup()
{
Serial.begin(9600); // communication with the host computer
pinMode(7, INPUT);
//while (!Serial) { ; }
// Start the software serial for communication with the ESP8266
ESPserial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("");
Serial.println("Remember to to set Both NL & CR in the serial monitor.");
Serial.println("Ready");
Serial.println("");
}
void loop()
{
// listen for communication from the ESP8266 and then write it to the serial monitor
cm = 0.01723 * readUltrasonicDistance(8);
Serial.print(cm);
Serial.println("cm");
delay(100);
if (cm >250){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
}
else if(cm <250 & cm >= 150){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);}
else if (cm<150 & cm >=75){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);}
else if (cm <75){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);}
else {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);}
if ( ESPserial.available() )
{ Serial.write( ESPserial.read() ); }
// listen for user input and send it to the ESP8266
if ( Serial.available() )
{ ESPserial.write( 'Esp Serial Data Read from = ');
ESPserial.write(Serial.read());
Serial.print(Serial.read());}
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
delay(1000); // Wait for a second
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
delay(2000);
}