This repository was archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch_logic2.ino
More file actions
111 lines (96 loc) · 2.1 KB
/
sketch_logic2.ino
File metadata and controls
111 lines (96 loc) · 2.1 KB
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
//this program aims to control a intellectual rotation of shelter board
#include <Wire.h>
#include <math.h>
#define refine1 29
#define refine2 30
#define CTL1 39
#define CTL2 40
#define RATE_UPDATE 5//milliseconds
void Stop();
boolean IsLimited(int refine1_or_refine2);
boolean IsIntense();
boolean IsLowest(int Lowest);
int RecordTheLowest(int Lowest);
volatile int dir = 0;
volatile int run = 0;
volatile uint16_t val = 0;
const bool back = 0;
const bool forward = 1;
const int intense_level = 1200;
const int deta = 100;// , in IsLowest to limit the range
void setup()
{
// put your setup code here, to run once:
pinMode(CTL1, OUTPUT);
digitalWrite(CTL1, LOW);
pinMode(CTL2, OUTPUT);
digitalWrite(CTL2, LOW);
pinMode(refine1, INPUT);
pinMode(refine2, INPUT);
}
void loop()
{
//rotate back to the origin spot
digitalWrite(RED_LED,LOW);
digitalWrite(BLUE_LED,LOW);
if(!IsLimited(refine1)){
dir = back;
run = 1;
while(!IsLimited(refine1)){
delay(RATE_UPDATE);
}
Stop();
}
//start rotating
//stop when the light is intense or hit the refine2 limit
int Lowest = 0; //record the lowest light;
dir = forward;
run =1;
while((!IsIntense() )&&!IsLimited(refine2 )){
Lowest = RecordTheLowest(Lowest);
delay(RATE_UPDATE);
}
Stop();
//if hit the refine2 limit, rotate back
//stop at the direction of the lowest light comes
if(IsLimited(refine2)){
dir = back;
run = 1;
while((IsLowest(Lowest) != true) && !IsLimited(refine1)){
delay(RATE_UPDATE);
}
Stop();
}
//sleep
delay(10000);
}
boolean IsLimited(int refine)
{
return digitalRead(refine);
}
boolean IsIntense()
{
if(val >= intense_level)
{
digitalWrite(RED_LED,HIGH);
}
else digitalWrite(RED_LED,LOW);
return (val >= intense_level) ;
}
boolean IsLowest(int Lowest){
if(val <= Lowest + deta)
{
digitalWrite(BLUE_LED,HIGH);
}
else digitalWrite(BLUE_LED,LOW);
return (val <= Lowest + deta);
}
int RecordTheLowest(int Lowest){
if(val < Lowest&&val>Lowest - 20) return val;
else return Lowest;
}
void Stop(){
run = 0;
delay(1000);
return;
}