So I have been double busy trying to write program to control a three phase inverter
the middle leg is driven at 16kHz square and the other two legs each use an Arduino each
I use a reference table and an interrupt to cycle through at the calculated time according to an input voltage from a pot on A0
I use 0-2.5V relating to 25Hz-50Hz and the slower the speed the less the amplitude of the current, I also need to synchronise with the other arduino so that the two cycle through the table in a locked manner!
#include <TimerOne.h>
volatile boolean enable_my_ints;
volatile boolean READY;
volatile int TEST_VIN;
volatile int TEST_VIN2;
float TEST_HERTZ;
float TEST_HERTZ2;
float FLOAT_NUM;
float FLOAT_NUM2;
volatile int NUM;
volatile int NUM2;
volatile int HERTZ;
volatile int HERTZ2;
volatile int STEP_TIME;
volatile int STEP_TIME2;
volatile int Vin;
volatile int count;
int MainRef[] = {
433, 435, 437, 439, 441, 443, 445, 447, 449, 451, 453, 455, 457,
459, 461, 463, 465, 467, 469, 471, 473, 475, 477, 479, 481, 483,
485, 487, 489, 491, 493, 495, 497, 499, 501, 503, 505, 507, 509,
511,
513, 515, 517, 519, 521, 523, 525, 527, 529, 531, 533, 535, 537,
539, 541, 543, 545, 547, 549, 551, 553, 555, 557, 559, 561, 563,
565, 567, 569, 571, 573, 575, 577, 579, 581, 583, 585, 587, 589,
591, 593
};
volatile int index;
volatile int increment ;
long unsigned int REF = MainRef[index];
int Iin;
int ERR;
int GATE = 12;
void setup(){
Serial.begin(9600);
digitalWrite(6, HIGH);
READY=false;
pinMode(13, OUTPUT);
pinMode(GATE, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, INPUT);
pinMode(6, OUTPUT);
bitClear(ADCSRA,ADPS0) ;
bitClear(ADCSRA,ADPS1) ;
bitSet(ADCSRA,ADPS2) ;
float SETUP_VIN =(analogRead(A0)+ 494.0);
float TEST_HERTZ = ((SETUP_VIN)/1020.0)*50.0;
HERTZ =ceil(TEST_HERTZ);
float FLOAT_NUM = HERTZ*1.6;
NUM=ceil(FLOAT_NUM);
TEST_VIN = SETUP_VIN;
STEP_TIME = (1000000)/(HERTZ*2*NUM);
index = 40;
count = 0;
increment=1;
Timer1.initialize();
Timer1.disablePwm(9);
Timer1.disablePwm(10);
digitalWrite(6, LOW);
Serial.println(STEP_TIME);
Serial.print("HERTZ ");
Serial.println(HERTZ);
Serial.print("TEST_VIN ");
Serial.println(TEST_VIN);
Serial.print("NUM ");
Serial.println(NUM);
Serial.print("index ");
Serial.println(index);
Serial.print("refindex ");
Serial.println(MainRef[index]);
digitalWrite(6, LOW);
while (digitalRead(7) == HIGH) {Serial.println("7 is HIGH... ");
if(digitalRead(7) == LOW){
break;}
}
enable_my_ints= true;
READY=true;
Timer1.attachInterrupt(flash);
Timer1.setPeriod(50000);
}
void flash ()
{
if( enable_my_ints == true){
digitalWrite(13, HIGH);
if (index<=40 -(NUM/2)){
(increment=1);}
else{if (index>=(40 + (NUM/2))){
increment=-1;}
}
index = index+increment;
count = count+1;
if(count>= 20*NUM){
noInterrupts();
enable_my_ints =false;
digitalWrite(6,HIGH);
READY=false;
digitalWrite(13, LOW);
count=0;
loop();}
Iin=analogRead(A1);
ERR = MainRef[index]-Iin;
if (ERR>= 2){
digitalWrite(GATE, HIGH);
}
else{
if (ERR <= -2){
digitalWrite(GATE, LOW);}
}
digitalWrite(13, LOW);
}
}
void loop(){
Serial.println(MainRef[index]);
while(READY==false){
noInterrupts();
digitalWrite(13, HIGH);
enable_my_ints =false;
if(digitalRead(7)==LOW){
digitalWrite(13,LOW);
digitalWrite(6,LOW);
enable_my_ints =true;
READY=true;
interrupts();
break;}
}
Vin=(analogRead(A0)+494);
volatile int x = abs(TEST_VIN - Vin);
if (x>15){
delay(10);
Vin=(analogRead(A0)+494);
x = abs(TEST_VIN - Vin);
if(x>50){
digitalWrite(6,HIGH);
noInterrupts();
enable_my_ints = false;
volatile int SETUP_VIN2 =(analogRead(A0)+494);
TEST_VIN2 = Vin;
TEST_VIN = TEST_VIN2;
Vin=analogRead(A0+520);
float TEST_HERTZ2 = ((SETUP_VIN2)/1020.0)*50.0;
float FLOAT_NUM2 = TEST_HERTZ2*1.6;
NUM2=ceil(FLOAT_NUM2);
HERTZ2 =ceil(TEST_HERTZ2);
STEP_TIME2 = (1000000)/(HERTZ2*2*NUM2);
index = 40;
increment=1;
count =0;
Serial.print("x ");
Serial.println(x);
Serial.print("NEWSTEP_TIME ");
Serial.println(STEP_TIME2);
Serial.print("TEST_VIN ");
Serial.println(TEST_VIN);
Serial.print("HERTZ2 ");
Serial.println(HERTZ2);
Serial.print("NUM2 ");
Serial.println(NUM2);
while ( digitalRead(7) == HIGH) {
if(digitalRead(7) == LOW){
break;}
}
enable_my_ints= true;
Timer1.setPeriod(STEP_TIME2);
interrupts();
}
}
}
The other program is virtually identical apart from array size and starting index etc
I am just looking for tips and ways I could improve the code thats all as tbh I have struggled with some parts of the program
Thanks in advance