Всем доброго дня.
Собрался сделать миди музыкальную катушку Тесла QCW DRSSTC. Понадобился генератор пилообразного напряжения , который выложил в свободном доступе на базе Ардуино один добрый человек(форум высокого напряжения под названием QCW ramp generator). Сам генератор с работой справляется,но выдаёт просто щелчки. А чтобы он воспроизводил музыку , нужно менять частоту пилы от 1 до 10 кГц на лету , т.е. каждые 10-20 миллисекунд на вход приходит меандр определяется его частота и отправляется на генератор пилы.В программировании я слаб , если не трудно, помогите добавить в код нужные строчки.
//QCW DRSSTC Tesla coil Ramp generator. 23-11-2019. Finn Hammer
//Generates the ramp signal, that serves as control reference to the QCW Buck converter.
// UPSLOPE potentiometer controlls the pulse length
// AMPLITUDE potentiometer is self explanatory
// SCALE potentiometer scales output, preserving slope factor
int AMPLITUDE = A0; // Analog input pin that the AMPLITUDE potentiometer is attached to
int PULSELENGTH = A1; // Analog input pin that the PULSELENGTH potentiometer is attached to
int SCALE = A2; // Analog input pin that the SCALE potentiometer is attached to
int BPS = A3; // Analog input pin that the WICK potentiometer is attached to
int WICK = A4; // Analog input pin that the AMPLITUDE potentiometer is attached to
int FIREPin = 2; //Pin connected to the "FIRE" button (debounce the button 20mS)http://www.labbookpages.co.uk/electronics/debounce.html
int oneshotpin = 3;
int pulsePin = 5; // This pin outputs the pulse that starts the controller
int pwmPin = 11; // The PWM pin, outputting the ramp signal
int wicklevel = 0; // Amplitude of the short "Wick" period, before ramp starts to rise
int fire = 1; // Variable determining when the code executes (When you press the FIRE buttton)
long uppot; //Pulselength mapped to microseconds
long pulselength; //pulselength of upslope, scaled
long scalepot;
long amplitudepot;
long amplitude = 0;
long amplup; //Amplitude of upslope relative to time
long ampldown; // Amplitude of downslope relative to time (deltatime)
long downtime ; // Time when ramp goes negative = 1/5th of uptime
long deltatime; //time referred to top of slope.
long BPSrunTime; //time interval setting the BPS
long BPSstartTime; //Time of starting the wick
long BPStimer = 0; // the timer that counts the BPS interval
long upslopetime = 0;
long T = 0; // T is the time reference in the loops.
long nowtime = 0; //Time operator for resetting T
void setup() {
Serial.begin(38400);
pinMode (PULSELENGTH, INPUT );
pinMode (SCALE, INPUT );
pinMode (AMPLITUDE, INPUT );
pinMode (WICK, INPUT );
pinMode (BPS, INPUT );
pinMode(pulsePin, OUTPUT);
pinMode(pwmPin, OUTPUT);
TCCR2B = TCCR2B & B11111000 | B00000001; // set timer 2 divisor to 1 for PWM frequency of 31372.55 Hz
attachInterrupt(digitalPinToInterrupt( oneshotpin ), rampISP, FALLING);
}
void loop () {
fire = digitalRead (FIREPin);
if (fire == 0)
BPSrunTime = (millis () - BPSstartTime);
T = (micros () - nowtime ) ;
if
(T >= 0 && T <= 2000 )
analogWrite (pwmPin, wicklevel);
if (T >= 1000 && T<=1500)
digitalWrite (pulsePin, HIGH);
if ( T >= 2000 && T <= pulselength + 2000)
{
amplup = wicklevel + (amplitude * ( T - 2000 )) / (pulselength ) ; //Here I calculate the amplitude at _this_ time in the loop
analogWrite (pwmPin, amplup);
}
downtime = ( pulselength / 5); //here i set the downslope to 20% of the upslope
deltatime = (T - (pulselength + 2000) ); //here I create a new clock, starting at the top of the ramp
ampldown = (amplup * (downtime - deltatime)) / downtime; //this line defines the downslope, relative to deltatime
if (T >= pulselength + 2000 && T < (pulselength + 2000 + downtime)) // this line defines the down ramp time interval
{
analogWrite (pwmPin, ampldown ); // and here the downslope is generated
}
if ( T >= (pulselength + 2000 + downtime))
analogWrite (pwmPin, 0);
if ( T >= (pulselength + 2000 + downtime + 1000 )) // The UD2.7 pulse is delayed 1000uS to allow ramp low pass filter to settle to 0
digitalWrite (pulsePin, LOW);
if (BPSrunTime >= BPStimer)
{
fire = 1;
//.....................Read the potentiometers and scale their values to fit the purpose ....................
BPStimer = map (analogRead (BPS), 0, 1023, 40, 1000);
wicklevel = map (analogRead (WICK), 0, 1023, 1, 50);
uppot = map(analogRead (PULSELENGTH), 0, 1023, 18000, 7000); //Map the duration of the upslope to microseconds
scalepot = map(analogRead (SCALE), 0, 1023, 1000 , 1); //Map scalepot to even numbers
amplitudepot = map (analogRead (AMPLITUDE), 0, 1023, 253 - wicklevel, 0 ); //map amplitude to 8bit, minus wicklevel
pulselength = (uppot * scalepot) / 1000;
amplitude = (amplitudepot * scalepot) / 1000 ;
nowtime = micros();
BPSstartTime = millis () ;
fire = 1;
}
}
void rampISP ()
{
nowtime = micros();
BPSstartTime = millis () ;
fire = 0;
}