|
| 1 | +#include <String.h> |
| 2 | +#include <Servo.h> |
| 3 | + |
| 4 | +#define DEBOUNCE_TIME .5 |
| 5 | +#define SAMPLE_FREQUENCY 20 |
| 6 | +#define MAXIMUM (DEBOUNCE_TIME * SAMPLE_FREQUENCY) |
| 7 | + |
| 8 | +// function declarations |
| 9 | +void RecvWithStartEndMarkers(); |
| 10 | +void ProcessData(); |
| 11 | +void ShowNewData(); |
| 12 | + |
| 13 | +// variables for communication |
| 14 | +const byte numChars = 32; |
| 15 | +char receivedChars[numChars]; |
| 16 | +bool newData = false; |
| 17 | + |
| 18 | +// variables for motor 1 |
| 19 | +// ports |
| 20 | +Servo myservo1; |
| 21 | +byte motor_port = 3; |
| 22 | +byte motor_counter_port = 4; |
| 23 | +byte motor_reset_port = 2; |
| 24 | +// motor info |
| 25 | +String motor_direction = ""; |
| 26 | +byte motor_rotation_number = 0; |
| 27 | +byte motor_sensor_counter1 = 0; |
| 28 | +byte motor_sensor_counter2 = 0; |
| 29 | +// |
| 30 | +// variables for motor 2 |
| 31 | +Servo myservo2; |
| 32 | +byte motor_port2 = 9; |
| 33 | +byte motor_counter_port2 = 8; |
| 34 | +byte motor_reset_port2 = 7; |
| 35 | +// motor info |
| 36 | +String motor_direction2 = ""; |
| 37 | +byte motor_rotation_number2 = 0; |
| 38 | +byte motor_sensor_counter12 = 0; |
| 39 | +byte motor_sensor_counter22 = 0; |
| 40 | +// |
| 41 | +byte input; /* 0 or 1 depending on the input signal */ |
| 42 | +int integrator; /* Will range from 0 to the specified MAXIMUM */ |
| 43 | +byte output; /* Cleaned-up version of the input signal */ |
| 44 | + |
| 45 | +byte input2; /* 0 or 1 depending on the input signal */ |
| 46 | +int integrator2; /* Will range from 0 to the specified MAXIMUM */ |
| 47 | +byte output2; /* Cleaned-up version of the input signal */ |
| 48 | + |
| 49 | +////////////////////////////////////////////////////////////////////////////// |
| 50 | +////////////////////////////////////////////////////////////////////////////// |
| 51 | + |
| 52 | +void ProcessData() { |
| 53 | + |
| 54 | + // variables |
| 55 | + String recievedString = ""; |
| 56 | + String motor_rotation_string = ""; |
| 57 | + String motor_rotation_string2 = ""; |
| 58 | + // logic |
| 59 | + recievedString = receivedChars; |
| 60 | + motor_direction = getValue(recievedString, ',', 0); |
| 61 | + motor_rotation_string = getValue(recievedString, ',', 1); |
| 62 | + motor_rotation_number = motor_rotation_string.toInt(); |
| 63 | + |
| 64 | +// |
| 65 | + motor_direction2 = getValue(recievedString, ',', 2); |
| 66 | + motor_rotation_string2 = getValue(recievedString, ',', 3); |
| 67 | + motor_rotation_number2 = motor_rotation_string2.toInt(); |
| 68 | +// |
| 69 | + |
| 70 | + if (motor_direction == "Up") { |
| 71 | + myservo1.write(100); |
| 72 | + } |
| 73 | + else if (motor_direction == "Down") { |
| 74 | + myservo1.write(80); |
| 75 | + } |
| 76 | + |
| 77 | + else if (motor_direction == "None"){ |
| 78 | + myservo1.write(90); |
| 79 | + } |
| 80 | + else if (motor_direction == "Reset"){ |
| 81 | + Reset1(); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + // TODO: make this exit the program |
| 86 | + Serial.println("<"); |
| 87 | + Serial.print("Arduino: "); |
| 88 | + Serial.println("Invalid Input!"); |
| 89 | + } |
| 90 | + |
| 91 | + while(true) { |
| 92 | + |
| 93 | + motor_sensor_counter2 = motor_sensor_counter1; |
| 94 | + checkswitch(motor_counter_port); |
| 95 | + motor_sensor_counter1 = output; |
| 96 | + delay(10); |
| 97 | + |
| 98 | + if(motor_rotation_number == 0) { |
| 99 | + myservo1.write(90); |
| 100 | + break; |
| 101 | + } |
| 102 | + |
| 103 | + if(digitalRead(motor_reset_port) == 0){ |
| 104 | + myservo1.write(90); |
| 105 | + break; |
| 106 | + } |
| 107 | + |
| 108 | + if(motor_sensor_counter1 == 1 && motor_sensor_counter2 == 0) { |
| 109 | + motor_rotation_number--; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (motor_direction2 == "Up") { |
| 114 | + myservo2.write(100); |
| 115 | + } |
| 116 | + else if (motor_direction2 == "Down") { |
| 117 | + myservo2.write(80); |
| 118 | + } |
| 119 | + else if (motor_direction2 == "None"){ |
| 120 | + myservo2.write(90); |
| 121 | + } |
| 122 | + else if (motor_direction2 == "Reset"){ |
| 123 | + Reset2(); |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + // TODO: make this exit the program |
| 128 | + Serial.println("<"); |
| 129 | + Serial.print("Arduino: "); |
| 130 | + Serial.println("Invalid Input!"); |
| 131 | + } |
| 132 | + |
| 133 | + while(true) { |
| 134 | + |
| 135 | + motor_sensor_counter22 = motor_sensor_counter12; |
| 136 | + checkswitch2(motor_counter_port2); |
| 137 | + motor_sensor_counter12 = output2; |
| 138 | + delay(10); |
| 139 | + |
| 140 | + if(motor_rotation_number2 == 0) { |
| 141 | + myservo2.write(90); |
| 142 | + break; |
| 143 | + } |
| 144 | + |
| 145 | + if(digitalRead(motor_reset_port2) == 0){ |
| 146 | + myservo2.write(90); |
| 147 | + break; |
| 148 | + } |
| 149 | + |
| 150 | + if(motor_sensor_counter12 == 1 && motor_sensor_counter22 == 0) { |
| 151 | + motor_rotation_number2--; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | +} |
| 156 | + |
| 157 | +////////////////////////////////////////////////////////////////////////////// |
| 158 | +////////////////////////////////////////////////////////////////////////////// |
| 159 | + |
| 160 | +void checkswitch(int switchPort) { |
| 161 | + /* Step 1: Update the integrator based on the input signal. Note that the |
| 162 | + integrator follows the input, decreasing or increasing towards the limits as |
| 163 | + determined by the input state (0 or 1). */ |
| 164 | + input = digitalRead(switchPort); |
| 165 | + |
| 166 | + if (input == 0) |
| 167 | + { |
| 168 | + if (integrator > 0) |
| 169 | + integrator--; |
| 170 | + } |
| 171 | + else if (integrator < MAXIMUM) |
| 172 | + integrator++; |
| 173 | + |
| 174 | + /* Step 2: Update the output state based on the integrator. Note that the |
| 175 | + output will only change states if the integrator has reached a limit, either |
| 176 | + 0 or MAXIMUM. */ |
| 177 | + |
| 178 | + if (integrator == 0) |
| 179 | + output = 0; |
| 180 | + else if (integrator >= MAXIMUM) |
| 181 | + { |
| 182 | + output = 1; |
| 183 | + integrator = MAXIMUM; /* defensive code if integrator got corrupted */ |
| 184 | + } |
| 185 | + |
| 186 | + /********************************************************* End of debounce.c */ |
| 187 | +} |
| 188 | + |
| 189 | +////////////////////////////////////////////////////////////////////////////// |
| 190 | +////////////////////////////////////////////////////////////////////////////// |
| 191 | + |
| 192 | +void checkswitch2(int switchPort) { |
| 193 | + /* Step 1: Update the integrator based on the input signal. Note that the |
| 194 | + integrator follows the input, decreasing or increasing towards the limits as |
| 195 | + determined by the input state (0 or 1). */ |
| 196 | + input2 = digitalRead(switchPort); |
| 197 | + |
| 198 | + if (input2 == 0) |
| 199 | + { |
| 200 | + if (integrator2 > 0) |
| 201 | + integrator2--; |
| 202 | + } |
| 203 | + else if (integrator2 < MAXIMUM) |
| 204 | + integrator2++; |
| 205 | + |
| 206 | + /* Step 2: Update the output state based on the integrator. Note that the |
| 207 | + output will only change states if the integrator has reached a limit, either |
| 208 | + 0 or MAXIMUM. */ |
| 209 | + |
| 210 | + if (integrator2 == 0) |
| 211 | + output2 = 0; |
| 212 | + else if (integrator2 >= MAXIMUM) |
| 213 | + { |
| 214 | + output2 = 1; |
| 215 | + integrator2 = MAXIMUM; /* defensive code if integrator got corrupted */ |
| 216 | + } |
| 217 | + |
| 218 | + /********************************************************* End of debounce.c */ |
| 219 | +} |
0 commit comments