Arduino nano sine wave sync grid

If want to combine output sine wave 1 period to output one channel, how to modify the code, thanks
#include <TimerOne.h>

int f_pwm =18000;
int f_sine =50;
float sinus[400];
float phi=3.14;
int outA = 9;
int outB=10;
int flag = 0;
int spwmen=8;
int sample=0,samples=0;
int potinput ;
int feedbackinput ;
int potinputval;
int feedbackinputval;
int A,max_power=500;
float invert=0.0;
int total_sample;
int phase =0,x=0;
double temp;

void setup() {
Serial.begin(9600);

total_sample =round((((1000000./f_sine)/(1000000./f_pwm))/2.));

for (int sudut= 0; sudut < total_sample; sudut++)
{

float radian = sudut * (180./total_sample) * phi/180;
    sinus[sudut] = sin(radian);

}
float t_pwm = (1000000./f_pwm);
delay(1000);
pinMode(spwmen,OUTPUT);
pinMode(potinput, INPUT);
pinMode(feedbackinput, INPUT);
pinMode(13, INPUT);
A = 400;
digitalWrite(spwmen,LOW);
Timer1.initialize(t_pwm);
Timer1.attachInterrupt(generatespwm);
//samples=(total_sample*2/3);
pinMode(LED_BUILTIN,OUTPUT);
attachInterrupt(digitalPinToInterrupt(2),matching,FALLING);

}
void loop() {
softstart();
}

void generatespwm(){

  sample++;

  if(flag==0){
      Timer1.pwm(outB,0);
      Timer1.pwm(outA,(sinus[sample]* A));
      digitalWrite(spwmen,HIGH);
  }
  if(flag==1){ 
      Timer1.pwm(outA,0);
      Timer1.pwm(outB,(sinus[sample]* A));
      digitalWrite(spwmen,HIGH);
 
  }

}

void softstart(){
if(A<max_power){
A++;
delay(5);
}
}

void matching(){
if(flag==0){
sample=-7;
flag=1;
}else{
sample=0;
flag=0;
}
}

Enclose all your code in code tags and indent it before copy/pasting it - see posting guidelines.

And please explain what you are trying to achieve so the question makes sense.

Here is the example of tested dc-ac inverter with feedbacks and protection circuitry https://github.com/Nebula182/Arduino_spwm_dcac

A few changes could be made to get it working with a grid.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.