Jump to content
ELFORUM - Forumul electronistilor
ain

Problema impunere coeficient de umplere variabil

Recommended Posts

Buna,

Incerc sa transform o masina de facut paine intr-un termostat cu temperatura reglabila.

Voi folosi un UNO care comanda rezistenta de incalzire printr-un optotriac S21ME6 (cu detectie trecere prin zero) si un triac de putere.

 

Deocamdata vreau sa testez doar partea de comanda a puterii. Comanda vreau sa o fac PWM cu o perioada de 2s, coeficientul de umplere reglabil undeva intre 0 si o valoare maxima de 50%. Am scris un softulet care permite alegerea valorii factorului de umplere prin apasarea a 2 butoane. Partea de modificare a coef. de umplere functioneaza corect (PWMVal este calculata corect), insa am probleme cu comanda spre portul digital de iesire (portul digital 9 nu are coeficientul de umplere variabil dorit, ci ramane la valoarea initializata in setup.. 

 

N-am mai programat ceva serios in C de 20 de ani; gresesc ceva, si nu stiu unde. Stiu sa fac cu ajutorul functiei delay, insa doresc ceva cu intreruperi care sa functioneze independent de programul de termostatare care urmeaza sa fie implementat. Orice ajutor va fi apreciat. Eventual o alta solutie.

Multumesc anticipat.

#include <Wire.h> 
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define BACKLIGHT_PIN     3

const int ONOFFbuttonPin = 8;
const int INCbuttonPin = 6;
const int DECbuttonPin = 7;
const int CommandLedPin =  9;  
const int PWMMax = 50;  

int ONOFFbuttonState = 0;
int INCbuttonState = 0;       
int DECbuttonState = 0;         
int PWMVal = 0;

LiquidCrystal_I2C  lcd(0x3F,2,1,0,4,5,6,7); // set the LCD address to 0x3F for a 16 chars and 2 line display

void setup()
{
  PWMVal = 0; 
  
  lcd.begin(16,2);   
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); // go home

  pinMode(CommandLedPin, OUTPUT);                               // Set digital pin 9 (D9) to an output
  pinMode(ONOFFbuttonPin, INPUT);
  pinMode(INCbuttonPin, INPUT);
  pinMode(DECbuttonPin, INPUT);

 // pinMode(9, OUTPUT);
  TCCR1A = _BV(COM1A1) | _BV(WGM11);                            // Enable the PWM output OC1A on digital pins 9
  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12) | _BV(CS10);     // Set fast PWM and prescaler of 1024 on timer 1
 
  ICR1 = 31249;                                                 // Set the PWM frequency to 0.5Hz: 16MHz/(1024 * 0.5Hz) - 1 = 31249
  OCR1A = PWMVal*312.49;                                        // Set the duty-cycle to 10%: 124999 / 10 = 12499

}


void loop() {

    lcd.setCursor(0,1);
    lcd.print("PWM");
    lcd.setCursor(4,1);
    lcd.print(PWMVal);
    lcd.print("% ");    
   
  ONOFFbuttonState = digitalRead(ONOFFbuttonPin);
  INCbuttonState = digitalRead(INCbuttonPin);
  DECbuttonState = digitalRead(DECbuttonPin);
  
  if (INCbuttonState == LOW && PWMVal<PWMMax) {
    digitalWrite(CommandLedPin, HIGH);
    lcd.setCursor(13,0);
    lcd.print("Inc");
    PWMVal += 1;
    lcd.setCursor(10,1);
    lcd.print(OCR1A);
    lcd.print("     ");
  } else {
       if (DECbuttonState == LOW && PWMVal>0) {
            digitalWrite(CommandLedPin, HIGH);
            lcd.setCursor(13,0);
            lcd.print("Dec");
            PWMVal -= 1;
            lcd.setCursor(10,1);
            lcd.print(OCR1A);
            lcd.print("     ");
         } else {
            digitalWrite(CommandLedPin, LOW);
            lcd.setCursor(13,0);
            lcd.print("   ");
         }
  }
  
  delay(300);
}

 

Edited by ain
Link to post
Share on other sites

PWMValue ar trebui să fie o variabilă tip byte nu int. PWM-ul din Arduino lucrează pe 8 biţi, un int acolo care poate  trece şi în domeniul negativ dacă nu ai grijă e cam nepotrivit, ar mai fi nevoie de o linie PWMValue = constrain(PWMValue, 0, 50), amplasată după ce incrementezi variabila.

Link to post
Share on other sites

Multumesc mult pentru interventie.

M-am uitat cu atentie peste program si am realizat ca foloseam comenzi ce nu aveau ce cauta acolo (digitalwrite).

Acum partea asta de program functioneaza bine. Il voi folosi sa masor/calibrez senzorul de temperatura (probabil NTC) si in functie de functia de masurare a temperaturi voi gandi partea electronica pentru masurarea acesteia.

Inca o data, multumesc.

#include <Wire.h> 
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define BACKLIGHT_PIN     3

const byte ONOFFbuttonPin = 8;
const byte INCbuttonPin = 6;
const byte DECbuttonPin = 7;
const byte CommandLedPin = 9;  
const byte PWMMax = 50;  

int ONOFFbuttonState = 0;
int INCbuttonState = 0;       
int DECbuttonState = 0;         
byte PWMVal = 0;

LiquidCrystal_I2C  lcd(0x3F,2,1,0,4,5,6,7); // set the LCD address to 0x3F for a 16 chars and 2 line display

void setup()
{
  lcd.begin(16,2);   
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); // go home

  pinMode(CommandLedPin, OUTPUT);
  pinMode(ONOFFbuttonPin, INPUT);
  pinMode(INCbuttonPin, INPUT);
  pinMode(DECbuttonPin, INPUT);

  TCCR1A = _BV(COM1A1) | _BV(WGM11);                            // Enable the PWM output OC1A on digital pins 9
  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12) | _BV(CS10);     // Set fast PWM and prescaler of 1024 on timer 1
  ICR1 = 31249;                                                 // Set the PWM frequency to 0.5Hz: 16MHz/(1024 * 0.5Hz) - 1 = 31249
  OCR1A = PWMVal*312.49;                                        // Set the duty-cycle to X%: ICR1 * X / 100 
}

void loop() {
    lcd.setCursor(0,1);
    lcd.print("PWM");
    lcd.setCursor(4,1);
    lcd.print(PWMVal);
    lcd.print("% ");    
   
  ONOFFbuttonState = digitalRead(ONOFFbuttonPin);
  INCbuttonState = digitalRead(INCbuttonPin);
  DECbuttonState = digitalRead(DECbuttonPin);
  
  if (INCbuttonState == LOW && PWMVal<PWMMax) {
    lcd.setCursor(13,0);
    lcd.print("Inc");
    PWMVal += 1;
    OCR1A = PWMVal*312.49;  
  } else {
       if (DECbuttonState == LOW && PWMVal>0) {
            lcd.setCursor(13,0);
            lcd.print("Dec");
            PWMVal -= 1;
            OCR1A = PWMVal*312.49;  
         } else {
           lcd.setCursor(13,0);
            lcd.print("   ");
         }
  }
  
  delay(150);
}

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines