All the working programs are on my desktop PC, but this looks like the working program with lots of serial print stuff for debugging etc
I used the asin function (offline) to write a step time array as the mac series took too long
Not the right program but it looks similar it shows my programming method and how noob I am! a little C with some wiring
#include <avr/io.h>
#include <avr/interrupt.h>
#include <math.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;
float T_OLD;
float T_NEW;
float X_OLD;
float X_NEW;
float INV_SIN;
float OMEGA;
float FLOAT_index;
volatile int n;
int MainRef[81] = {
431, 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,
};
volatile int index;
volatile int increment ;
int REF = MainRef[index];
int OFFSET;
volatile int Iin1;
volatile int Tindex;
volatile int x;
volatile int Iin4;
volatile long int ERR;
int GATE = 12;
int INDEX_TIME [82]={};
int TIMEARRAY [82]={};
void setup(){
Serial.begin(9600);
digitalWrite(6&7, LOW);
enable_my_ints=false;
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(A4)+ 511.0);
float TEST_HERTZ = ((SETUP_VIN)/1020.0)*50.0;
HERTZ =ceil(TEST_HERTZ);
float FLOAT_NUM = HERTZ*1.6 ;
NUM=floor(FLOAT_NUM);
TEST_VIN = SETUP_VIN;
OMEGA = 2*PI*HERTZ;
OFFSET = analogRead(A3) -511;
index = 40;
for(index=40; index<= 40+ NUM/2; index++)
{
X_NEW = ((index-40)/(FLOAT_NUM/2));
INV_SIN = asin(X_NEW);
T_NEW = (1000000)*(INV_SIN/OMEGA);
INDEX_TIME [index] = T_NEW;
}
x=0;
index=40-NUM/2;
for(index>=40-NUM/2; index< 40; index++)
{ INDEX_TIME [index] = -INDEX_TIME [(40+NUM/2-x)];
x=x+1;
}
index = 40-NUM/2;
for(index>=40-NUM/2; index< 40+NUM/2; index++)
{
TIMEARRAY [index]= 2*(INDEX_TIME[(index+1)]-INDEX_TIME[index])-1;
}
for(index=40-NUM/2; index<= 40+NUM/2; index++)
{ Serial.println(INDEX_TIME[index]);
}
Serial.println("MAIN_REF ");
for(index=40-NUM/2; index<= 40+NUM/2; index++)
{ Serial.println(MainRef[index]);
}
Serial.println("TIME ARRAY ");
for(index=40-NUM/2; index<= 39+NUM/2; index++)
{ Serial.println(TIMEARRAY[index]);
}
index = 40+NUM/2;
Serial.print("HERTZ ");
Serial.println(HERTZ);
Serial.print("TEST_VIN ");
Serial.println(TEST_VIN);
Serial.print("T_NEW ");
Serial.println(T_NEW);
Serial.print("X_NEW ");
Serial.println(X_NEW,9);
Serial.print("T_OLD ");
Serial.println(T_OLD);
Serial.print("OMEGA ");
Serial.println(OMEGA);
Serial.print("INV_SIN ");
Serial.println(INV_SIN, 9);
Serial.print("NUM ");
Serial.println(NUM);
Serial.print("index ");
Serial.println(index);
Serial.print("refindex ");
Serial.println(MainRef[index]);
digitalWrite(6, HIGH);
Serial.print("HERTZ ");
Serial.println(HERTZ);
Serial.print("STEP TIME ");
Serial.println(STEP_TIME);
enable_my_ints= true;
READY=true;
count = 0;
increment=-1;
index =40+NUM/2;
cli();
TCCR1A=0;
TCCR1B=0;
Iin4= TIMEARRAY[index];
Serial.println(Iin4);
OCR1A = Iin4;
TCCR1B|=(1<<WGM12);
TCCR1B|=(1<<CS11);
TIMSK1|=(1<<OCIE1A);
sei();
}
ISR(TIMER1_COMPA_vect){
if( enable_my_ints == true){digitalWrite(6,HIGH);
index = index+increment;
Tindex=index;
if(Tindex>=40+NUM/2)
{Tindex=39+NUM/2;
}
if(Tindex<=40-NUM/2)
{Tindex=41-NUM/2;
}
Iin4= TIMEARRAY[Tindex];
OCR1A = Iin4;
if(index<=40 -(NUM/2))
{increment=1;
}
else{if (index>=40 + NUM/2)
{increment=-1;}
}
count = count+1;
if(count>= NUM/2 ){
enable_my_ints =false;
READY=false;
cli();
loop();
}
}
}
void loop(){
while(READY==false){digitalWrite(6,LOW);
if(digitalRead(7)==LOW){
enable_my_ints =true;
READY=true;
count=0;
TCNT1=0x00;
sei();
break;}
}
Iin1=analogRead(A3) -OFFSET;
ERR = MainRef[index]-Iin1;
if (ERR>= 2){
digitalWrite(GATE, LOW);
}
else{if (ERR <= -2){
digitalWrite(GATE, HIGH );}
}
}