/
hourGlassV1.ino
227 lines (189 loc) · 5.49 KB
/
hourGlassV1.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
Servo myservo;
const int magPin = A0;
const int motorPin = D3;
int noMagVal=0; //zero Magnetic Field Value
int negPoleTH = 0;
int posPoleTH = 0;
int stateBuffNum = 500; //wait for this state count before accepting state
int stateCount=0;//how many times has a state been seen
bool moving=false; //is hourGlass moving
int state = 0;
int globalState=3;
bool matchState=false;
int lastState=0;
unsigned long lastTime = 0;
unsigned long lastGlobalState = millis();
const int numReadings = 2;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int states2global = 0;
int maxV=0;
int minV=0;
int unitNo=1;
void setup()
{
myservo.attach(motorPin);
pinMode(magPin, INPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {readings[thisReading] = 0;}
myservo.write(100);
unsigned long start =millis();
int maxV=0;
int minV =10000;
int val;
while(millis()-start<20000){
int val = analogRead(magPin);
if (val<minV){minV=val;}
if(val>maxV){maxV=val;}
delay(1);
}
myservo.detach();
noMagVal = (maxV+minV)*0.5;
negPoleTH = 0.95*(minV - noMagVal);
posPoleTH = 0.95*(maxV - noMagVal);
Particle.publish("maxmin",String(posPoleTH)+", "+String(negPoleTH)+", "+String(noMagVal), 100, PRIVATE);
Serial.println(String(maxV)+", "+String(minV));
Serial.println(String(posPoleTH)+", "+String(negPoleTH));
Particle.subscribe("globalState", stateHandler, MY_DEVICES);
Serial.begin(9600);
String myID = System.deviceID();
if (myID == "270039000c47363339343638"){
unitNo=2;
// noMagVal = 2028;
// negPoleTH = -250;
// posPoleTH = 250;
}
else{
unitNo=1;
// noMagVal = 2028;
// negPoleTH = -265;
// posPoleTH = 265;
}
delay(1000);
Serial.println(unitNo);
Serial.println("begin");
}
void loop()
{
average = getAverage();
state = getState(average, lastState); //get state from value
///////////////////////////////////////Print Things
if (state != lastState && abs(state)==2 && state !=globalState && (millis()-lastGlobalState>1000 && !matchState)){
lastGlobalState=millis();
Serial.println("New "+String(state));
Particle.publish("globalState"+String(unitNo), String(state), 100, PRIVATE);
}
unsigned long now = millis(); //print things
if ((now - lastTime) >= 1000) {
lastTime = now;
// Serial.println(String(state) + ", "+ String(average) + ", " + String(stateCount)+", "+String(moving));
Serial.println(String(globalState)+", "+ String(state)+", "+ String(average));
}
//////////////////////////////////////////
if (matchState){gotoGlobalState();}
else{//notMatchState
if (moving){
if (abs(state) ==2){
delay(100);
stopServo();
}
else if (state == 0 && stateCount>stateBuffNum){
stopServo();
}
else if (abs(state)==1){
if ( getSpeed(state) != myservo.read() && stateCount > 300){
myservo.write(getSpeed(state));
delay(10);
}
}
}
else{//not moving
if (abs(state)==1 && stateCount >500){
startServo();
}
}
}
lastState=state;
delay(1);
}//end of loop
int getAverage(){
total = total - readings[readIndex];
readings[readIndex] = analogRead(magPin)-noMagVal;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {readIndex = 0;}
return total / numReadings;
}
int gotoGlobalState(){
int states2see[] = {1,-1,1};
if (state==states2see[states2global]){
states2global +=1;
if (states2global==3){
states2global=0;
matchState=false;
}
}
if (state == globalState){
matchState=false;
myservo.write(90);
myservo.detach();
}else if (state==0 && stateCount>1000){
myservo.write(90);
myservo.detach();
}
else{
if(!myservo.attached()){myservo.attach(motorPin);}
myservo.write(95);
}
}
int getState(int value, int pState){
// int negPoleTH = -380;
// int posPoleTH = 330;
//
// if (unitNo==2){
// negPoleTH = -500;
// posPoleTH = 380;
// }
int onBaseTH = 60;
float thresh = 0.8;
int sign=0;
int newState;
if (value > 0){sign=1;}else if(value<0){sign=-1;}
if (abs(value) > onBaseTH) {
if (value > posPoleTH || value < negPoleTH){newState= sign*2;}
else if( value > posPoleTH*thresh || value < negPoleTH*thresh){ newState= pState;}
else{newState=sign*1;}
}
else{ newState= 0;}
if (newState==pState){if(stateCount<5000){stateCount+=1;} } //increment stateCount
else {stateCount=0;}
return newState;
}
int getSpeed(int state){
if (state==1){return 83;}
else if (state == -1){return 96;}
else{ return 90;}
}
void startServo(){
myservo.attach(motorPin);
myservo.write(getSpeed(state));
moving=true;
}
void stopServo(){
myservo.write(90);
myservo.detach();
moving=false;
}
void stateHandler(const char *event, const char *data)
{
if (data){
if (String(event)!="globalState"+String(unitNo) ){
Serial.println("here");
Serial.println(String(globalState)+", "+String(data)+", "+String(state));
globalState=atoi(data);
if (globalState!=state){ matchState=true;}
}
}
else {Serial.println("NULL");}
}