Skip to main content
added 1 character in body
Source Link
M_K
  • 47
  • 1
  • 6

Can anybody explain me, why the PWM doesn't work in such a simple example?

Generally, Timer interrupt is being called once per 100 ms and then in the interrupt function the bool flag (state ) is being set. Then in the main loop the flag is checked and when it is 1 (true) several instructions are being executed. Next, PWM is generated and measured via ADC and also displayed on the LCD.

The problem is that the PWM is generated as 0 or sligthly over the 0, even when the value put to the analogWrite is 255 (max of 8 bit). Variable counter is incremented without problems and this value is being put to the analogWrite and despite it the PWM doesn't work.

I don't know, totally, what can be wrong.

Can anybody help me?

 #include<LiquidCrystal.h>
 #include<TimerOne.h>

  LiquidCrystal lcd(52, 50, 48, 46, 44, 42, 40);

  int out = 12; 
  int in = A7; 
  volatile bool state;  

  int counter = 0;
  volatile int y;


  void setup()
  {
    lcd.begin(16, 2);        
    lcd.clear();
    pinMode(out, OUTPUT);
    pinMode(in, INPUT);


    analogReference(DEFAULT);  
    Timer1.initialize(100000);   
    Timer1.attachInterrupt(functionInterrupt);  


     state = 0;
      y = 0;
   }

   void loop() 
   {
     y = analogRead(in);
     y = y / 4; 
     y = y - 128;


    if (state === 1)
    {
      state = 0;

      counter++;
      if (counter > 255)
      //counter = 0;
      counter = 200;

      //state = 0;
     }

      analogWrite(out,counter);

      lcd.home();
      lcd.setCursor(0, 1);
      lcd.print(counter);  
      lcd.setCursor(12,1);
      lcd.print(y);
     }

    void functionInterrupt()
    {
      state = 1;


     }

Can anybody explain me, why the PWM doesn't work in such a simple example?

Generally, Timer interrupt is being called once per 100 ms and then in the interrupt function the bool flag (state ) is being set. Then in the main loop the flag is checked and when it is 1 (true) several instructions are being executed. Next, PWM is generated and measured via ADC and also displayed on the LCD.

The problem is that the PWM is generated as 0 or sligthly over the 0, even when the value put to the analogWrite is 255 (max of 8 bit). Variable counter is incremented without problems and this value is being put to the analogWrite and despite it the PWM doesn't work.

I don't know, totally, what can be wrong.

Can anybody help me?

 #include<LiquidCrystal.h>
 #include<TimerOne.h>

  LiquidCrystal lcd(52, 50, 48, 46, 44, 42, 40);

  int out = 12; 
  int in = A7; 
  volatile bool state;  

  int counter = 0;
  volatile int y;


  void setup()
  {
    lcd.begin(16, 2);        
    lcd.clear();
    pinMode(out, OUTPUT);
    pinMode(in, INPUT);


    analogReference(DEFAULT);  
    Timer1.initialize(100000);   
    Timer1.attachInterrupt(functionInterrupt);  


     state = 0;
      y = 0;
   }

   void loop() 
   {
     y = analogRead(in);
     y = y / 4; 
     y = y - 128;


    if (state = 1)
    {
      state = 0;

      counter++;
      if (counter > 255)
      //counter = 0;
      counter = 200;

      //state = 0;
     }

      analogWrite(out,counter);

      lcd.home();
      lcd.setCursor(0, 1);
      lcd.print(counter);  
      lcd.setCursor(12,1);
      lcd.print(y);
     }

    void functionInterrupt()
    {
      state = 1;


     }

Can anybody explain me, why the PWM doesn't work in such a simple example?

Generally, Timer interrupt is being called once per 100 ms and then in the interrupt function the bool flag (state ) is being set. Then in the main loop the flag is checked and when it is 1 (true) several instructions are being executed. Next, PWM is generated and measured via ADC and also displayed on the LCD.

The problem is that the PWM is generated as 0 or sligthly over the 0, even when the value put to the analogWrite is 255 (max of 8 bit). Variable counter is incremented without problems and this value is being put to the analogWrite and despite it the PWM doesn't work.

I don't know, totally, what can be wrong.

Can anybody help me?

 #include<LiquidCrystal.h>
 #include<TimerOne.h>

  LiquidCrystal lcd(52, 50, 48, 46, 44, 42, 40);

  int out = 12; 
  int in = A7; 
  volatile bool state;  

  int counter = 0;
  volatile int y;


  void setup()
  {
    lcd.begin(16, 2);        
    lcd.clear();
    pinMode(out, OUTPUT);
    pinMode(in, INPUT);


    analogReference(DEFAULT);  
    Timer1.initialize(100000);   
    Timer1.attachInterrupt(functionInterrupt);  


     state = 0;
      y = 0;
   }

   void loop() 
   {
     y = analogRead(in);
     y = y / 4; 
     y = y - 128;


    if (state == 1)
    {
      state = 0;

      counter++;
      if (counter > 255)
      //counter = 0;
      counter = 200;

      //state = 0;
     }

      analogWrite(out,counter);

      lcd.home();
      lcd.setCursor(0, 1);
      lcd.print(counter);  
      lcd.setCursor(12,1);
      lcd.print(y);
     }

    void functionInterrupt()
    {
      state = 1;


     }
Source Link
M_K
  • 47
  • 1
  • 6

Arduino Mega, PWM analogWite doesn't work with Timer interrupt

Can anybody explain me, why the PWM doesn't work in such a simple example?

Generally, Timer interrupt is being called once per 100 ms and then in the interrupt function the bool flag (state ) is being set. Then in the main loop the flag is checked and when it is 1 (true) several instructions are being executed. Next, PWM is generated and measured via ADC and also displayed on the LCD.

The problem is that the PWM is generated as 0 or sligthly over the 0, even when the value put to the analogWrite is 255 (max of 8 bit). Variable counter is incremented without problems and this value is being put to the analogWrite and despite it the PWM doesn't work.

I don't know, totally, what can be wrong.

Can anybody help me?

 #include<LiquidCrystal.h>
 #include<TimerOne.h>

  LiquidCrystal lcd(52, 50, 48, 46, 44, 42, 40);

  int out = 12; 
  int in = A7; 
  volatile bool state;  

  int counter = 0;
  volatile int y;


  void setup()
  {
    lcd.begin(16, 2);        
    lcd.clear();
    pinMode(out, OUTPUT);
    pinMode(in, INPUT);


    analogReference(DEFAULT);  
    Timer1.initialize(100000);   
    Timer1.attachInterrupt(functionInterrupt);  


     state = 0;
      y = 0;
   }

   void loop() 
   {
     y = analogRead(in);
     y = y / 4; 
     y = y - 128;


    if (state = 1)
    {
      state = 0;

      counter++;
      if (counter > 255)
      //counter = 0;
      counter = 200;

      //state = 0;
     }

      analogWrite(out,counter);

      lcd.home();
      lcd.setCursor(0, 1);
      lcd.print(counter);  
      lcd.setCursor(12,1);
      lcd.print(y);
     }

    void functionInterrupt()
    {
      state = 1;


     }