Author Topic: how to add function to menubackend  (Read 1550 times)

0 Members and 1 Guest are viewing this topic.

Offline walakuluTopic starter

  • Newbie
  • Posts: 6
  • Country: lk
how to add function to menubackend
« on: December 15, 2018, 07:03:15 am »
my problem is how to add function like
1. led on-delay time,off- delay time repeatedly and when (i am using lcd keypad-analog pin (A0)) back to main menu just press ok (reading value of button 638) button. this for learning for me.can use while loop, how?
below send part of my code what is the wrong of that?i want to write code to get reading as a sensor (water levels) through analog pin A1 and display inside the "Display" menu. i tried many times . but in my codes can't back to the main menu.hence i want to learn .if can pls tell me to how to write above A1 reading codes.

and also i tried to display those sensor values as a default display.and when i press a ok button then go to the menu function.but its also difficult to me.can anyone help?

if (used.item.getName() == "Display"){
                                                   
                                                    int  Action=-1;delay(1000);     //not prees button   
                                                    while(Action!=4)                     
                                                     {
                                                       zm=-1;
                                                       Action=Read_1(0);//delay(300);   button press
                                                       if(zm!=Action)                   
                                                         {
                                                         while (Action==1) { 
                                                            lcd.clear(),lcd.setCursor(1,0);lcd.print("fgfg");
                                                            digitalWrite(10,HIGH);
                                                            delay(1000);
                                                            digitalWrite(10,LOW);
                                                            delay(1000);                                                           
                                                         }
                                                           
                                                                                                                   
                                                         if(Action==4)
                                                           {
                                                             lcd.clear();lcd.setCursor(0,0);lcd.print("setLow.Level OK");delay(2000);
                                                             lcd.setCursor(1,0);lcd.print("              ");
                                                             lcd.clear();lcd.setCursor(1,0);lcd.print(Line_1);         
                                                           }
                                                         }
                                                     } zm=Action; 
                   
                                                     
                                            }
« Last Edit: December 15, 2018, 07:05:19 am by walakulu »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: how to add function to menubackend
« Reply #1 on: December 15, 2018, 10:47:51 pm »
Once you are inside that while() loop, you are stuck forever unless some interrupt changes the value of Action and, if that is true, you need to define Action as volatile

Code: [Select]
  while (Action==1) { 
     lcd.clear(),lcd.setCursor(1,0);lcd.print("fgfg");
     digitalWrite(10,HIGH);
     delay(1000);
     digitalWrite(10,LOW);
     delay(1000);                                                           
  }

I'm kind of an evangelist on these conditional statements.  Too many people accidentally type "if (Action = 1) ..." (actually an assignment statement) when they mean "if (Action == 1) ...".  If you write the expression the other way around, the compiler will choke if you write it incorrectly.  Try this: "if (1 == Action) ..."  For most compilers, this is perfectly acceptable.  If you make the mistake of typing "if (1 = Action) ..." the compiler will flag it as an error.

FWIW, "if (Action = 1) ..." assigns the value 1 to the variable Action and since the result is not 0, the expression evaluates to TRUE.  For any non-zero value it always evaluates to true.
 
The following users thanked this post: Bassman59

Offline walakuluTopic starter

  • Newbie
  • Posts: 6
  • Country: lk
Re: how to add function to menubackend
« Reply #2 on: December 16, 2018, 12:47:10 am »
Thank you sir for reply,
my bad still not work, :phew:
i want run the function when select the "display".problem is its run only one time not continuously run(my project is get water level readings through A1 analog pin-6 levels- and display continuously till when i go to the menu back by pressing ok button)
in my code like you said stuck in while loop.cannot out by pressing ok button.

sir can you explain how to correct code by using above my sample code?

i use menubackend library,actually i want to main display indicate above mention water levels on first row and second row display time.and  press
ok button go to the backendmenu.but makes thats coding hard to me.i am a beginner.not familiar codes .learning arduino codes is it hard?

if you have a time please what code modification do?can you explain?
have a nice day..
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: how to add function to menubackend
« Reply #3 on: December 16, 2018, 07:41:31 am »
Instead of while (Action == 1) ...  why can't you use if (Action == 1).  You don't want to lock yourself in that while loop.

I can't help much with the overall program because I have no experience with the infrastructure.  For example, I have no idea what if (used.item.getName() == "Display") does.

I find it useful to write out the steps I want to take in some made up language.  Pseudo code.

while(1) {  // the main loop - everything happens in here
  if (any switch is pressed {
    if (switch1 is ON) {
      process whatever is to be done with switch 1
    }
    if (switch 3 is ON) {
      process whatever is to be done with switch 3
    }
  } // if (any switch is pressed) -- just a comment at the end of a LONG if statement
  now do any other process that doesn't depend on a button push
} // while(1) -- just a comment at the end of a LONG while statement

I usually scratch out something like this before I start to code just to make sure a) I understand the problem and b) I have some sense of program flow


 

Offline walakuluTopic starter

  • Newbie
  • Posts: 6
  • Country: lk
Re: how to add function to menubackend
« Reply #4 on: December 17, 2018, 01:45:09 am »
thank s,sorry for troubling you. here my code send you part by part.

#include "MenuBackend.h" // attach the library
#include <LiquidCrystal.h> // handle the LCD display
// --- Define their own characters for the LCD arrow: down, left, right, the top-down and back ---
uint8_t arrowUpDown[8] = {
  0b00100, //   *
  0b01110, //  ***
  0b11111, // * * *
  0b00000, //   *
  0b00000, //   *
  0b00000, //   *
  0b00000, //   *
  0b00000  //   *
};
uint8_t arrowDown[8]  = {
  0b00000, //   *
  0b00000, //   *
  0b00000, //   *
  0b00000, //   *
  0b00000, //   *
  0b11111, // * * *
  0b01110, //  ***
  0b00100  //   *
};
uint8_t arrowRight[8] = {
  B01000, //  *
  B00100, //   *
  B00010, //    *
  B00001, //     *
  B00010, //    *
  B00100, //   *
  B01000, //  *
  B00000  //
};
uint8_t arrowLeft[8] = {
  B00010, //  *
  B00100, //   *
  B01000, //    *
  B10000, //     *
  B01000, //    *
  B00100, //   *
  B00010, //  *
  B00000  //
};;
uint8_t arrowBack[8] = {0x1,0x1,0x5,0x9,0x1f,0x8,0x4};

LiquidCrystal lcd(8,9,4,5,6,7);    // definicja pinów dla LCD (sprawdź piny w swoim LCD)
volatile int zm =-1;               // to dla kontroli zmiany stanu klawiatury
volatile int x=-1;                 // zmienna pomocnicza
volatile int stan_Analog;          // wartość na wejściu Analogowym dla klawiaturki analogowej
char *Line_1;                      // pierwsza linia wyświetlanego tekstu na LCD
char *Line_2;                      // druga linia wyświetlanego tekstu na LCD

int ledState = HIGH;   
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;

int tankupperlevel=0;
int tanklowerlevel=0;
int tankgap=0;

int sumpupperlevel=0;
int sumplowerlevel=0;
int minsump=20;

int tankvolume=0;
int sumpvolume=0;
int sensorvaluetank =0;


int b = 0;
int d =0;
int currx = 1023;

int u = 0;
 

Offline walakuluTopic starter

  • Newbie
  • Posts: 6
  • Country: lk
Re: how to add function to menubackend
« Reply #5 on: December 17, 2018, 02:03:18 am »
void menuChangeEvent(MenuChangeEvent changed);
void menuUseEvent(MenuUseEvent used);

MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent); // konstruktor
   //                        ("                ")
   MenuItem P1 =  MenuItem("Display Set",1);
      MenuItem P11 = MenuItem("LCD On/Off",2);
      MenuItem P12 = MenuItem("Brightness",2);
      MenuItem P13 = MenuItem("Programme",2);
      MenuItem P14 = MenuItem("Exit",2);
     
   MenuItem P2 = MenuItem("WaterLevel Set",1);
   
      MenuItem P21 = MenuItem("Tank Level",2);
     
          MenuItem P211 = MenuItem("Tank Upp.Level",2);
                     
          MenuItem P212 = MenuItem("Tank Low.Level",2);
                             
                   
      MenuItem P22 = MenuItem("Sump Level",2);
     
          MenuItem P221 = MenuItem("Sump Upp.Level",2);
                                     
          MenuItem P222 = MenuItem("Sump Low.Level",2);
         
             
   MenuItem P3 = MenuItem("Time Settings",1);
     
   
   MenuItem P4 = MenuItem("Pump Controlle",1);
      MenuItem P41 = MenuItem("Auto Controlle",3);
          MenuItem P411 = MenuItem(" Auto On",4);
          MenuItem P412 = MenuItem(" Auto Off",4);
         
      MenuItem P42 = MenuItem("Manual Controlle",3);
         
          MenuItem P421 = MenuItem("  Off ",4);
          MenuItem P422 = MenuItem("  1 minute ",4);
          MenuItem P423 = MenuItem("  2 minutes",4);
          MenuItem P424 = MenuItem("  4 minutes",4);
          MenuItem P425 = MenuItem("  5 minutes",4);
          MenuItem P426 = MenuItem("  7 minutes",4);
          MenuItem P427 = MenuItem("  10 minutes",4);
          MenuItem P428 = MenuItem("User Controlle",4);
             
       MenuItem P43 = MenuItem("Time Set",3); 

     
               
      MenuItem P5 = MenuItem("Display",1);
         
/* --- Now position the menu (according to the setting specified above) ------------
add - adds vertical addRight - adds a level to the right, to the left adds addLeft
*/
void menuSetup() // function class MenuBackend
{
       menu.getRoot().add(P1); // Set the root menu, which is the first option
       P1.add(P11); // FILE parent has a child new so adds them vertically
             
             P11.add(P12); P11.addLeft(P1); // Below is a New Open so also in the vertical
                                            // A addLeft (P1) will allow us to return the key to the left to FILE
             P12.add (P13); P12.addLeft (P1); // Analogously we do with all podopcjami for FILE
             P13.add (P14); P13.addLeft (P1);
             P14.addLeft (P1); P14.add (P11); // Here we close the loop and back to the first suboption
                                            // Thanks to this we do not need to go back to the top of the cumbersome
                                            // Clicking the button Up
                                 
       P1.addRight (P2); // Right for the EDIT FILE

       
       P2.add (P21); // EDIT parent has a child Cut
       
             P21.add (P22); P21.addLeft (P2); // Below is Cut Copy

                P21.addRight (P211);
                     P211.add (P212); P211.addLeft (P21); // Below is MenuBackend EEPROM

                     P212.addLeft (P21); P212.add (P211); // We deal similarly
                                                       
            P22.addLeft (P2); P22.add (P21); // Similarly for all the suboptions
           
                P22.addRight (P221);
                     P221.add (P222); P221.addLeft (P22); // Below is MenuBackend EEPROM
                 
                     P222.addLeft (P22); P222.add (P221); // We deal similarly
                             
       P2.addRight (P3); // To the Time Settings
       
       P3.addRight (P4); // Time settings to the
       
       P4.add (P41); // EDIT parent has a child Cut
       
             P41.add (P42); P41.addLeft (P4); // Below is Cut Copy

                P41.addRight (P411);
                     P411.add (P412); P411.addLeft (P41); // Below is MenuBackend EEPROM
                     P412.addLeft (P41); P412.add (P411);
                         
                             
                                                   
             P42.addLeft (P4); P42.add (P41); // Similarly for all the suboptions
           
             P42.addRight (P421);
                     
                   P421.add (P422); P421.addLeft (P42);
                   P422.add (P423); P422.addLeft (P42);
                   P423.add (P424); P423.addLeft (P42);
                   P424.add (P425); P424.addLeft (P42);
                   P425.add (P426); P425.addLeft (P42);
                   P426.add (P427); P426.addLeft (P42);
                   P427.add (P428); P427.addLeft (P42);
                   P428.addLeft (P42); P428.add (P421);
                               
                     
             
       P4.addRight (P5); // To the Time Settings
         
       P5.addRight(P1); // Main loop closure, ie, the horizontal - the AID is FILE
}
 

Offline walakuluTopic starter

  • Newbie
  • Posts: 6
  • Country: lk
Re: how to add function to menubackend
« Reply #6 on: December 17, 2018, 02:05:25 am »
void menuUseEvent(MenuUseEvent used)      // function of the MenuBackend class - reAction by pressing OK
                                          // here we are giving the menu in favor of the OK action
                    {
                     
                       // LCD backlite on off
                       if (used.item.getName() == "LCD On/Off")   // Note - exactly the same string "Open" as in the menu !!!
                                                                  // because getName () gets the name
                                                {                                                   
                                                  if(brightness==0){ brightness=255;
                                                   lcd.clear();
                                                   lcd.setCursor(0,0);lcd.print("Backlite ON" );
                                                   analogWrite(10,brightness);}
                                                   else { brightness=0;
                                                   lcd.clear();
                                                   lcd.setCursor(0,0);lcd.print("Backlite OFF" );
                                                   delay(2000);
                                                   analogWrite(10,brightness);
                                                   }                                                                                                 
                                                }


                        // LCD backlite Brightness
                      if (used.item.getName() == "Brightness"){
                       
                                lcd.clear();
                                lcd.setCursor(0,0);lcd.print("Brightness" ); // tekst dla użytkownika
                                lcd.setCursor(5,1);lcd.print(brightness); // wyświetlamy akt. temperaturę
                                int  Action=-1;delay(1000);         
                                while(Action!=4)                     
                                 {
                                   zm=-1;
                                   Action=Read_1(0);//delay(300);   
                                   if(zm!=Action)                   
                                     {
                                     if (Action==1) { 
                                         brightness=brightness+10;
                                        if(brightness>256)brightness=255;
                                        lcd.clear(),lcd.setCursor(5,0);lcd.print("Brightness");
                                        lcd.setCursor(5,1);lcd.print(brightness);delay(300);
                                        analogWrite(10,brightness);
                                        brightness = brightness + fadeAmount;
                                     }
                                       
                                     if(Action==2)  { 
                                       brightness=brightness-10;
                                       if(brightness<0)brightness=0;
                                       lcd.clear(),lcd.setCursor(5,0);lcd.print("Brightness");
                                       lcd.setCursor(5,1);lcd.print(brightness);delay(300);
                                       analogWrite(10,brightness);
                                       brightness = brightness - fadeAmount;
                                     }
                                       
                                     if(Action==4)
                                       {
                                         lcd.clear();lcd.setCursor(0,0);lcd.print("Brightness OK");delay(2000); // pokazujemy OK przez 2 sek.
                                         lcd.setCursor(1,0);lcd.print("              "); // czyścimy linię
                                         lcd.clear();lcd.setCursor(1,0);lcd.print(Line_1);           // odtwarzamy poprzedni stan na LCD
                                       }
                                     }
                                 } zm=Action; 

                                 
                        }

                       
    // tank controlle
        //Tank Level
                        if (used.item.getName() == "Tank Upp.Level"){
                                           
                                                    lcd.clear();
                                                    lcd.setCursor(0,0);lcd.print("Tank Upp.Level" ); // tekst dla użytkownika
                                                    lcd.setCursor(5,1);lcd.print(tankupperlevel); // wyświetlamy akt. temperaturę
                                                    int  Action1=-1;delay(1000);         
                                                    while(Action1!=4)                     
                                                     {
                                                       zm=-1;
                                                       Action1=Read_1(0);//delay(300);   
                                                       if(zm!=Action1)                   
                                                         {
                                                         if (Action1==1) { 
                                                            tankupperlevel=tankupperlevel+20;
                                                            if(tankupperlevel>110)tankupperlevel=100;
                                                            lcd.clear(),lcd.setCursor(5,0);lcd.print("Tank Upp.Level");
                                                            lcd.setCursor(5,1);lcd.print(tankupperlevel);delay(300);
                                                            tankupperlevel = tankupperlevel + tankgap;
                                                         }
                                                           
                                                         if(Action1==2)  { 
                                                           tankupperlevel=tankupperlevel-20;
                                                           if(tankupperlevel<0)tankupperlevel=0;
                                                           lcd.clear(),lcd.setCursor(5,0);lcd.print("Tank Upp.Level");
                                                           lcd.setCursor(5,1);lcd.print(tankupperlevel);delay(300);
                                                           tankupperlevel = tankupperlevel - tankgap;
                                                         }
                                                           
                                                         if(Action1==4)
                                                           {
                                                             lcd.clear();lcd.setCursor(0,0);lcd.print("setUpp.Level OK");delay(2000); // pokazujemy OK przez 2 sek.
                                                             lcd.setCursor(1,0);lcd.print("              "); // czyścimy linię
                                                             lcd.clear();lcd.setCursor(1,0);lcd.print(Line_1);           // odtwarzamy poprzedni stan na LCD
                                                           }
                                                         }
                                                     } zm=Action1; 
                   
                                                     
                                            }



                                        if (used.item.getName() == "Tank Low.Level"){
                                           
                                                    lcd.clear();
                                                    lcd.setCursor(0,0);lcd.print("Tank Low.Level" ); // tekst dla użytkownika
                                                    lcd.setCursor(5,1);lcd.print(tanklowerlevel); // wyświetlamy akt. temperaturę
                                                    int  Action1=-1;delay(1000);         
                                                    while(Action1!=4)                     
                                                     {
                                                       zm=-1;
                                                       Action1=Read_1(0);//delay(300);   
                                                       if(zm!=Action1)                   
                                                         {
                                                         if (Action1==1) { 
                                                            tanklowerlevel=tanklowerlevel+20;
                                                            if(tanklowerlevel>110)tanklowerlevel=100;
                                                            lcd.clear(),lcd.setCursor(1,0);lcd.print("Tank Low.Level");
                                                            lcd.setCursor(5,1);lcd.print(tanklowerlevel);delay(300);
                                                            tanklowerlevel = tanklowerlevel + tankgap;
                                                         }
                                                           
                                                         if(Action1==2)  { 
                                                           tanklowerlevel=tanklowerlevel-20;
                                                           if(tanklowerlevel<0)tanklowerlevel=0;
                                                           lcd.clear(),lcd.setCursor(1,0);lcd.print("Tank Low.Level");
                                                           lcd.setCursor(5,1);lcd.print(tanklowerlevel);delay(300);
                                                           tanklowerlevel = tanklowerlevel - tankgap;
                                                         }
                                                           
                                                         if(Action1==4)
                                                           {
                                                             lcd.clear();lcd.setCursor(0,0);lcd.print("setLow.Level OK");delay(2000); // pokazujemy OK przez 2 sek.
                                                             lcd.setCursor(1,0);lcd.print("              "); // czyścimy linię
                                                             lcd.clear();lcd.setCursor(1,0);lcd.print(Line_1);           // odtwarzamy poprzedni stan na LCD
                                                           }
                                                         }
                                                     } zm=Action1; 
                   
                                                     
                                            }


 

Offline walakuluTopic starter

  • Newbie
  • Posts: 6
  • Country: lk
Re: how to add function to menubackend
« Reply #7 on: December 17, 2018, 02:06:43 am »
 if (used.item.getName() == "Sump Low.Level"){
                                           
                                                    lcd.clear();
                                                    lcd.setCursor(0,0);lcd.print("Sump Low.Level" ); // tekst dla użytkownika
                                                    lcd.setCursor(5,1);lcd.print(sumplowerlevel); // wyświetlamy akt. temperaturę
                                                    int  Action1=-1;delay(1000);         
                                                    while(Action1!=4)                     
                                                     {
                                                       zm=-1;
                                                       Action1=Read_1(0);//delay(300);   
                                                       if(zm!=Action1)                   
                                                         {
                                                         if (Action1==1) { 
                                                            sumplowerlevel=sumplowerlevel+20;
                                                            if(sumplowerlevel>110)sumplowerlevel=100;
                                                            lcd.clear(),lcd.setCursor(1,0);lcd.print("Sump Low.Level");
                                                            lcd.setCursor(5,1);lcd.print(sumplowerlevel);delay(300);
                                                            sumplowerlevel = sumplowerlevel + tankgap;
                                                         }
                                                           
                                                         if(Action1==2)  { 
                                                           sumplowerlevel=sumplowerlevel-20;
                                                           if(sumplowerlevel<0)sumplowerlevel=0;
                                                           lcd.clear(),lcd.setCursor(1,0);lcd.print("Sump Low.Level");
                                                           lcd.setCursor(5,1);lcd.print(sumplowerlevel);delay(300);
                                                           sumplowerlevel = sumplowerlevel - tankgap;
                                                         }
                                                           
                                                         if(Action1==4)
                                                           {
                                                             lcd.clear();lcd.setCursor(0,0);lcd.print("setLow.Level OK");delay(2000); // pokazujemy OK przez 2 sek.
                                                             lcd.setCursor(1,0);lcd.print("              "); // czyścimy linię
                                                             lcd.clear();lcd.setCursor(1,0);lcd.print(Line_1);           // odtwarzamy poprzedni stan na LCD
                                                           }
                                                         }
                                                     } zm=Action1; 
                   
                                                     
                                            }





//Display

                         if (used.item.getName() == "Display"){           //i want to display water "tankvolume" here till press back button.tank level should be update.

                                                           
                                  //update button pressed
                                  lcd.clear();
                                  lcd.setCursor(0,0);
                                  lcd.print("Tank Volume ");
                                  lcd.setCursor(12,0);
                                  lcd.print(tankvolume);
                             
                               
                            }

                         
                                   
 // Pump Controlle                                   
                         

                         if (used.item.getName() == "Auto"){
                           
                         }


 // time display
                         if (used.item.getName() == "Display"){
                                                 
                         }

     }
                         
// --- Response to pressing ----------------------------------------- ------------------------
void menuChangeEvent (MenuChangeEvent changed) // function class MenuBackend
{
   /* So really it is only here that shortkey useful and is used primarily to enrich the menu
      of arrow symbols, whichever is selected. Everything here is going on is displayed on the LCD.
   */
   int c = changed.to.getShortkey (); // Fetch shortkey (1,2,3, LUB4)
   lcd.clear (); // No comment
   lcd.setCursor (0,0);
// i want to display water "tankvolume" here on first lcd row
//second row should display "time"
// then press ok button go to below main menu

   
   if (c == 1) // if the menu Main contacts (shortkey = 1) are:
     {
     lcd.setCursor(14,0);lcd.write (3); // Left arrow
     strcpy (Line_1, changed.to.getName ()); // Create a string in the first line
     lcd.setCursor(0,0);lcd.print (Line_1); // Display it
     lcd.setCursor (15,0); lcd.write (4); // Right arrow
     lcd.setCursor (14,1); lcd.write (5); // Down arrow
     lcd.setCursor (15,1); lcd.write (5); // Down arrow
     }
     if (c == 2) // if the submenu for the child - (shortkey = 2) are:
     {
     lcd.setCursor (0,0); // Draw a star
     strcpy (Line_2, changed.to.getName ()); // Create a string in the first line
     lcd.print (Line_1); // Display it
     lcd.setCursor (0,1); // Second line and arrow return (arrowBack)
     lcd.print (changed.to.getName ()); // Display name of "child"
     lcd.setCursor (15,1); lcd.write (7); // Arrow up and down
     }
     if (c == 3) // if the child has a child - (shortkey = 3) are:
     {
     lcd.setCursor (0,0); // Star
     strcpy (Line_2, changed.to.getName ()); // Copy the files. the name of the menu options to the variable line 2
     lcd.print (Line_1); // And display the first line of
     lcd.setCursor (0,1);  // Second line and arrow arrowBack
     lcd.print (changed.to.getName ()); // Display the grandson of the second line
     lcd.setCursor (15,1); lcd.write (4); // Right arrow as they are the grandchildren
     }
   
     if (c == 4) // if this grandson (shortkey = 4) are:
     {
     lcd.setCursor (0,0);
     lcd.print (Line_2); // Display the first line of the child (or parent grandchild)
     lcd.setCursor (0,1); // Second line and arrow arrowBack
     lcd.print (changed.to.getName ()); // Display grandson
     lcd.setCursor (15,1); lcd.write (7); // Arrow up and down
     }
}
/* --- Hereinbelow feature that senses the state of the keyboard ---------------------------------------- ---
I prepared three different versions:
1) for analog keypad with shield LCDanalogKey company DFRobot
2) for the joystick (2 analog inputs and 1 digital pin)
    Note for this version option, you must add the function setup configuration for the pin, like this:
    pinMode (1, INPUT); digitalWrite (1, HIGH);
3) for the 5-five individual buttons (need 5 digital pins)
    Note for this version option, you must add the function setup configuration for the pins, like this:
    pinMode (1, INPUT); digitalWrite (1, HIGH);
    pinMode (2, INPUT); digitalWrite (2, HIGH);
    pinMode (3, INPUT); digitalWrite (3, HIGH);
    pinMode (11, INPUT); digitalWrite (11, HIGH);
    pinMode (12, INPUT); digitalWrite (12, HIGH);
*/
// --- Version for keyboard 5-button mouse DFRobot ------------------------------------- -
volatile int Read_1(int analog)
{
    int stan_Analog = analogRead (analog); delay (30) ;// Serial.println (stan_Analog);
    if (stan_Analog> 1000) return -1; // For values ​​outside the range
    if (stan_Analog <50) return 0; // Right
    if (stan_Analog <150) return 1; // To the top
    if (stan_Analog <300) return 2; // Down
    if (stan_Analog <500) return 3; // Left
    if (stan_Analog <700) return 4; // OK
    return -1; // Do not pressed
}
void setup ()
{
   Line_1 = new char [16]; // Initialize a pointer to a dynamic text
   Line_2 = new char [16]; // Is VERY IMPORTANT because dynamic indicator must indicate
                         // Pre-defined location in memory. If we did not do
                         // This sooner or later applet could jump in indefinite
                         // Close the memory area, which can result in irreversible consequences
                         // Including switching Fuse Bits!
                         // Please be careful at all dynamic indicators, SUCH GOOD ADVICE :-)
   Serial.begin (9600); // Initialize serial, mainly for test
   lcd.begin (16, 2); // Initialize the LCD
   lcd.createChar (3, arrowLeft); // Create a memory of your LCD 5 characters for arrows
   lcd.createChar (4, arrowRight);
   lcd.createChar (5, arrowDown);
   lcd.createChar (6, arrowBack);
   lcd.createChar (7, arrowUpDown);

   pinMode (10,OUTPUT);
   pinMode (11,OUTPUT);
   pinMode (12,OUTPUT);
   pinMode (13,OUTPUT);
   digitalWrite (10, HIGH);   
   /* Here exemplary digital pins for the 3 versions feature Read_3 (1,2,3,11,12)
    pinMode (1, INPUT); digitalWrite (1, HIGH);
    pinMode (2, INPUT); digitalWrite (2, HIGH);
    pinMode (3, INPUT); digitalWrite (3, HIGH);
    pinMode (11, INPUT); digitalWrite (11, HIGH);
    pinMode (12, INPUT); digitalWrite (12, HIGH);
   */
   
   menuSetup (); // Function MenuBackend class - there really are creating our menu
   menu.moveDown (); // Go to the first option - FILE, moveDown because originally we were at the root
                         // It's like in the Avatar tree roots grow up :-)
}
// --- And it's time for neverending story :-) ------------------------------------ --------
void loop ()
{
 
   x=Read_1(0);delay(30);   // Read the state of the keyboard:
   /*
   I use Read_1 function () because I just Keyboard plugged into A0
   If you have a different keypads that use the Read_2 or Read_3 - see description
   In addition, you must remember that in the functions obsługo OK button - menuUseEvent (MenuUseEvent used)
   Also you need to use the appropriate version of the function read!
   */
   if(zm!=x)   // if it was the change in the state are:
     {
       switch(x) // check to see what was pressed
       {
       case 0: menu.moveRight(); break; // If pressed, move it in the right menu to the right
       case 1: menu.moveUp(); break; // Menu to top
       case 2: menu.moveDown(); break; // Menu Down
       case 3: menu.moveLeft(); break; // Menu to the left
       case 4: menu.use(); break; // Pressed OK, so jump to the function menuUseEvent (MenuUseEvend used)
                                           // This function is just serve our menu, check here
                                           // Which option is selected, and here we create code to handle the event.
       }
     } zm = x; // Assign the value of x variable amended so that the long pressing the
                                           // Same key did not result in the re-generation event.
       
   // getting tank level use analog A1 pin   
       b = analogRead(A1);
       if ((b != 1023) && (b != currx)){
 
                                //update screen and change currx
                               
                                currx = b;
                               
                                if (currx > 600 && currx < 800){
                               
                                   tankvolume="0 %";
                               
                                } else if (currx > 300 && currx < 510){
                               
                                  tankvolume="20";
                                 
                                } else if (currx < 10){
                               
                                  tankvolume="40";
                                 
                                } else if (currx > 50 && currx < 150){
                               
                                  tankvolume="60";
                                 
                                } else if (currx > 200 && currx < 300){
                               
                                  tankvolume="80";
                                 
                                }
                      delay(10);
                  }

//// getting sump level use analog A2 pin 
                 d = analogRead(A2);
                                if ((d != 1023) && (d != currx)){
 
                                //update screen and change currx
                               
                                currx = d;
                               
                                if (currx > 600 && currx < 800){
                               
                                   sumpvolume="0 %";
                               
                                } else if (currx > 300 && currx < 510){
                               
                                  sumpvolume="20";
                                 
                                } else if (currx < 10){
                               
                                  sumpvolume="40";
                                 
                                } else if (currx > 50 && currx < 150){
                               
                                  sumpvolume="60";
                                 
                                } else if (currx > 200 && currx < 300){
                               
                                  sumpvolume="80";
                                 
                                }
                      delay(10);
                  }


               autorun();





                 
}



void autorun(){

  if ((tanklowerlevel>=tankvolume) && (sumplowerlevel<=sumpvolume)&&(u==1)){
   
    digitalWrite (11,HIGH); // motor on
    digitalWrite (12,HIGH); // Buzzer on
    digitalWrite (13,HIGH); // LED on
  }

if (((tankupperlevel<=tankvolume) || (sumplowerlevel>=sumpvolume))&&(u==1)){
   
    digitalWrite (11,LOW); // motor off
    digitalWrite (12,LOW); // Buzzer off
    digitalWrite (13,LOW); // LED off
  }


 
}

// === END
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: how to add function to menubackend
« Reply #8 on: December 17, 2018, 04:00:06 pm »
That's a LOT of code!

You might think about using serial.print() or serial.println() to display debugging messages

Clearly you want a message when you enter and just before you leave various functions.
You might also want to print a message when certain program branches are taken (or not taken).
You can output the value of variables.

Serial.print() is ugly but it's what we have...

https://www.arduino.cc/en/Serial/Print

Here is one way to do conditional debugging output (NOT tested)

Code: [Select]

#define DEBUG   // turn on debug messages
                // remove or comment out to stop messages
//#undef DEBUG  // another way to turn off messages

#ifdef DEBUG
  #define DEBUG_PRINT(val,fmt)    Serial.print(val, fmt)
  #define DEBUG_PRINTLN(val,fmt)  Serial.println(val, fmt)
  #define DEBUG_PRINTSTR(str)     Serial.print(str)
  #define DEBUG_PRINTLNSTR(str)   Serial.println(str)
#else
  #define DEBUG_PRINT(val,fmt)    /* Don't do anything in release builds */
  #define DEBUG_PRINTLN(val,fmt)
  #define DEBUG_PRINTSTR(str)
  #define DEBUG_PRINTLNSTR(str)
#endif

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  DEBUG_PRINTLNSTR("Hello World");
  DEBUG_PRINT(13,DEC);
}

Note that I didn't try to get clever with whether Serial.print requires a numeric format - it always does.
« Last Edit: December 17, 2018, 04:29:06 pm by rstofer »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf