Guest User

V2

a guest
Feb 17th, 2026
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.89 KB | None | 0 0
  1. /*
  2. ==============================================================
  3. Basic Tesla Coil Interrupter V2
  4.  
  5. • Clean mode switching (full reset on change)
  6. • Burst Mode in SSTC + DRSSTC (Using A3)
  7. • A3 = smoothing in AUDIO
  8. • Audio mode uses 3-pot adjustment (This still needs a lot of work)
  9.  
  10. ==============================================================
  11. */
  12.  
  13. #include <Arduino.h>
  14. #include <EEPROM.h>
  15.  
  16. // ---------------- Pins ----------------
  17.  
  18. const uint8_t burstPotPin = A0;
  19. const uint8_t freqPotPin = A1;
  20. const uint8_t audioPin = A2;
  21. const uint8_t smoothPotPin = A3;
  22.  
  23. const uint8_t modeButtonPin = 2;
  24. const uint8_t cwPin = 3;
  25. const uint8_t armPin = 4;
  26.  
  27. const uint8_t ledSSTC = 5;
  28. const uint8_t ledDRSSTC = 6;
  29. const uint8_t ledAUDIO = 7;
  30. const uint8_t ledCW = 8;
  31.  
  32. const uint8_t outPin = 9; // OC1A
  33. const uint8_t ledPulse = 10; // Indicator mirror
  34.  
  35. // ---------------- Modes ----------------
  36.  
  37. enum Mode { MODE_SSTC = 0, MODE_DRSSTC, MODE_AUDIO, MODE_CW };
  38.  
  39. Mode selectedMode = MODE_SSTC;
  40. Mode activeMode = MODE_SSTC;
  41. Mode lastMode = MODE_SSTC;
  42.  
  43. // ---------------- State ----------------
  44.  
  45. bool outputEnabled = false;
  46.  
  47. // ---------------- SSTC ----------------
  48.  
  49. unsigned long sstc_cycleStart = 0;
  50. unsigned long sstc_period = 1000000;
  51. unsigned long sstc_onTime = 1000;
  52. bool sstc_high = false;
  53. uint16_t sstc_filtFreq = 0;
  54. uint16_t sstc_filtBurst = 0;
  55.  
  56. uint16_t sstc_burstCounter = 0;
  57. bool sstc_burstGate = true;
  58.  
  59. // ---------------- DRSSTC ----------------
  60.  
  61. uint16_t drs_top = 1000;
  62. uint16_t drs_duty = 100;
  63. uint16_t drs_lastTCNT = 0;
  64.  
  65. uint16_t drs_burstCounter = 0;
  66. bool drs_burstGate = true;
  67.  
  68. // ---------------- AUDIO ----------------
  69.  
  70. int32_t audioFiltered = 0;
  71.  
  72. // ---------------- Debounce ----------------
  73.  
  74. bool lastStableButtonState;
  75. bool lastButtonReading;
  76. unsigned long lastDebounceTime = 0;
  77. const unsigned long debounceDelay = 20;
  78.  
  79. // ======================================================
  80. // ---------------- Startup Sweep -----------------------
  81. // ======================================================
  82.  
  83. void startupSweep()
  84. {
  85. uint8_t leds[] = {ledSSTC, ledDRSSTC, ledAUDIO, ledCW};
  86.  
  87. for (int i = 0; i < 4; i++)
  88. {
  89. digitalWrite(leds[i], HIGH);
  90. delay(120);
  91. digitalWrite(leds[i], LOW);
  92. }
  93.  
  94. for (int i = 3; i >= 0; i--)
  95. {
  96. digitalWrite(leds[i], HIGH);
  97. delay(120);
  98. digitalWrite(leds[i], LOW);
  99. }
  100. }
  101.  
  102. // ======================================================
  103. // ---------------- Hard Reset --------------------------
  104. // ======================================================
  105.  
  106. void resetAll()
  107. {
  108. TCCR1A = 0;
  109. TCCR1B = 0;
  110. TCNT1 = 0;
  111.  
  112. ICR1 = 0;
  113. OCR1A = 0;
  114. OCR1B = 0;
  115.  
  116. digitalWrite(outPin, LOW);
  117. digitalWrite(ledPulse, LOW);
  118.  
  119. sstc_high = false;
  120. sstc_cycleStart = micros();
  121. sstc_filtFreq = 0;
  122. sstc_filtBurst = 0;
  123. sstc_burstCounter = 0;
  124. sstc_burstGate = true;
  125.  
  126. drs_lastTCNT = 0;
  127. drs_burstCounter = 0;
  128. drs_burstGate = true;
  129.  
  130. audioFiltered = 0;
  131. }
  132.  
  133. // ======================================================
  134. // ---------------- Timer Setup -------------------------
  135. // ======================================================
  136.  
  137. void setupTimer_DRSSTC()
  138. {
  139. TCNT1 = 0;
  140. TCCR1A = 0;
  141. TCCR1B = 0;
  142.  
  143. TCCR1A |= (1 << WGM11);
  144. TCCR1B |= (1 << WGM12) | (1 << WGM13);
  145.  
  146. TCCR1A |= (1 << COM1A1);
  147. TCCR1A |= (1 << COM1B1);
  148.  
  149. TCCR1B |= (1 << CS11); // prescaler 8
  150. }
  151.  
  152. void setupTimer_AUDIO()
  153. {
  154. TCNT1 = 0;
  155. TCCR1A = 0;
  156. TCCR1B = 0;
  157.  
  158. TCCR1A |= (1 << WGM11);
  159. TCCR1B |= (1 << WGM12) | (1 << WGM13);
  160.  
  161. TCCR1A |= (1 << COM1A1);
  162. TCCR1A |= (1 << COM1B1);
  163.  
  164. TCCR1B |= (1 << CS10); // prescaler 1
  165. }
  166.  
  167. // ======================================================
  168. // ---------------- Mode Entry --------------------------
  169. // ======================================================
  170.  
  171. void enterMode(Mode m)
  172. {
  173. resetAll();
  174.  
  175. if (m == MODE_DRSSTC)
  176. {
  177. setupTimer_DRSSTC();
  178.  
  179. uint16_t freqVal = analogRead(freqPotPin);
  180. uint16_t burstVal = analogRead(burstPotPin);
  181.  
  182. uint32_t freq = 50UL + (950UL * freqVal) / 1023UL;
  183. uint32_t period_us = 1000000UL / freq;
  184.  
  185. drs_top = period_us * 2UL;
  186. uint32_t burst_us = 20UL + (180UL * burstVal) / 1023UL;
  187. drs_duty = burst_us * 2UL;
  188.  
  189. if (drs_duty > (drs_top * 9UL) / 10UL)
  190. drs_duty = (drs_top * 9UL) / 10UL;
  191.  
  192. ICR1 = drs_top;
  193. OCR1A = drs_duty;
  194. OCR1B = drs_duty;
  195.  
  196. drs_lastTCNT = TCNT1;
  197. }
  198. else if (m == MODE_AUDIO)
  199. {
  200. setupTimer_AUDIO();
  201. }
  202. }
  203.  
  204. // ======================================================
  205. // ---------------- SSTC -------------------------------
  206. // ======================================================
  207.  
  208. void runSSTC()
  209. {
  210. uint16_t burstControl = analogRead(smoothPotPin);
  211. uint16_t cyclesOn = 3 + (burstControl * 30UL) / 1023UL;
  212. uint16_t cyclesOff = cyclesOn * 3;
  213.  
  214. sstc_filtFreq = (sstc_filtFreq*3 + analogRead(freqPotPin)) >> 2;
  215. sstc_filtBurst = (sstc_filtBurst*3 + analogRead(burstPotPin)) >> 2;
  216.  
  217. unsigned long now = micros();
  218.  
  219. if (!sstc_high && (now - sstc_cycleStart >= sstc_period))
  220. {
  221. sstc_cycleStart = now;
  222.  
  223. uint32_t bps = 1 + (59UL * sstc_filtFreq) / 1023UL;
  224. sstc_period = 1000000UL / bps;
  225.  
  226. uint32_t maxOn = (sstc_period * 9UL) / 10UL;
  227. uint32_t curved = (uint32_t)sstc_filtBurst * sstc_filtBurst / 1023UL;
  228. sstc_onTime = (maxOn * curved) / 1023UL;
  229.  
  230. if (sstc_onTime < 1000UL) sstc_onTime = 1000UL;
  231.  
  232. sstc_high = true;
  233.  
  234. if (burstControl < 10)
  235. {
  236. sstc_burstGate = true;
  237. sstc_burstCounter = 0;
  238. }
  239. else
  240. {
  241. sstc_burstCounter++;
  242. if (sstc_burstGate && sstc_burstCounter >= cyclesOn)
  243. {
  244. sstc_burstGate = false;
  245. sstc_burstCounter = 0;
  246. }
  247. else if (!sstc_burstGate && sstc_burstCounter >= cyclesOff)
  248. {
  249. sstc_burstGate = true;
  250. sstc_burstCounter = 0;
  251. }
  252. }
  253. }
  254.  
  255. if (sstc_high && (now - sstc_cycleStart >= sstc_onTime))
  256. sstc_high = false;
  257.  
  258. bool final = (outputEnabled && sstc_high && sstc_burstGate);
  259.  
  260. digitalWrite(outPin, final);
  261. digitalWrite(ledPulse, final);
  262. }
  263.  
  264. // ======================================================
  265. // ---------------- DRSSTC -----------------------------
  266. // ======================================================
  267.  
  268. void runDRSSTC()
  269. {
  270. uint16_t burstControl = analogRead(smoothPotPin);
  271. uint16_t cyclesOn = 5 + (burstControl * 95UL) / 1023UL;
  272. uint16_t cyclesOff = cyclesOn * 4;
  273.  
  274. uint16_t freqVal = analogRead(freqPotPin);
  275. uint16_t burstVal = analogRead(burstPotPin);
  276.  
  277. uint32_t freq = 50UL + (950UL * freqVal) / 1023UL;
  278. uint32_t period_us = 1000000UL / freq;
  279.  
  280. uint16_t new_top = period_us * 2UL;
  281. uint16_t burst_us = 20UL + (180UL * burstVal) / 1023UL;
  282. uint16_t new_duty = burst_us * 2UL;
  283.  
  284. if (new_duty > (new_top * 9UL) / 10UL)
  285. new_duty = (new_top * 9UL) / 10UL;
  286.  
  287. uint16_t nowTCNT = TCNT1;
  288.  
  289. if (nowTCNT < drs_lastTCNT)
  290. {
  291. drs_top = new_top;
  292. drs_duty = new_duty;
  293. ICR1 = drs_top;
  294.  
  295. if (burstControl < 10)
  296. {
  297. drs_burstGate = true;
  298. drs_burstCounter = 0;
  299. }
  300. else
  301. {
  302. drs_burstCounter++;
  303. if (drs_burstGate && drs_burstCounter >= cyclesOn)
  304. {
  305. drs_burstGate = false;
  306. drs_burstCounter = 0;
  307. }
  308. else if (!drs_burstGate && drs_burstCounter >= cyclesOff)
  309. {
  310. drs_burstGate = true;
  311. drs_burstCounter = 0;
  312. }
  313. }
  314. }
  315.  
  316. drs_lastTCNT = nowTCNT;
  317.  
  318. bool final = (outputEnabled && drs_burstGate);
  319.  
  320. if (final)
  321. {
  322. OCR1A = drs_duty;
  323. OCR1B = drs_duty;
  324. }
  325. else
  326. {
  327. OCR1A = 0;
  328. OCR1B = 0;
  329. }
  330. }
  331.  
  332. // ======================================================
  333. // ---------------- AUDIO ------------------------------
  334. // ======================================================
  335.  
  336. void runAudio()
  337. {
  338. const uint16_t top = 512;
  339. ICR1 = top;
  340.  
  341. uint16_t baseline = map(analogRead(burstPotPin), 0, 1023,
  342. top * 5 / 100,
  343. top * 95 / 100);
  344.  
  345. uint16_t depth = analogRead(freqPotPin);
  346.  
  347. int32_t sample = analogRead(audioPin) - 512;
  348.  
  349. uint16_t smoothVal = analogRead(smoothPotPin);
  350. uint8_t smoothShift = map(smoothVal, 0, 1023, 0, 3);
  351. smoothShift = 3 - smoothShift;
  352.  
  353. audioFiltered += (sample - audioFiltered) >> smoothShift;
  354. sample = audioFiltered;
  355.  
  356. sample += 512;
  357.  
  358. int32_t modulation = (sample * depth * 3) / 1023;
  359. modulation -= (depth * 3) / 2;
  360.  
  361. int32_t duty = baseline + modulation;
  362.  
  363. if (duty < top * 5 / 100) duty = top * 5 / 100;
  364. if (duty > top * 95 / 100) duty = top * 95 / 100;
  365.  
  366. uint16_t finalDuty = outputEnabled ? duty : 0;
  367.  
  368. OCR1A = finalDuty;
  369. OCR1B = finalDuty;
  370. }
  371.  
  372. // ======================================================
  373. // ---------------- Setup -------------------------------
  374. // ======================================================
  375.  
  376. void setup()
  377. {
  378. pinMode(outPin, OUTPUT);
  379. pinMode(ledPulse, OUTPUT);
  380.  
  381. pinMode(modeButtonPin, INPUT_PULLUP);
  382. pinMode(cwPin, INPUT_PULLUP);
  383. pinMode(armPin, INPUT_PULLUP);
  384.  
  385. pinMode(ledSSTC, OUTPUT);
  386. pinMode(ledDRSSTC, OUTPUT);
  387. pinMode(ledAUDIO, OUTPUT);
  388. pinMode(ledCW, OUTPUT);
  389.  
  390. startupSweep();
  391.  
  392. lastButtonReading = digitalRead(modeButtonPin);
  393. lastStableButtonState = lastButtonReading;
  394.  
  395. byte saved = EEPROM.read(0);
  396. if (saved <= 2) selectedMode = (Mode)saved;
  397.  
  398. activeMode = selectedMode;
  399. lastMode = activeMode;
  400.  
  401. enterMode(activeMode);
  402. }
  403.  
  404. // ======================================================
  405. // ---------------- Loop -------------------------------
  406. // ======================================================
  407.  
  408. void loop()
  409. {
  410. bool reading = digitalRead(modeButtonPin);
  411.  
  412. if (reading != lastButtonReading)
  413. lastDebounceTime = millis();
  414.  
  415. if ((millis() - lastDebounceTime) > debounceDelay)
  416. {
  417. if (reading != lastStableButtonState)
  418. {
  419. lastStableButtonState = reading;
  420.  
  421. if (lastStableButtonState == LOW)
  422. {
  423. selectedMode = (Mode)((selectedMode + 1) % 3);
  424. EEPROM.update(0, selectedMode);
  425. }
  426. }
  427. }
  428.  
  429. lastButtonReading = reading;
  430.  
  431. bool cwActive = (digitalRead(cwPin) == LOW);
  432.  
  433. if (cwActive)
  434. activeMode = MODE_CW;
  435. else
  436. activeMode = selectedMode;
  437.  
  438. if (activeMode != lastMode)
  439. {
  440. enterMode(activeMode);
  441. lastMode = activeMode;
  442. }
  443.  
  444. outputEnabled = (digitalRead(armPin) == LOW);
  445.  
  446. digitalWrite(ledSSTC, activeMode == MODE_SSTC);
  447. digitalWrite(ledDRSSTC, activeMode == MODE_DRSSTC);
  448. digitalWrite(ledAUDIO, activeMode == MODE_AUDIO);
  449. digitalWrite(ledCW, activeMode == MODE_CW);
  450.  
  451. switch (activeMode)
  452. {
  453. case MODE_SSTC: runSSTC(); break;
  454. case MODE_DRSSTC: runDRSSTC(); break;
  455. case MODE_AUDIO: runAudio(); break;
  456. case MODE_CW:
  457. digitalWrite(outPin, outputEnabled);
  458. digitalWrite(ledPulse, outputEnabled);
  459. break;
  460. }
  461. }
  462.  
Advertisement
Add Comment
Please, Sign In to add comment