The Mapping function isn't working properly

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);


}

The code is down There and it isn't working properly at all

What does "working" mean?

There and it isn't working properly at all

It is usually helpful if you describe what you expect to see and what you are seeing.

When dealing with floating value constants, it is usually better to type them with decimal points (ie, 1023.0 rather than 1023) to guarantee that all the calcs are done as floats

TheMemberFormerlyKnownAsAWOL:
What does "working" mean?

It simply gives zero for some values and other values it gives its half

Can you re-word this bit?

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,,,,

It sounds like you are looking for a non-linier mapper. But i can'r really follow what you wrote.

-jim lee

jimLee:
Can you re-word this bit?

It sounds like you are looking for a non-linier mapper. But i can'r really follow what you wrote.

-jim lee

Sorry I was writing on hurry,,,,,
I don't want the circuit to interfere before the 50V and after I want to vary the duty cycle to achieve the 50 V.

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