-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfour_sensors.ino
100 lines (90 loc) · 2.77 KB
/
four_sensors.ino
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
96
97
98
99
100
/******************************************************************************
QRD1114_Proximity_Example.ino
Example sketch for SparkFun's QRD1114 Reflectance Proximity Sensor
(https://www.sparkfun.com/products/246)
Jim Lindblom @ SparkFun Electronics
May 2, 2016
Connect a QRD1114, 330 resistor and 10k resistor as follows:
QRD1114 Pin ---- Arduino ---- Resistors
1 A0 10k Pull-up to 5V
2 GND
3 330 Resistor to 5V
4 GND
As an object comes closer to the QRD1114, the voltage on A0 should go down.
Development environment specifics:
Arduino 1.6.7
******************************************************************************/
const int QRD1114_PIN0 = A0; //right
const int QRD1114_PIN1 = A1; //up
const int QRD1114_PIN2 = A2; //down
const int QRD1114_PIN3 = A3; //left
int out1= 2;
int out2= 3;
int out3= 4;
int out4= 5;
void setup()
{
Serial.begin(9600); //connection to computer
pinMode(QRD1114_PIN0, INPUT); //right
pinMode(QRD1114_PIN1, INPUT); //up
pinMode(QRD1114_PIN2, INPUT); //down
pinMode(QRD1114_PIN3, INPUT); //left
pinMode(out1,OUTPUT);
pinMode(out2,OUTPUT);
pinMode(out3,OUTPUT);
pinMode(out4,OUTPUT);
}
void loop()
{
// Read in the ADC and convert it to a voltage:
int proximityADC0 = analogRead(QRD1114_PIN0);
int proximityADC1 = analogRead(QRD1114_PIN1);
int proximityADC2 = analogRead(QRD1114_PIN2);
int proximityADC3 = analogRead(QRD1114_PIN3);
float proximityV0 = (float)proximityADC0 * 3.3 / 1023.0;
float proximityV1 = (float)proximityADC1 * 3.3 / 1023.0;
float proximityV2 = (float)proximityADC2 * 3.3 / 1023.0;
float proximityV3 = (float)proximityADC3 * 3.3 / 1023.0;
char direction;
int zero=0;
if (proximityV0<=2.00){
direction = 'r';
Serial.println(direction);
digitalWrite(out1, HIGH);
digitalWrite(out2, LOW);
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
}
else if (proximityV1<=2.00){
direction = 'u';
Serial.println(direction);
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH);
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
}
else if (proximityV2<=2.00){
direction = 'd';
Serial.println(direction);
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
}
else if (proximityV3<=2.00){
direction = 'l';
Serial.println(direction);
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
digitalWrite(out3, LOW);
digitalWrite(out4, HIGH);
}
else{
Serial.println(zero);
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
}
delay(100);
}