Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ==============================================================
- Basic Tesla Coil Interrupter V2
- • Clean mode switching (full reset on change)
- • Burst Mode in SSTC + DRSSTC (Using A3)
- • A3 = smoothing in AUDIO
- • Audio mode uses 3-pot adjustment (This still needs a lot of work)
- ==============================================================
- */
- #include <Arduino.h>
- #include <EEPROM.h>
- // ---------------- Pins ----------------
- const uint8_t burstPotPin = A0;
- const uint8_t freqPotPin = A1;
- const uint8_t audioPin = A2;
- const uint8_t smoothPotPin = A3;
- const uint8_t modeButtonPin = 2;
- const uint8_t cwPin = 3;
- const uint8_t armPin = 4;
- const uint8_t ledSSTC = 5;
- const uint8_t ledDRSSTC = 6;
- const uint8_t ledAUDIO = 7;
- const uint8_t ledCW = 8;
- const uint8_t outPin = 9; // OC1A
- const uint8_t ledPulse = 10; // Indicator mirror
- // ---------------- Modes ----------------
- enum Mode { MODE_SSTC = 0, MODE_DRSSTC, MODE_AUDIO, MODE_CW };
- Mode selectedMode = MODE_SSTC;
- Mode activeMode = MODE_SSTC;
- Mode lastMode = MODE_SSTC;
- // ---------------- State ----------------
- bool outputEnabled = false;
- // ---------------- SSTC ----------------
- unsigned long sstc_cycleStart = 0;
- unsigned long sstc_period = 1000000;
- unsigned long sstc_onTime = 1000;
- bool sstc_high = false;
- uint16_t sstc_filtFreq = 0;
- uint16_t sstc_filtBurst = 0;
- uint16_t sstc_burstCounter = 0;
- bool sstc_burstGate = true;
- // ---------------- DRSSTC ----------------
- uint16_t drs_top = 1000;
- uint16_t drs_duty = 100;
- uint16_t drs_lastTCNT = 0;
- uint16_t drs_burstCounter = 0;
- bool drs_burstGate = true;
- // ---------------- AUDIO ----------------
- int32_t audioFiltered = 0;
- // ---------------- Debounce ----------------
- bool lastStableButtonState;
- bool lastButtonReading;
- unsigned long lastDebounceTime = 0;
- const unsigned long debounceDelay = 20;
- // ======================================================
- // ---------------- Startup Sweep -----------------------
- // ======================================================
- void startupSweep()
- {
- uint8_t leds[] = {ledSSTC, ledDRSSTC, ledAUDIO, ledCW};
- for (int i = 0; i < 4; i++)
- {
- digitalWrite(leds[i], HIGH);
- delay(120);
- digitalWrite(leds[i], LOW);
- }
- for (int i = 3; i >= 0; i--)
- {
- digitalWrite(leds[i], HIGH);
- delay(120);
- digitalWrite(leds[i], LOW);
- }
- }
- // ======================================================
- // ---------------- Hard Reset --------------------------
- // ======================================================
- void resetAll()
- {
- TCCR1A = 0;
- TCCR1B = 0;
- TCNT1 = 0;
- ICR1 = 0;
- OCR1A = 0;
- OCR1B = 0;
- digitalWrite(outPin, LOW);
- digitalWrite(ledPulse, LOW);
- sstc_high = false;
- sstc_cycleStart = micros();
- sstc_filtFreq = 0;
- sstc_filtBurst = 0;
- sstc_burstCounter = 0;
- sstc_burstGate = true;
- drs_lastTCNT = 0;
- drs_burstCounter = 0;
- drs_burstGate = true;
- audioFiltered = 0;
- }
- // ======================================================
- // ---------------- Timer Setup -------------------------
- // ======================================================
- void setupTimer_DRSSTC()
- {
- TCNT1 = 0;
- TCCR1A = 0;
- TCCR1B = 0;
- TCCR1A |= (1 << WGM11);
- TCCR1B |= (1 << WGM12) | (1 << WGM13);
- TCCR1A |= (1 << COM1A1);
- TCCR1A |= (1 << COM1B1);
- TCCR1B |= (1 << CS11); // prescaler 8
- }
- void setupTimer_AUDIO()
- {
- TCNT1 = 0;
- TCCR1A = 0;
- TCCR1B = 0;
- TCCR1A |= (1 << WGM11);
- TCCR1B |= (1 << WGM12) | (1 << WGM13);
- TCCR1A |= (1 << COM1A1);
- TCCR1A |= (1 << COM1B1);
- TCCR1B |= (1 << CS10); // prescaler 1
- }
- // ======================================================
- // ---------------- Mode Entry --------------------------
- // ======================================================
- void enterMode(Mode m)
- {
- resetAll();
- if (m == MODE_DRSSTC)
- {
- setupTimer_DRSSTC();
- uint16_t freqVal = analogRead(freqPotPin);
- uint16_t burstVal = analogRead(burstPotPin);
- uint32_t freq = 50UL + (950UL * freqVal) / 1023UL;
- uint32_t period_us = 1000000UL / freq;
- drs_top = period_us * 2UL;
- uint32_t burst_us = 20UL + (180UL * burstVal) / 1023UL;
- drs_duty = burst_us * 2UL;
- if (drs_duty > (drs_top * 9UL) / 10UL)
- drs_duty = (drs_top * 9UL) / 10UL;
- ICR1 = drs_top;
- OCR1A = drs_duty;
- OCR1B = drs_duty;
- drs_lastTCNT = TCNT1;
- }
- else if (m == MODE_AUDIO)
- {
- setupTimer_AUDIO();
- }
- }
- // ======================================================
- // ---------------- SSTC -------------------------------
- // ======================================================
- void runSSTC()
- {
- uint16_t burstControl = analogRead(smoothPotPin);
- uint16_t cyclesOn = 3 + (burstControl * 30UL) / 1023UL;
- uint16_t cyclesOff = cyclesOn * 3;
- sstc_filtFreq = (sstc_filtFreq*3 + analogRead(freqPotPin)) >> 2;
- sstc_filtBurst = (sstc_filtBurst*3 + analogRead(burstPotPin)) >> 2;
- unsigned long now = micros();
- if (!sstc_high && (now - sstc_cycleStart >= sstc_period))
- {
- sstc_cycleStart = now;
- uint32_t bps = 1 + (59UL * sstc_filtFreq) / 1023UL;
- sstc_period = 1000000UL / bps;
- uint32_t maxOn = (sstc_period * 9UL) / 10UL;
- uint32_t curved = (uint32_t)sstc_filtBurst * sstc_filtBurst / 1023UL;
- sstc_onTime = (maxOn * curved) / 1023UL;
- if (sstc_onTime < 1000UL) sstc_onTime = 1000UL;
- sstc_high = true;
- if (burstControl < 10)
- {
- sstc_burstGate = true;
- sstc_burstCounter = 0;
- }
- else
- {
- sstc_burstCounter++;
- if (sstc_burstGate && sstc_burstCounter >= cyclesOn)
- {
- sstc_burstGate = false;
- sstc_burstCounter = 0;
- }
- else if (!sstc_burstGate && sstc_burstCounter >= cyclesOff)
- {
- sstc_burstGate = true;
- sstc_burstCounter = 0;
- }
- }
- }
- if (sstc_high && (now - sstc_cycleStart >= sstc_onTime))
- sstc_high = false;
- bool final = (outputEnabled && sstc_high && sstc_burstGate);
- digitalWrite(outPin, final);
- digitalWrite(ledPulse, final);
- }
- // ======================================================
- // ---------------- DRSSTC -----------------------------
- // ======================================================
- void runDRSSTC()
- {
- uint16_t burstControl = analogRead(smoothPotPin);
- uint16_t cyclesOn = 5 + (burstControl * 95UL) / 1023UL;
- uint16_t cyclesOff = cyclesOn * 4;
- uint16_t freqVal = analogRead(freqPotPin);
- uint16_t burstVal = analogRead(burstPotPin);
- uint32_t freq = 50UL + (950UL * freqVal) / 1023UL;
- uint32_t period_us = 1000000UL / freq;
- uint16_t new_top = period_us * 2UL;
- uint16_t burst_us = 20UL + (180UL * burstVal) / 1023UL;
- uint16_t new_duty = burst_us * 2UL;
- if (new_duty > (new_top * 9UL) / 10UL)
- new_duty = (new_top * 9UL) / 10UL;
- uint16_t nowTCNT = TCNT1;
- if (nowTCNT < drs_lastTCNT)
- {
- drs_top = new_top;
- drs_duty = new_duty;
- ICR1 = drs_top;
- if (burstControl < 10)
- {
- drs_burstGate = true;
- drs_burstCounter = 0;
- }
- else
- {
- drs_burstCounter++;
- if (drs_burstGate && drs_burstCounter >= cyclesOn)
- {
- drs_burstGate = false;
- drs_burstCounter = 0;
- }
- else if (!drs_burstGate && drs_burstCounter >= cyclesOff)
- {
- drs_burstGate = true;
- drs_burstCounter = 0;
- }
- }
- }
- drs_lastTCNT = nowTCNT;
- bool final = (outputEnabled && drs_burstGate);
- if (final)
- {
- OCR1A = drs_duty;
- OCR1B = drs_duty;
- }
- else
- {
- OCR1A = 0;
- OCR1B = 0;
- }
- }
- // ======================================================
- // ---------------- AUDIO ------------------------------
- // ======================================================
- void runAudio()
- {
- const uint16_t top = 512;
- ICR1 = top;
- uint16_t baseline = map(analogRead(burstPotPin), 0, 1023,
- top * 5 / 100,
- top * 95 / 100);
- uint16_t depth = analogRead(freqPotPin);
- int32_t sample = analogRead(audioPin) - 512;
- uint16_t smoothVal = analogRead(smoothPotPin);
- uint8_t smoothShift = map(smoothVal, 0, 1023, 0, 3);
- smoothShift = 3 - smoothShift;
- audioFiltered += (sample - audioFiltered) >> smoothShift;
- sample = audioFiltered;
- sample += 512;
- int32_t modulation = (sample * depth * 3) / 1023;
- modulation -= (depth * 3) / 2;
- int32_t duty = baseline + modulation;
- if (duty < top * 5 / 100) duty = top * 5 / 100;
- if (duty > top * 95 / 100) duty = top * 95 / 100;
- uint16_t finalDuty = outputEnabled ? duty : 0;
- OCR1A = finalDuty;
- OCR1B = finalDuty;
- }
- // ======================================================
- // ---------------- Setup -------------------------------
- // ======================================================
- void setup()
- {
- pinMode(outPin, OUTPUT);
- pinMode(ledPulse, OUTPUT);
- pinMode(modeButtonPin, INPUT_PULLUP);
- pinMode(cwPin, INPUT_PULLUP);
- pinMode(armPin, INPUT_PULLUP);
- pinMode(ledSSTC, OUTPUT);
- pinMode(ledDRSSTC, OUTPUT);
- pinMode(ledAUDIO, OUTPUT);
- pinMode(ledCW, OUTPUT);
- startupSweep();
- lastButtonReading = digitalRead(modeButtonPin);
- lastStableButtonState = lastButtonReading;
- byte saved = EEPROM.read(0);
- if (saved <= 2) selectedMode = (Mode)saved;
- activeMode = selectedMode;
- lastMode = activeMode;
- enterMode(activeMode);
- }
- // ======================================================
- // ---------------- Loop -------------------------------
- // ======================================================
- void loop()
- {
- bool reading = digitalRead(modeButtonPin);
- if (reading != lastButtonReading)
- lastDebounceTime = millis();
- if ((millis() - lastDebounceTime) > debounceDelay)
- {
- if (reading != lastStableButtonState)
- {
- lastStableButtonState = reading;
- if (lastStableButtonState == LOW)
- {
- selectedMode = (Mode)((selectedMode + 1) % 3);
- EEPROM.update(0, selectedMode);
- }
- }
- }
- lastButtonReading = reading;
- bool cwActive = (digitalRead(cwPin) == LOW);
- if (cwActive)
- activeMode = MODE_CW;
- else
- activeMode = selectedMode;
- if (activeMode != lastMode)
- {
- enterMode(activeMode);
- lastMode = activeMode;
- }
- outputEnabled = (digitalRead(armPin) == LOW);
- digitalWrite(ledSSTC, activeMode == MODE_SSTC);
- digitalWrite(ledDRSSTC, activeMode == MODE_DRSSTC);
- digitalWrite(ledAUDIO, activeMode == MODE_AUDIO);
- digitalWrite(ledCW, activeMode == MODE_CW);
- switch (activeMode)
- {
- case MODE_SSTC: runSSTC(); break;
- case MODE_DRSSTC: runDRSSTC(); break;
- case MODE_AUDIO: runAudio(); break;
- case MODE_CW:
- digitalWrite(outPin, outputEnabled);
- digitalWrite(ledPulse, outputEnabled);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment