Go Down

Topic: Electronic control of fire oven. (Read 874 times) previous topic - next topic

Bobo

Hello all, who can help me in this topic. I'm new here, sorry for some uncoordinated or chaotic way.

I need to create a control for fire oven, for airpump, which distribute an air to grate ( or furnace). In this case I need to use a fan to distribute a heat air to rooms.
At this moment I have a hardware for it like A-Duemilanove with LCD shield 2 rows.
In this case is used at this moment 3 pins for analog inlet, 0-from LCD keys, 1-from grate sensor PT100 and 2-from sensor heated air.

As outputs at this moment is used outs for LCD and output 0 I mean for steps to air pump (f.e. L297,298 new board). Because some pins are used for LCD, some free dig.pins I need to use do electronical clamps (servo) and controling a heat fan to ON only if an air is heated.

Here is a program which I created, but if I connect the LED to pin 0, it blinks  dependly on air pump rotation, but I don't know, if this steps are sufficient for example for L297..... to create f.e. 2000 rot.
Because I'm only basic programer of Arduino, may be, that the program have some mistakes, or loop excludes. F.e. If I entering a menu (buttons 1 or 2 on LCD), airpump stop(stop blink a LED on 0 DIG) and I don't know why.

Please help me to modifying the program to run correct. Many thanks.

Code: [Select]

#include <LCD4Bit_mod.h>
#include <EEPROM.h>


LCD4Bit_mod lcd = LCD4Bit_mod(2);

char menu[5][17] = {"Min.Temp.fire ",
                   "Max.Temp.fire ",
                   "Req.Temp.fanON",
                   "Req.Temp.fanOFF",
                   "Door switch" };
int  adc_key_val[5] ={30, 150, 360, 535, 760 };
int adc_b;
int NUM_KEYS = 5;
int adc_key_in;
int dig_otac;
int key=-1;
int oldkey=-1;
int heat;
int heat_min;
int rotate;
int heat_max;
int heat_delay;
int heat_mem;
int heat_coef;
int size_rot;
int lp;
boolean active=true;
boolean act_read=false;
boolean act_wr=false;
boolean temp_act=true;
char temp[5];
char menu_num[2];
char temp_read[5];
char rot_read[4];

void setup() {
 pinMode(13, OUTPUT);
pinMode(0, OUTPUT);

 lcd.init();
  lcd.clear();
 lcd.cursorTo(1, 0);
 lcd.printIn("T.fire="); //temp fire
 lcd.cursorTo(2, 0);
 lcd.printIn("Rot.airp=");// rotation airpump
 temp_act=true;
 
}

void loop() {
 
 

     adc_key_in = analogRead(0);    // read the value from the sensor  
 digitalWrite(13, HIGH);  
 key = get_key(adc_key_in);                    // convert into key press
     
     if (key != oldkey)                            // if keypress is detected
     {
  temp_act=false;
   delay(10);            // wait for debounce time
           adc_key_in = analogRead(0);    // read the value from the sensor  
   key = get_key(adc_key_in);                    // convert into key press
   if (key != oldkey)                        
   {                  
     oldkey = key;
     
     if (active==true){
     if (key == 1){
      lcd.clear();  
 lcd.printIn(":Main menu :");
 lcd.cursorTo(1, 14);  //line=1, x=14
 sprintf(menu_num, "%u", lp);
 lcd.printIn(menu_num);
     lcd.cursorTo(2, 0);  //line=2, x=0
 delay(10);
 lcd.printIn(menu[lp]);
 if (lp>4){
 lp=0;
 }else{
 lp=lp+1;
 }
   }
     
     if (key == 2){
      lcd.clear();
 lcd.printIn(":Main menu :");
 lcd.cursorTo(1, 14);  //line=1, x=14
 sprintf(menu_num, "%u", lp);
 lcd.printIn(menu_num);
     lcd.cursorTo(2, 0);  //line=2, x=0
 delay(10);
  lcd.printIn(menu[lp]);
 if (lp<0){
 lp=4;
 }else{
 lp=lp-1;
 }
     }
     }
if (key == 0){
  act_read=true;
      lcd.clear();
 lcd.printIn(menu[lp-1]);
 if (lp-1<=3){
heat = round(EEPROM.read(lp-1)*4);
 }
 if (lp-1==4){
 heat = EEPROM.read(lp-1);  
 }
sprintf(temp, "%u", heat); //Convert long powerusage to string
act_wr=true;
if (lp-1<=3){
 lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn(temp);
 lcd.cursorTo(2,4);
 lcd.printIn("'C");
}
if (lp-1==4){
if (heat==1){
   lcd.clear();
 lcd.printIn(menu[lp-1]);
lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn("ON");  
}
if (heat==0){
   lcd.clear();
 lcd.printIn(menu[lp-1]);
lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn("OFF");  
}
}
 active=false;
 }
if (act_wr == true){
if (key == 1){
if ((heat<999)&(lp-1<=3)){  
heat=heat+5;
delay(10);
sprintf(temp, "%u", heat); //Convert long powerusage to string
lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn(temp);
 lcd.cursorTo(2,4);
 lcd.printIn("'C");
}
if (lp-1==4){
  lcd.clear();
 lcd.printIn(menu[lp-1]);
 lcd.cursorTo(2,0);
 lcd.printIn("ON");  
 heat=1;
}
}  

if (key == 2){
if ((heat>30)&(lp-1<=3)){  
heat=heat-5;
delay(10);
sprintf(temp, "%u", heat); //Convert long powerusage to string
lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn(temp);
 lcd.cursorTo(2,4);
 lcd.printIn("'C");
}
if (lp-1==4){
  lcd.clear();
 lcd.printIn(menu[lp-1]);
lcd.cursorTo(2,0);
 lcd.printIn("OFF");  
 heat=0;  
}
}

if (key == 4){
 if (lp-1<=3){
EEPROM.write(lp-1,round(heat/4));
 }
 if (lp-1==4){
   EEPROM.write(lp-1,heat);
 }
lcd.clear();
lcd.printIn("Set.saved ...");
delay(2000);
lcd.clear();
 lcd.printIn(":Main menu :");
 lcd.cursorTo(1, 14);  //line=1, x=13
 sprintf(menu_num, "%u", lp-1);
 lcd.printIn(menu_num);
     lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn(menu[lp-1]);    
 active=true;
 act_wr=false;

}
}
 
if ((key == 3)&(act_read == true)){  
  lcd.clear();
lcd.printIn(":Main menu :");
 lcd.cursorTo(1, 14);  //line=1, x=13
 sprintf(menu_num, "%u", lp-1);
 lcd.printIn(menu_num);
     lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn(menu[lp-1]);    
 active=true;
 act_read=false;
 act_wr=false;
   }
   }
 }
 

 //delay(1000);
 digitalWrite(13, LOW);
main_disp();    
}

void main_disp(){

 if (temp_act ==true){
 adc_b = analogRead(1);
 adc_b = round(adc_b*0.9);
 sprintf(temp_read, "%u", adc_b); //Convert to string
lcd.cursorTo(1, 11);  //line=1, x=11
 lcd.printIn(temp_read);
 lcd.cursorTo(1,14);
 lcd.printIn("'C");
delay(1);
heat_min = round(EEPROM.read(0)*4);
heat_max = round(EEPROM.read(1)*4);

if ((adc_b>heat_min)&&(adc_b<heat_max)){
heat_coef=heat_max-adc_b;  
heat_delay=round(adc_b*0.01);  
digitalWrite(0,HIGH);
delayMicroseconds(heat_delay);
digitalWrite(0,LOW);
delayMicroseconds(1);
rotate=round(3*heat_coef);
sprintf(rot_read, "%u", rotate);
lcd.cursorTo(2, 12);  //line=2, x=14
 lcd.printIn(rot_read);
 size_rot=sizeof(rot_read);
 
 if (rotate<=999){
lcd.cursorTo(2, 15);  //line=2, x=15
 lcd.printIn(" ");    
 }
  if (rotate<=99){
lcd.cursorTo(2, 14);  //line=2, x=14
 lcd.printIn(" ");    
 }
}else{
lcd.cursorTo(2, 12);  //line=2, x=12
 lcd.printIn("0   ");  
}
 }
 
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
     int k;
   
     for (k = 0; k < NUM_KEYS; k++)
     {
           if (input < adc_key_val[k])
           {
         
   return k;
       }
     }
   
   if (k >= NUM_KEYS)
       k = -1;     // No valid key pressed
   
   return k;

PaulS

I think you should start by reading this:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1266076497/15

The loop function is called in an endless loop. Your code is very difficult to read because of the random indents and nested switch statements and it's length.

Learn to create and call functions. With a function like menu() to handle all of the menu stuff (responding to a key press, etc.), the loop function will become a lot shorter.

Then, you can see whether the problem is in loop or in menu or in some function called by menu.

Bobo

Ok PaulS, I start with this, but please, give me example (f.e. from my code) please, to short the void loop, because I'm programming in different languages like a PHP, and some Delphi applications and it is little bit different, than this.

Please. Many thanks.

PaulS

Everything from this code:

Code: [Select]
     adc_key_in = analogRead(0);    // read the value from the sensor  
 digitalWrite(13, HIGH);  
 key = get_key(adc_key_in);                    // convert into key press

     if (key != oldkey)                            // if keypress is detected
     {


to the matching curly brace should be in a function. Call it menu, for instance, and call that function in loop (instead of having all that code in loop).

I can't tell you how much shorter loop will be, since the random indenting of your code makes it so difficult to locate the closing curly brace that matches the opening brace shown above.

Bobo

PaulS, many thanks.

I create a new split of program, but I cannot see a difference after upload do board.
Here is new code :

Code: [Select]

#include <LCD4Bit_mod.h>
#include <EEPROM.h>


LCD4Bit_mod lcd = LCD4Bit_mod(2);

char menu[5][17] = {"Min.Temp.fire ",
                   "Max.Temp.fire ",
                   "Req.Temp.fanON",
                   "Req.Temp.fanOFF",
                   "Door switch" };
int  adc_key_val[5] ={30, 150, 360, 535, 760 };
int adc_b;
int NUM_KEYS = 5;
int adc_key_in;
int dig_otac;
int key=-1;
int oldkey=-1;
int heat;
int heat_min;
int rotate;
int heat_max;
int heat_delay;
int heat_mem;
int heat_coef;
int size_rot;
int lp;
boolean active=true;
boolean act_read=false;
boolean act_wr=false;
boolean temp_act=true;
char temp[5];
char menu_num[2];
char temp_read[5];
char rot_read[4];

void setup() {
 pinMode(13, OUTPUT);
pinMode(0, OUTPUT);

 lcd.init();
  lcd.clear();
 lcd.cursorTo(1, 0);
 lcd.printIn("T.fire="); //temp fire
 lcd.cursorTo(2, 0);
 lcd.printIn("Rot.airp=");// rotation airpump
 temp_act=true;
 
}

void loop() {


     adc_key_in = analogRead(0);    // read the value from the sensor  
 digitalWrite(13, HIGH);  
 key = get_key(adc_key_in);                    // convert into key press
     
     if (key != oldkey)                            // if keypress is detected
     {
             // wait for debounce time            
     oldkey = key;
     
    temp_act=false;
   delay(10);
  menu_start();  
  menu_enter();
  menu_ch_wr();
  menu_exit();
 
 }//end of key !=oldkey

 digitalWrite(13, LOW);
main_disp();    
}// end of main loop


void menu_exit(){
 if ((key == 3)&(act_read == true)){  
  lcd.clear();
lcd.printIn(":Main menu :");
 lcd.cursorTo(1, 14);  //line=1, x=13
 sprintf(menu_num, "%u", lp-1);
 lcd.printIn(menu_num);
     lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn(menu[lp-1]);    
 active=true;
 act_read=false;
 act_wr=false;
   }
}//end menu_exit


void menu_enter(){
 if (key == 0){
  act_read=true;
      lcd.clear();
 lcd.printIn(menu[lp-1]);
 if (lp-1<=3){
heat = round(EEPROM.read(lp-1)*4);
 }
 if (lp-1==4){
 heat = EEPROM.read(lp-1);  
 }
sprintf(temp, "%u", heat); //Convert long powerusage to string
act_wr=true;
if (lp-1<=3){
 lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn(temp);
 lcd.cursorTo(2,4);
 lcd.printIn("'C");
}
if (lp-1==4){
if (heat==1){
   lcd.clear();
 lcd.printIn(menu[lp-1]);
lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn("ON");  
}
if (heat==0){
   lcd.clear();
 lcd.printIn(menu[lp-1]);
lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn("OFF");  
}
}
 active=false;
 }
}// end of menu_enter

void menu_ch_wr(){
if (act_wr == true){
if (key == 1){
if ((heat<999)&(lp-1<=3)){  
heat=heat+5;
delay(10);
sprintf(temp, "%u", heat); //Convert long powerusage to string
lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn(temp);
 lcd.cursorTo(2,4);
 lcd.printIn("'C");
}
if (lp-1==4){
  lcd.clear();
 lcd.printIn(menu[lp-1]);
 lcd.cursorTo(2,0);
 lcd.printIn("ON");  
 heat=1;
}
}  

if (key == 2){
if ((heat>30)&(lp-1<=3)){  
heat=heat-5;
delay(10);
sprintf(temp, "%u", heat); //Convert long powerusage to string
lcd.cursorTo(2, 0);  //line=2, x=0
 lcd.printIn(temp);
 lcd.cursorTo(2,4);
 lcd.printIn("'C");
}
if (lp-1==4){
  lcd.clear();
 lcd.printIn(menu[lp-1]);
lcd.cursorTo(2,0);
 lcd.printIn("OFF");  
 heat=0;  
}
}

   if (key == 4)  {
   if (lp-1<=3){
   EEPROM.write(lp-1,round(heat/4));
               }
   if (lp-1==4){
   EEPROM.write(lp-1,heat);
               }
   lcd.clear();
   lcd.printIn("Set.saved ...");
   delay(2000);
   lcd.clear();
   lcd.printIn(":Main menu :");
   lcd.cursorTo(1, 14);  //line=1, x=13
   sprintf(menu_num, "%u", lp-1);
   lcd.printIn(menu_num);
   lcd.cursorTo(2, 0);  //line=2, x=0
   lcd.printIn(menu[lp-1]);    
   active=true;
   act_wr=false;

                   }
}// end of act_wr
}// end of void menu_ch_wr


void menu_start(){
 if (active==true){
     if (key == 1){
      lcd.clear();  
 lcd.printIn(":Main menu :");
 lcd.cursorTo(1, 14);  //line=1, x=14
 sprintf(menu_num, "%u", lp);
 lcd.printIn(menu_num);
     lcd.cursorTo(2, 0);  //line=2, x=0
 delay(10);
 lcd.printIn(menu[lp]);
 if (lp>4){
 lp=0;
 }else{
 lp=lp+1;
 }
   }  
     if (key == 2){
      lcd.clear();
 lcd.printIn(":Main menu :");
 lcd.cursorTo(1, 14);  //line=1, x=14
 sprintf(menu_num, "%u", lp);
 lcd.printIn(menu_num);
     lcd.cursorTo(2, 0);  //line=2, x=0
 delay(10);
  lcd.printIn(menu[lp]);
 if (lp<0){
 lp=4;
 }else{
 lp=lp-1;
 }
     }
     }
}// end of menu_start

void main_disp(){

 if (temp_act ==true){
 adc_b = analogRead(1);
 adc_b = round(adc_b*0.9);
 sprintf(temp_read, "%u", adc_b); //Convert to string
lcd.cursorTo(1, 11);  //line=1, x=11
 lcd.printIn(temp_read);
 lcd.cursorTo(1,14);
 lcd.printIn("'C");
delay(1);
heat_min = round(EEPROM.read(0)*4);
heat_max = round(EEPROM.read(1)*4);

if ((adc_b>heat_min)&&(adc_b<heat_max)){
heat_coef=heat_max-adc_b;  
heat_delay=round(adc_b*0.01);  
digitalWrite(0,HIGH);
delayMicroseconds(heat_delay);
digitalWrite(0,LOW);
delayMicroseconds(1);
rotate=round(3*heat_coef);
sprintf(rot_read, "%u", rotate);
lcd.cursorTo(2, 12);  //line=2, x=14
 lcd.printIn(rot_read);
 size_rot=sizeof(rot_read);
 
 if (rotate<=999){
lcd.cursorTo(2, 15);  //line=2, x=15
 lcd.printIn(" ");    
 }
  if (rotate<=99){
lcd.cursorTo(2, 14);  //line=2, x=14
 lcd.printIn(" ");    
 }
}else{
lcd.cursorTo(2, 12);  //line=2, x=12
 lcd.printIn("0   ");  
}
 }
 
}// end of main display refreshing


int get_key(unsigned int input)
{
     int k;
   
     for (k = 0; k < NUM_KEYS; k++)
     {
           if (input < adc_key_val[k])
           {
         
   return k;
       }
     }
   
   if (k >= NUM_KEYS)
       k = -1;     // No valid key pressed
   
   return k;
}


Please, help me, because I'm helpless. I'm passed through the all code, but I cannot find any problem. If you press 1 or 2 button, the menu will block output 0 for blinking.

Thank you for your time.

PaulS

Add a Serial.begin(9600); statement to setup(). Move the LED (airpump) from pin 0 to another digital pin (not 1, though).

To each menu function, add Serial.println statements that show when the function is entered, and when it is exited:

Code: [Select]
void menu_function()
{
  Serial.println("menu_function ==>");
  // Do the useful stuff
  Serial.println("menu_function <==");
}


Properly indent your code:
Code: [Select]
if(something == somethingElse)
{
}

switch(something)
{
  case n:
     // Whatever
     break;
  case x:
     // Whatever
     break;
}


This will make reading the code, and trouble shooting much easier.

Open the serial monitor window, and see what happens when you press the buttons. Post the revised code, and the output, along with a description of the problem. Something like:

I uploaded the code, and pressed button 1, button 3, and button 2. This appeared in the serial monitor window:
Code: [Select]

Serial monitor output

Describe what you expected to see, and how that differs from what you see.

Go Up