So I'm controlling a wind turbine through a buck converter, the turbine is changing from 0-120v and that corresponds to 0-5.66V input to arduino uno,,,
I don't the circuit to interfere before the 50V which means I want the input to be as same as the output and after the 50V I want the Output to be a percentage from the input,,,,
The code is down There and it isn't working properly at all,,,,,,
May anyone pls tell me what is the wrong with the code?????
#define TOP 799 // Fosc = Fclk/(N*(1+TOP), Fosc = 20kHz, Fosc = 16MHz
#define CMP_VALUE_HALF_DUTY 399 // 50% duty cycle
#define FeedbackPin A5 // feedback pin at A5
#define R1plusR2_resistor 9500 // variable of R1+R2 value
#define R1_resistor 470 // variable of R1 value
#define PWM 9 // PWM(Pulse Width Modulation) wave at pin 9
float Map_ADC(); // function declaration
float Map_ADC() {
// function definition
int Digital_Read = analogRead(FeedbackPin);
// reading analog voltage form 0 to 5.2V and converting it to digital values in between 0 to 1023
float ADC_READ = (Digital_Read / 1023) * 120;
// mapping the digital value into analog voltage of 0 to 5.2V
float mapping_result = (ADC_READ * (R1plusR2_resistor / R1_resistor));
// calculating the actual output voltage
return (mapping_result);
// return the calculated output voltage
}
void setup() {
// put your setup code here, to run once:
pinMode(PWM, OUTPUT); // set 9 pin as output
pinMode(FeedbackPin, INPUT); // set A5 pin as input
TCCR1A = 0; //reset the register
TCCR1B = 0; //reset the register
TCNT1 = 0; //reset the register
TCCR1A |= (1 << COM1A1); // set output at non inverting mode
TCCR1A |= (1 << WGM11); // selecting Fast PWM mode
ICR1 = TOP; // setting frequency to 20KHz
TCCR1B |= (1 << CS10) | (1 << WGM12) | (1 << WGM13); //Timer Starts
// setting PWM of 50% duty cycle
OCR1A = CMP_VALUE_HALF_DUTY;
delay(3000); // delay for compensating the hardware transient time with software
}
void loop() {
float av = Map_ADC();
if ( av > 54) {
OCR1A ++;
}
else if (av < 54) {
//comparing actual output voltage with desired output voltage to find error in voltage
OCR1A --;
// if error is negative the duty cycle is increased for a regulated voltage of 5V
}
delay(3000);
}