hello
I'm a beginner on Arduino. I apologize for my English.
I'm trying to generate sinusoidal half-wave PWMs on pins 5 and 6. Then I will send the sinusoids to two bjt or mosfet connected in push pull and connected on the primary of a transformer as shown in figure 1: "Generatore sinusoidi Schema elettrico.jpg"
Now the outputs are connected, via RC filter, to the inputs of an oscilloscope. The code works perfectly and I get the sinusoids, only that each cycle lasts 120 mS while in Italy we have the mains current frequency at 50 Hz, so 20 mS per cycle: see "Generatore Sinusoidi funzionante con filtro RC.jpg"
The code:
// SINUSOIDE_MIA 5
// Define Pins
#define pin5 5
#define pin6 6
#define maxIndex 78 // (actually the array is 79 units, but to avoid operations during For cycles)
unsigned long ulngDelayT=0; // delay time
unsigned long ulngStartCycleT=0; // start time of last cycle
//original array 312 values from Pier Aisa's video #367 :
// https://www.youtube.com/watch?v=BWouNZAFWG0
// code Pwmsin2canali.ino
// byte sinPWM[]={1,2,5,7,10,12,15,17,19,22,24,27 ...};
//
// array taken from Pier Aisa's arry reduced to 1/4 of the original length,
//taking one element of the array every two,
//and then divided by two in order to store only a 1/4 of the whole wavelength
byte sinPWM[]={
1 ,5 ,10 ,15 ,19 ,24 ,30 ,34 ,39 ,44 ,49 ,54 ,59 ,64 ,69 ,73 ,78 ,83 ,88 ,
92 ,97 ,101,106,110,115,119,124,128,132,136,140,144,148,152,156,160,164,168,
171,175,178,182,185,188,192,195,198,201,204,207,209,212,215,217,220,222,224,
226,228,230,232,234,236,237,239,240,242,243,244,245,246,247,247,248,248,249,
249,250,250};
void setup()
{
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
Serial.begin(115200);
}
// main loop
void loop()
{
// stores new wave start time
ulngStartCycleT=millis();
// 1/4 cycle
for (int i=1; i < maxIndex; i++){
analogWrite(pin5, sinPWM[i]);
Serial.println(sinPWM[i]);// prints the values on plotter or serial monitor
}
// 2/4 cycle
for (int i=maxIndex; i > 0; i--){
analogWrite(pin5, sinPWM[i]);
Serial.println(sinPWM[i]);// prints the values on plotter or serial monitor
}
// to prevent spurious appearance on unused pin
analogWrite(pin5, 0);
// delay to prevent absorption conflict between BJTs
ulngDelayT = millis();
while (millis() - ulngDelayT < 2){};
//
// 3/4 cycle
for (int i=1; i < maxIndex; i++){
analogWrite(pin6, sinPWM[i]);
Serial.println(sinPWM[i]);// prints the values on plotter or serial monitor
}
// 4/4 cycle
for (int i=maxIndex; i > 0; i--){
analogWrite(pin6, sinPWM[i]);
Serial.println(sinPWM[i]);// prints the values on plotter or serial monitor
}
// to prevent spurious appearance on unused pin
analogWrite(pin6, 0);
// delay to prevent absorption conflict between BJTs
ulngDelayT = millis();
while (millis() - ulngDelayT < 2){};
//
// calculates the elapsed time to get a whole wave
ulngDelayT = millis() - ulngStartCycleT;
Serial.print("Cycle time in mS:");
Serial.println(ulngDelayT);
//delay(1000);
}
Let me describe the problem:
to get 20 mS I removed the printing instructions on the serial plotter Serial.println(sinPWM*) but at this point I get a*
mess visible in : "Generatore Sinusoidi NON funzionante con filtro RC.jpg"
I tried everything. I plugged in another RC filter to no avail.
Can anyone help me?