-
Notifications
You must be signed in to change notification settings - Fork 0
/
phototrope.ino
123 lines (102 loc) · 1.75 KB
/
phototrope.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
phototrope
This example shows how to create a light following robot
created Aug 2017
by M.Himamsu
modified Sep 2017
by M.Himamsu
*/
void setup()
{
Serial.begin(9600);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
}
void loop()
{
int sensorValue1 = analogRead(A0);
float v1 = sensorValue1 * (5000.0 / 1023.0);
Serial.print(" sensor 1 ");
Serial.print(v1);
int sensorValue2 = analogRead(A1);
float v2 = sensorValue2 * (5000.0 / 1023.0);
Serial.print(" sensor 2 ");
Serial.print(v2);
int sensorValue3 = analogRead(A2);
float v3 = sensorValue3 * (5000.0 / 1023.0);
Serial.print(" sensor 3 ");
Serial.print(v3);
int sensorValue4 = analogRead(A3);
float v4 = sensorValue4 * (5000.0 / 1023.0);
Serial.print(" sensor 4 ");
Serial.print(v4);
int e1= (v4-v3)/5;
int d1=0;
if (e1<1023&&e1>-1023)
{
d1=e1;
}
else
{
if(e1>1023)
{
d1=1023;
}
if(e1<-1023)
{
d1=-1023;
}
}
Serial.print(" diffference aw ");
Serial.print(d1);
int e2= (v2-v1)/5;
int d2=0;
if (e2<1023&&e2>-1023)
{
d2=e2;
}
else
{
if(e2>1023)
{
d2=1023;
}
if(e2<-1023)
{
d2=-1023;
}
}
Serial.print(" diffference pw ");
Serial.print(d2);
Serial.println();
if(d1>0)
{
analogWrite(5,d1);
analogWrite(6,LOW);
analogWrite(9,d1);
analogWrite(10,LOW);
}
if(d1<0)
{
analogWrite(5,LOW);
analogWrite(6,(d1*-1));
analogWrite(9,LOW);
analogWrite(10,(d1*-1));
}
if(d2>0)
{
analogWrite(5,d2);
analogWrite(6,LOW);
analogWrite(9,LOW);
analogWrite(10,d2);
}
if(d2<0)
{
analogWrite(5,LOW);
analogWrite(6,(d2*-1));
analogWrite(9,(d2*-1));
analogWrite(10,LOW);
}
}