The code is pasted below.
The platform is an Arduino Due, and I have modified the variant.h file to produce 20kHz PWM
I am new to programming, and this is only my second piece of code.
It generates a sawtooth voltage ramp by PWM on pin6, and an accompanying square wave pulse on pin7.
I transmit this PWM signal via Fiber optic cable, and then smooth it out with a low pass filter and opamp buffer. This is for a remote controll for a special kind of Tesla Coil.
The code is a long series of loops controlled by "if" filters, where the ramp is built up by calculating the amplitude relative to the time.
When I wrote the code, the ramp was generated inside the main loop, and it runs well and does what it should do.
You can see a demonstration of it here:
To be able to turn this ramp off and on, I then declared the contents as a function (void ramp () )
So that I could poll a switch inside the main loop and direct focus on to the ramp function, when I want it to output a ramp.
The switch is connected to pin12, hardware debounced with 2mS of clean signal transition.
This is the way the posted code looks like, but there are a couple of problems, I will start with the biggest first:
When I turn the switch off, the code gets interupted inside the execution of the function, you can see it on the attached photo, where yellow is the square wave on pin7, Magenta is the lowpassfiltered sawtooth wave, and Cyan is the switch.
This is what I don't understand, the switch is in the main loop, but it interferes with the execution of the ramp function.
The way I see it, the processor should complete the ramp function before it goes back to the main loop ( void loop ) and read the switch.
How can I prevent the switch from interfering with the execution of the ramp function in the midle of it?
I am at my wits end, and any pointers will be greatly appreciated.
Cheers, Finn Hammer
//QCW DRSSTC Tesla coil Ramp generator. 29-10-2016. Finn Hammer
//Generates the ramp signal, that serves as control reference to the QCW Buck converter.
// UPSLOPE potentiometer controlls the pulse length
// AMPLITUDE potentiometer controlls the peak amplitude of the ramp
// SCALE potentiometer controlls ontime, while preserving upslope rate
// BPS potentiometer controlls the BPS
// WICK (trim)potentiometer controlls the voltage level of the wick period
// ............................ "Calling names and Pointing Fingers".............................
int analogInPin0 = A0; // Analog input pin that the UPSLOPE potentiometer is attached to
int analogInPin1 = A1; // Analog input pin that the SCALE potentiometer is attached to
int analogInPin2 = A2; // Analog input pin that the AMPLITUDE potentiometer is attached to
int analogInPin3 = A3; // Analog input pin that the BPS potentiometer is attached to
int analogInPin5 = A5; // Analog input pin that the WICK potentiometer is attached to
int inPin = 12; // The number of the input pin of the "Run" switch
int switchValue;
float uppot; // Ramp Risetime mapped to microseconds
float uppot1; // Ramp Risetime after scalepot
float scalepot; // scalefactor mapped to 1-1000
float skewpot; // Not implemented yet
float BPSpot; // bps time (time between ramps)
float amplitudepot; // Amplitude mapped to 8bit
float amplitudepot1; // Amplitude after the scale pot
int pulsePin = 5; // the number of the pulse pin
int pwmPin = 6; // the number of the PWM pin
int wicklevel; // The short "Wick" period, before ramp starts to rise
float amplup; // Amplitude of upslope relative to time
float samplup; // sample of amplup, taken at last pass, before it vanishes in the code at top.
float ampldown; // Amplitude of downslope relative to time (deltatime)
int downtime ; // Time when ramp goes negative = 1/5th of uptime
volatile long T = 0; // T is the time reference in the loops.
// Gets reset to zero every time the ramp starts.
volatile long deltatime; //time referred to top of slope.
volatile long nowtime ; //Time operator for resetting T
void setup() {
Serial.begin(38400);
pinMode(pulsePin, OUTPUT);
pinMode(pwmPin, OUTPUT);
analogWriteResolution (8);
analogReadResolution (10);
pinMode(inPin, INPUT);
digitalWrite(12, HIGH);
}
void loop () {
switchValue = digitalRead (inPin);
if (switchValue == HIGH )
ramp ();
}
//delay (BPSpot);
void ramp()
{
// ................Read the pots and Scale the values to suit what I want to do......................
uppot = map(analogRead(analogInPin0), 0, 1023, 5000, 18000); // Map the duration of the upslope to microseconds
scalepot = map(analogRead(analogInPin1), 0, 1023, 150, 1000); // Map scalepot
BPSpot = map (analogRead(analogInPin3), 0, 1023, 20, 980); // Map BPSpot to between 20-980uS
amplitudepot = map (analogRead(analogInPin2), 0, 1023, 0, 255); // map amplitude to 8bit,
wicklevel = map (analogRead (analogInPin5), 0, 1023, 0, 60);
uppot1 = (uppot / 1000) * scalepot; // Here the ramp is scaled to preserve ramp rate
amplitudepot1 = (amplitudepot / 1000) * scalepot; // Here the ramp is scaled to preserve ramp rate
T = (micros() - nowtime); //here the loop-clock "T" is set (close) to zero
if (T >= 5 && T <= 1500) analogWrite (pwmPin, wicklevel); //Start of the "wick" part of the ramp, to start the oscillations
if (T >= 800)digitalWrite (pulsePin, HIGH); //Here the pulse to start the UD2.7C goes high
if ( T > 1500 && T <= uppot1) { // this is the period where the ramp goes up
amplup = wicklevel + ((amplitudepot1 - wicklevel ) * (T - 1500) / (uppot1 - 1500)); //Here I calculate the amplitude at _this_ time in _this_ loop
samplup = amplup; //here I store a sample of the amplitude for use after T> uppot1
analogWrite (pwmPin, amplup);
} // and here the ramp is generated
downtime = uppot1 / 5; //this sets the downslope length relative to the upslope
deltatime = (T - uppot1); //this creates a new clock, starting at the top of the ramp
ampldown = (samplup * (downtime - deltatime)) / downtime; //this line describes the downslope, relative to deltatime
if (T > uppot1 && T < uppot1 + downtime) // this line defines the down ramp time interval
{
analogWrite (pwmPin, ampldown ); // and here the downslope is generated
}
if ( T >= uppot1 + downtime ) //this defines interval between ramps
{
analogWrite (pwmPin, 0); //rampsignal set to zero (buckconverter idling)
}
if ( T >= uppot1 + downtime + 600 ) // The UD2.7 pulse is delayed 600uS to allow ramp low pass filter to settle to 0
{
digitalWrite (pulsePin, LOW); //signal to UD2.7C is terminated
delay (BPSpot); //this sets the repetition rate between ramps
nowtime = micros(); //updating time operator, for loop clock reset
}
}