|
| 1 | +/* *************************************************************************** |
| 2 | + * This sketch contains logic of self-maintained silo launcher. * |
| 3 | + * * |
| 4 | + * Sketch uses Arduino Nano as main board. * |
| 5 | + * Sketch uses piezzo buzzer, servo, two relay modules to control ignitor, * |
| 6 | + * solenoid valve. It's recommended to power board, servo and ignitor with * |
| 7 | + * stable 2x18650. Solenoid valve powered by 12v moto battery. * |
| 8 | + * * |
| 9 | + * Logic: * |
| 10 | + * 1) Init all modules * |
| 11 | + * 2) Beep after init is complete * |
| 12 | + * 3) Wait a safety delay (i.e. 1 min) * |
| 13 | + * 4) Beep several times as final countdown * |
| 14 | + * 5) Open silo cap * |
| 15 | + * 6) Ignite engine * |
| 16 | + * 7) Open valve to push out rocket from silo * |
| 17 | + * 8) Switch off ignition * |
| 18 | + * 9) Close valve * |
| 19 | + * 10) Close cap * |
| 20 | + * 11) Beep continuously * |
| 21 | + * * |
| 22 | + * Sketch written by Iliya Vereshchagin 2021. * |
| 23 | + *****************************************************************************/ |
| 24 | + |
| 25 | +#include <Servo.h> |
| 26 | + |
| 27 | +#define DEBUG |
| 28 | + |
| 29 | +// Servo globals |
| 30 | +Servo servo; |
| 31 | +const int SERVO_PIN = 10; |
| 32 | +const int SERVO_DELAY = 1000; |
| 33 | + |
| 34 | +// Solenoid valve globals |
| 35 | +const int VALVE_PIN = 7; |
| 36 | + |
| 37 | +// Ignitor globals |
| 38 | +const int IGNITOR_PIN = 5; |
| 39 | +const long IGNITOR_DELAY = 2000; |
| 40 | + |
| 41 | +// Buzzer globals |
| 42 | +const int BUZZER_PIN = 3; |
| 43 | +const int BUZZER_FREQUENCY = 3500; |
| 44 | +const int BUZZER_DELAY = 500; |
| 45 | +const int BUZZER_LAST_DELAY = 5000; |
| 46 | +const int BUZZER_OK = 1; |
| 47 | +const int BUZZER_START = 3; |
| 48 | + |
| 49 | +// Sequence globals |
| 50 | +const long LAUNCH_DELAY = 60000; // 1 minute from power on to start |
| 51 | +const int POST_LAUNCH_DELAY = 3000; // 3 seconds |
| 52 | + |
| 53 | + |
| 54 | +void setup() |
| 55 | +{ |
| 56 | + #ifdef DEBUG |
| 57 | + Serial.begin(9600); |
| 58 | + #endif |
| 59 | + init_valve(); |
| 60 | + init_ignitor(); |
| 61 | + init_servo(); |
| 62 | + init_buzzer(); |
| 63 | + beep_count(BUZZER_OK); |
| 64 | + // Force launch sequence right after setup |
| 65 | + launch_sequence(); |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +/* Init functions */ |
| 70 | + |
| 71 | +void init_ignitor() |
| 72 | +{ |
| 73 | + pinMode(IGNITOR_PIN, OUTPUT); |
| 74 | + digitalWrite(IGNITOR_PIN, LOW); |
| 75 | + #ifdef DEBUG |
| 76 | + Serial.println("Ignitor initialized"); |
| 77 | + #endif |
| 78 | +} |
| 79 | + |
| 80 | +void init_valve() |
| 81 | +{ |
| 82 | + pinMode(VALVE_PIN, OUTPUT); |
| 83 | + digitalWrite(VALVE_PIN, LOW); |
| 84 | + #ifdef DEBUG |
| 85 | + Serial.println("Solenoid valve initialized"); |
| 86 | + #endif |
| 87 | +} |
| 88 | + |
| 89 | +void init_servo() |
| 90 | +{ |
| 91 | + pinMode(SERVO_PIN, OUTPUT); |
| 92 | + servo.attach(SERVO_PIN); |
| 93 | + close_cap(); |
| 94 | + #ifdef DEBUG |
| 95 | + Serial.println("Servo init done"); |
| 96 | + #endif |
| 97 | +} |
| 98 | + |
| 99 | +void init_buzzer() |
| 100 | +{ |
| 101 | + pinMode(BUZZER_PIN, INPUT); |
| 102 | + #ifdef DEBUG |
| 103 | + Serial.println("Buzzer pin set"); |
| 104 | + #endif |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +/* Control functions */ |
| 109 | + |
| 110 | +void open_valve() |
| 111 | +{ |
| 112 | + #ifdef DEBUG |
| 113 | + Serial.println("Opening solenoid valve"); |
| 114 | + #endif |
| 115 | + digitalWrite(VALVE_PIN, HIGH); |
| 116 | +} |
| 117 | + |
| 118 | +void close_valve() |
| 119 | +{ |
| 120 | + #ifdef DEBUG |
| 121 | + Serial.println("Closing solenoid valve"); |
| 122 | + #endif |
| 123 | + digitalWrite(VALVE_PIN, LOW); |
| 124 | +} |
| 125 | + |
| 126 | +void ignite() |
| 127 | +{ |
| 128 | + #ifdef DEBUG |
| 129 | + Serial.println("Ignition!"); |
| 130 | + #endif |
| 131 | + digitalWrite(IGNITOR_PIN, HIGH); |
| 132 | + delay(IGNITOR_DELAY); |
| 133 | +} |
| 134 | + |
| 135 | +void switch_off_ignitor() |
| 136 | +{ |
| 137 | + #ifdef DEBUG |
| 138 | + Serial.println("Ignitor switch-off"); |
| 139 | + #endif |
| 140 | + digitalWrite(IGNITOR_PIN, LOW); |
| 141 | +} |
| 142 | + |
| 143 | +void open_cap() |
| 144 | +{ |
| 145 | + #ifdef DEBUG |
| 146 | + Serial.println("Opening silo cap"); |
| 147 | + #endif |
| 148 | + servo.write(0); |
| 149 | + delay(SERVO_DELAY); |
| 150 | +} |
| 151 | + |
| 152 | +void close_cap() |
| 153 | +{ |
| 154 | + #ifdef DEBUG |
| 155 | + Serial.println("Closing silo cap"); |
| 156 | + #endif |
| 157 | + servo.write(120); |
| 158 | + delay(SERVO_DELAY); |
| 159 | +} |
| 160 | + |
| 161 | +void beep(int ghz, int demanded_delay) |
| 162 | +{ |
| 163 | + tone(BUZZER_PIN, ghz, demanded_delay); |
| 164 | + delay(demanded_delay); |
| 165 | + pinMode(BUZZER_PIN, INPUT); |
| 166 | + delay(demanded_delay); |
| 167 | +} |
| 168 | + |
| 169 | +void beep_count(int repeats) |
| 170 | +{ |
| 171 | + for (int i = 0; i < repeats; i++) |
| 172 | + { |
| 173 | + beep(BUZZER_FREQUENCY, BUZZER_DELAY); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +/* Main logic */ |
| 179 | +void launch_sequence() |
| 180 | +{ |
| 181 | + #ifdef DEBUG |
| 182 | + Serial.println("Launch sequence initiated"); |
| 183 | + #endif |
| 184 | + delay(LAUNCH_DELAY); |
| 185 | + #ifdef DEBUG |
| 186 | + Serial.println("Launch imminent"); |
| 187 | + #endif |
| 188 | + beep_count(BUZZER_START); |
| 189 | + // Launch! |
| 190 | + open_cap(); |
| 191 | + ignite(); |
| 192 | + open_valve(); |
| 193 | + #ifdef DEBUG |
| 194 | + Serial.println("Launch done, now tear-down"); |
| 195 | + #endif |
| 196 | + // Teardown |
| 197 | + switch_off_ignitor(); |
| 198 | + delay(POST_LAUNCH_DELAY); |
| 199 | + close_valve(); |
| 200 | + close_cap(); |
| 201 | + #ifdef DEBUG |
| 202 | + Serial.println("Teardown completed, calling for maintenance"); |
| 203 | + #endif |
| 204 | +} |
| 205 | + |
| 206 | +void loop() |
| 207 | +{ |
| 208 | + beep_count(BUZZER_OK); |
| 209 | + delay(BUZZER_LAST_DELAY); |
| 210 | +} |
0 commit comments