Arduino Polska Forum

Pełna wersja: Proste MENU (Rotary.h, OnButton.h)
Aktualnie przeglądasz uproszczoną wersję forum. Kliknij tutaj, by zobaczyć wersję z pełnym formatowaniem.
Witam 


Wszystko było by ok ale chwiałbym pogłębić menu na tym prostym przykładzie ale coś jest nie tak ...

Kod:
#include <Rotary.h>
#include "OneButton.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,10,11,12,13);

Rotary r = Rotary(2, 3);
 
OneButton button1(A4, true);

//int keypad_value = 0;
// keypad_value_old = 0;

char btn_push;

byte mainMenuPage = 1;
byte mainMenuPageOld = 1;
byte mainMenuTotal = 4;

void setup() {
 //setup LCD
 lcd.begin(16,2);  //Initialize a 2x16 type LCD

 // link the button 1 functions.
 button1.attachClick(click1);
 button1.attachDoubleClick(doubleclick1);
 button1.attachLongPressStart(longPressStart1);
 button1.attachLongPressStop(longPressStop1);
 button1.attachDuringLongPress(longPress1);
 
   
   lcd.clear();
   lcd.print("*** WELCOME! ***");
   lcd.setCursor(0,1);
   lcd.print("Initializing");
   for (int i=0; i<10; ++i){
   if (i%3 == 0) lcd.print(".");
   delay(200);
   }
   
   MainMenuDisplay();
   delay(100);
}

void loop()
{
  btn_push = ReadKeypad();
  button1.tick();
 
  MainMenuBtn();

   if(btn_push == 'S')//enter selected menu
   {
       switch (mainMenuPage)
       {
           case 1:
             MenuA();
             break;
           case 2:
             MenuB();
             break;
           case 3:
             MenuC();
             break;
           case 4:
             MenuD();
             break;
         
       }

         MainMenuDisplay();
 }
}
//--------------- End of loop() loop ---------------------

void click1() {
 btn_push ='L';
 
}
void doubleclick1() {
 btn_push ='S';
 
}
void longPressStart1() {
 btn_push ='X';
}
void longPress1() {
 btn_push ='R';

}
void longPressStop1() {
 btn_push ='N';

}


void MenuA()
{  

   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Inside Menu 1");
   
    while(ReadKeypad()!= 'R')
  {
    //MainMenuDisplay();
   
  }
 
}
void MenuB()
{  
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Inside Menu 2");
 
   while (ReadKeypad()!= 'L')
   {
   
   }
}
void MenuC()
{  
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Inside Menu 3");
 
   while(ReadKeypad()!= 'L')
   {
       //Insert Task for Menu C here
     
   }
}
void MenuD()
{  
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Inside Menu 4");
 
   while(ReadKeypad()!= 'L')
   {
       //Insert Task for Menu D here
     
   }
}

void MainMenuDisplay()
{
   lcd.clear();
   lcd.setCursor(0,0);
   switch (mainMenuPage)
   {
       case 1:
         lcd.print("1. MENU");
         break;
       case 2:
         lcd.print("2. MENU");
         break;
       case 3:
         lcd.print("3. MENU");
         break;
       case 4:
         lcd.print("4. MENU");
         break;
       case 5:
         lcd.print("5. MENU");
         break;
       
   }
}

void MainMenuBtn()
{
 
   if(btn_push == 'U')
   {
       mainMenuPage++;
       if(mainMenuPage > mainMenuTotal)
         mainMenuPage = 1;
   }
   else if(btn_push == 'D')
   {
       mainMenuPage--;
       if(mainMenuPage == 0)
         mainMenuPage = mainMenuTotal;    
   }
 
   if(mainMenuPage != mainMenuPageOld) //only update display when page change
   {
       MainMenuDisplay();
       mainMenuPageOld = mainMenuPage;
   }
}


char ReadKeypad()
{
unsigned char result = r.process();
  if (result == DIR_NONE) {
  // do nothing
 }
  else if (result == DIR_CW){
   //Serial.println("ClockWise");
    return 'U';
 }
  else if (result == DIR_CCW){
  //Serial.println("CounterClockWise");
   return 'D';
 }

Kod:
void MenuA()
{  

   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Inside Menu 1");
   
    while(ReadKeypad()!= 'R')
  {
    //MainMenuDisplay();
   
  }
 
}


Chciałbym dodać kolejne pod menu , dodając najprostsze zadanie nie działa? Np: gdy nacisnę długo przycisk ('R') powinienem wrócić do głównego MainMenuDisplay. ale tak się nie dzieje , zadanie zostanie wykonane jak tylko wejdę w "MenuA()"...

(Może to coś ma spólnego)
Korzystam z Arduino 1.6.12 i pojawił się problem z rozpoznaniem Klona płyty UNO ale widzi płytą pod Arduino Mini ATmega 328 (na Arduion 1.0.5 nie miałem problemu z płytą)

Przycisk działa iż wykonuje pozostałe czynności..
Co mam tu zrobić?
Jestem początkujący w temacie wiec proszę o poradę...

poz.