1

I want to give different PWM (D11) signals based on how much voltage is input (A7). How can I change PWM for different analog values?

#include <LiquidCrystal.h>          
#include <ezButton.h>        

ezButton button(7);        
int count;       
int count1;          

const float Pin0=A0;       
const float Pin1=A1;      
const float Pin2=A2;       
const float Pin7=A7;            

float valueA;    
float valueB;       
float valueC;      
int valueD;       

const float rs = 1, en = 2, d4 = 3, d5 = 4, d6 = 5, d7 = 6;      

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);          

void setup() {       
  TCCR2B = 0b00000001; // x1      
  TCCR2A = 0b00000011; // fast pwm       
  pinMode(11, OUTPUT);       
  pinMode(9, OUTPUT);        
  button.setDebounceTime(5); // set debounce time to 50 milliseconds       
  button.setCountMode(COUNT_FALLING);         
  lcd.begin(16, 1);          
  pinMode(Pin0, INPUT);           
  pinMode(Pin1, INPUT);              
  pinMode(Pin2, INPUT);          
  pinMode(Pin7, INPUT);             
}           

void loop() {                        
  float a;            
  float b;               
  float c;               

  valueD = analogRead(Pin7);                

  digitalWrite(9, HIGH);             
  digitalWrite(9, LOW);                    
  valueA = analogRead(Pin0);                  
  digitalWrite(9, HIGH);                  

  a = valueA * 5 / 1023;                
  b = valueB * 5 / 1023 * 1.91 - a;                
  c = valueC * 5 / 1023 * 0.182;                 

  valueB = analogRead(Pin1);                  
  valueC = analogRead(Pin2);                    

  button.loop();                   
  int count = button.getCount();                  
  count1 = constrain(count, 0, 4);                   
  if (count1 == 0) {                  
    lcd.print("Bal-Chg");                 
  }                   
  if (count1 == 1) {                     
    lcd.print("V1: ");                     
    lcd.print(a);                    
    lcd.print("V");                    
  }                 
  if (count1 == 2) {                
    lcd.print("V2: ");                 
    lcd.print(b);                     
    lcd.print("V");                 
  }                
  if (count1 == 3) {                  
    lcd.print("I:");               
    lcd.print(c);                
    lcd.print("A");              
  }               
  if (count1 == 4) {                 
    button.resetCount();                  
  }             

  delay(500);               
  lcd.clear();                
}                     
2
  • gammon.com.au/forum/?id=11504&reply=6#reply6 is an example of using a potentiometer (on an analog input) to control the duty cycle of hardware timer. This is example is especially useful if you don't want to work with standard frequencies that analogWrite() restricts you to.
    – 6v6gt
    Commented May 14, 2023 at 18:45
  • Are you using an Arduino UNO or some other processor?
    – Gil
    Commented Jun 19, 2023 at 5:09

1 Answer 1

1

The map() function can help. Supply it the minimum and maximum possible values of the A/D output, and the minimum and maximum inputs to the PWM. (This as assuming you expect the full range of inputs and want the full range of outputs.) It will do the calculations for you. You'll have to code reading the A/D and writing the PWM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.