-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotorLoopWithTimer.ino
176 lines (153 loc) · 5.26 KB
/
motorLoopWithTimer.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
#include <Servo.h>
#include <StopWatch.h>
int pushButtonUp = 4;// YELLOW button, declare which pin is for pushButtonUp
int pushButtonDown = 8;// BLUE button, declare which pin is for pushButtonUp
int buttonEStop = 13;// declare which pin is for buttonEStop
int upButtonState = 0;
int downButtonState = 0;
int eStopButtonState = 0;
long pwmOutput = 3;// declare output pin for pwm to controller
//config
boolean noTimeLimit = true;
long restPower = 1500;
long upPower = 1000;
long downPower = 2000;
int operationTime = 3000;
//operation states
boolean finishedOperation = false;
boolean reachedTop = false;
boolean reachedBottom = true; //ASSUME initial condition is at bottom
long remainingUpTime = operationTime;
long remainingDownTime = operationTime;
boolean firstLoop = true;
Servo myservo;
StopWatch MySW;
void setup() {
Serial.begin(9600); // initializes the serial communication at 9600 bits per second bw arduino and comp
pinMode (pwmOutput, OUTPUT);
pinMode (pushButtonUp, INPUT);
pinMode (pushButtonDown, INPUT);
pinMode (buttonEStop, INPUT);
myservo.attach(3);
myservo.writeMicroseconds(1500); // set servo to mid-point
}
void loop() {
firstLoop = true;
upButtonState = digitalRead(pushButtonUp);
downButtonState = digitalRead(pushButtonDown);
eStopButtonState = digitalRead(buttonEStop);
if (firstLoop == true){
delay(100); // 250 milliseconds delay (1/4 second delay)
};
if (MySW.elapsed() != 0){
Serial.println("RESETTING TIMER");
MySW.reset();
}
while (eStopButtonState === 1) {
eStopButtonState = digitalRead(buttonEStop);
myservo.writeMicroseconds(restPower);
}
while (upButtonState == 1 && downButtonState != 1 && finishedOperation == false && reachedTop == false) {
if (firstLoop == true){
Serial.println("Up");
}
firstLoop = false;
MySW.start();
reachedBottom = false;
myservo.writeMicroseconds(upPower);
upButtonState = digitalRead(pushButtonUp);
eStopButtonState = digitalRead(buttonEStop);
if (eStopButtonState === 1) {
myservo.writeMicroseconds(restPower);
break;
}
//if top position reached
if (MySW.elapsed() > remainingUpTime && noTimeLimit == false) {
finishedOperation = true;
reachedTop = true;
Serial.println("Held for duration of:");
Serial.println(MySW.elapsed());
remainingUpTime = 0;
remainingDownTime = operationTime;
myservo.writeMicroseconds(1500);
Serial.println("User is Fully Standing");
Serial.println("Remaining Up time");
Serial.println(remainingUpTime);
Serial.println("Remaining Down time");
Serial.println(remainingDownTime);
MySW.stop();
break;
}
//if interrupted during up process
else if (upButtonState != 1) {
Serial.println("Held for duration of:");
Serial.println(MySW.elapsed());
remainingUpTime = remainingUpTime - MySW.elapsed();
remainingDownTime = operationTime - remainingUpTime;
myservo.writeMicroseconds(restPower);
Serial.println("User is Partially Standing");
Serial.println("Remaining Up time");
Serial.println(remainingUpTime);
Serial.println("Remaining Down time");
Serial.println(remainingDownTime);
MySW.stop();
break;
}
}
while (upButtonState == 1 && downButtonState == 1) {
//analogWrite (pwmOutput, 60); // need to figure out 0->255 range for pwm output
Serial.println("both pressed"); // Print out status of Upbuton to the serial monitor
break;
}
while(upButtonState != 1 && downButtonState == 1 && finishedOperation == false && reachedBottom == false) {
if (firstLoop == true){
Serial.println("Down");
}
firstLoop = false;
MySW.start();
reachedTop = false;
myservo.writeMicroseconds(downPower);
downButtonState = digitalRead(pushButtonDown);
eStopButtonState = digitalRead(buttonEStop);
if (eStopButtonState === 1) {
myservo.writeMicroseconds(restPower);
break;
}
//if bottom position reached
if (MySW.elapsed() > remainingDownTime && noTimeLimit == false) {
finishedOperation = true;
reachedBottom = true;
Serial.println("Held for duration of:");
Serial.println(MySW.elapsed());
remainingUpTime = operationTime;
remainingDownTime = 0;
myservo.writeMicroseconds(restPower);
Serial.println("User is Fully Sitting");
Serial.println("Remaining Up time");
Serial.println(remainingUpTime);
Serial.println("Remaining Down time");
Serial.println(remainingDownTime);
MySW.stop();
break;
}
//if interrupted during down process
else if (downButtonState != 1) {
Serial.println("Held for duration of:");
Serial.println(MySW.elapsed());
remainingDownTime = remainingDownTime - MySW.elapsed();
remainingUpTime = operationTime - remainingDownTime;
myservo.writeMicroseconds(restPower);
Serial.println("User is Partially Sitting");
Serial.println("Remaining Up time");
Serial.println(remainingUpTime);
Serial.println("Remaining Down time");
Serial.println(remainingDownTime);
MySW.stop();
break;
}
}
if (upButtonState == 0 && downButtonState == 0) {
finishedOperation = false;
myservo.writeMicroseconds(restPower);
}
}