Changing PWM value in function of an other pin value

Hi guys,

I m to ask for your help once again.

I made a wattmeter for monitoring of consuption of a brushless motor and everything works fine.

I control the ESC via manual PWM signal

Now i would like to get a value of PWM in funcion of analog read of watt sensor

Example:

Automatic acceleration writing PWM signal on pin 11 (1,2,3,4...10....50...2000...255), at the same moment reading the value of the analog pin for wattmeter and change the value of PWM tryin to make costant the value po analog pin for wattmeter that i chiose

Something like this

Value o range i chiose : 30-35 watt

Read anog pin 10 watt, and accelerate till the value will be between 30 and 35 and decelerate in case will go over

Thank you

I use this code to change the value of PWM pin

int ESCpin = 11;    // ESC signal wire connected to pin 11 on arduino
int val = 0;        //variable to store PWM value
 
void setup() 
{ 
    Serial.begin(9600);

} 
 
 
void loop() 
{ 
  for(val = 178; val <= 255; val += 1)
  {
    analogWrite(ESCpin, val);
    delay(8000);
    Serial.println (val);

  }
}

and this code to find the value of watt

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


  float average = 0;
  for(int i = 0; i < 1000; i++) {
    average = average + (0.0049 * analogRead(A4)/0.185 -13.5447) / 1000;
    delay(1);
  }
  Serial.print("mAmp..."); 
  Serial.println(average*1000);  
  Serial.print("Watt..."); 
  Serial.println(average*37); 
}

how can i combine them?

how can i combine them?

First, you define what the combined program should do. After that, it's really a matter of cut and paste.

Of course, in the combined program you are NOT going to be using delay() at all.

thank you PoulS

so the combination should be like this

int ESCpin = 11;    // ESC signal wire connected to pin 11 on arduino
int Throttle= 0;        //variable to store PWM value
 

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


  float average = 0;
  for(int i = 0; i < 1000; i++) {
    average = average + (0.0049 * analogRead(A4)/0.185 -13.5447) / 1000;
    delay(1);
  }
  
      for(Throttle= 0; Throttle<= 255; Throttle+= 1)
  {
    analogWrite(ESCpin, Throttle);

  }
  
  Serial.print("mAmp..."); 
  Serial.println(average*1000);  
  Serial.print("Watt..."); 
  Serial.println(average*37); 
  Serial.print("PWM..."); 
  Serial.println(Throttle); 


}

but i would like to make an algorithm that will check all the time the value of Watt and adapt the value of throttle to the value of watts

is ti possible?

i thought about something like this but didn't get any result

int Throttle = 1;   
int MaxWatt= 10.0;
   
    if (Energy < MaxWatt) 
  {
    for(Throttle > 0; Throttle <= 255; Throttle += 1)
   {
     analogWrite(ESCpin, Throttle);
  }
}
   if (Energy > MaxWatt) 
   {
    for(Throttle > 0; Throttle <= 255; Throttle -= 1)
    {analogWrite(ESCpin, Throttle);
   }
   }

doctor_t,

From what you are describing, it sounds like you are looking to make a PID controller.

Sudo code that could get you a first pass on what you are wanting:

Throttle = start point
loop start
   Check Current Power
   if Current Power < Target Power: increase Throttle
   if Current power > Target Power: decrease Throttle
loop end

Your power sample code snippet is taking an average over a full second, is that needed for your application?
If so, how rapidly do you need your Throttle to adjust to variations in power usage?

JasonK:
doctor_t,

From what you are describing, it sounds like you are looking to make a PID controller.

Sudo code that could get you a first pass on what you are wanting:

Throttle = start point

loop start
   Check Current Power
   if Current Power < Target Power: increase Throttle
   if Current power > Target Power: decrease Throttle
loop end




Your power sample code snippet is taking an average over a full second, is that needed for your application? 
If so, how rapidly do you need your Throttle to adjust to variations in power usage?

Yes it is exactly what i need!!! :slight_smile: :slight_smile: :slight_smile:

how can i put this code on my sketch?

for sure i would like to make this change as fast as it possible, how fast can it be?

doctor_t:
Yes it is exactly what i need!!! :slight_smile: :slight_smile: :slight_smile:

how can i put this code on my sketch?

for sure i would like to make this change as fast as it possible, how fast can it be?

To answer how fast it can be, I need to know why your example was averaging 1000 (slightly over one second of time) samples for the power measurement.

This is putting your code segments together to fit with my sudo code. It compiles, but I don't have your hardware setup to test its functionality. As written it will only update the Throttle at less then 1 time a second because your code to gather the power average takes 1 second in delay time alone. If you don't need a 1 second average or can use a rolling average (all depends on your application), you could get a more frequently update cycle.

With the current update cycle it will take more then 4.25 minutes to go from no power to full power or full power to no power.

int ESCpin = 11;        // ESC signal wire connected to pin 11 on arduino
int Throttle = 0;       // variable to store PWM value
float GoalWatt = 10.0;   // Set point for Goal wattage
void setup() {
   Serial.begin(9600);
}
 
void loop() {
  float average = 0;
  for(int i = 0; i < 1000; i++) {
    average = average + (0.0049 * analogRead(A4)/0.185 -13.5447) / 1000;
    delay(1);
  }
  Serial.print("mAmp..."); 
  Serial.println(average*1000);  
  Serial.print("Watt..."); 
  Serial.println(average*37); 
  
  if(average > GoalWatt) {          // Higher then goal
    if(Throttle > 0) Throttle--;    // decrease Throttle if greater then min  
  } else if (average < GoalWatt) {  // Lower then goal
    if(Throttle < 255) Throttle++;  // increase Throttle if less then max  
  }
  
  analogWrite(ESCpin, Throttle);    // Update PIN output
}

thx very much Jason

i will may be put 150 in the cycle of calculation of the amps to make the cycle faster because 4.25 min are a bit long

by the way i was trying to read about PID and applications

how can i use pid in my case?

It looks like there is a PID library here: Arduino Playground - PIDLibrary

As my experience with PID is limited at this point, it would reference you to that documentation.

JasonK:
It looks like there is a PID library here: Arduino Playground - PIDLibrary

As my experience with PID is limited at this point, it would reference you to that documentation.

i really appreciate your help and it seems to work well but i understood that with pid you can have a smoother or more aggressive answer of PWM