Go Down

Topic: Menu Driven Program (Read 16906 times) previous topic - next topic

advancedservers

May 10, 2010, 07:25 am Last Edit: May 10, 2010, 07:25 am by advancedservers Reason: 1
Hi

Im in the process of building and automatic toilet cistern valve tester.

It basically consists of a linear actuator, LCD, and a couple of switches.

I am stuck trying to implement the sceen. The menu will have 6 options which have to be set before the actuator starts testing.

Menu Below

1. Fill Delay Time
2. Ram Delay Time
3. Ram Pulse Time
4. Start
5. Pause
6. Stop

obviously the pause and stop commands will have to run during the void loop() part of the code and opt 1, 2 & 3 could run in the void setup() part of the code. The pause function will have to not interupt the current ram cycle.

Below is my current code with static variables for opt 1,2 3.

Code: [Select]
int FRPin =  8;  
int MotorPin = 7;

int FillDelay = 10000;
int RamDelay = 500;
int RamPulse = 2000;
int TCount;
int Cycles = 10;

void setup()   {  
 Serial.begin(9600);
 Serial.println("Testing Rig V1.0");
 Serial.println("");
 
 pinMode(FRPin, OUTPUT);  
 pinMode(MotorPin, OUTPUT);
 
 //Pull Ram in fully
 Serial.println("Retracting Ram");
 digitalWrite(MotorPin, HIGH);
 digitalWrite(FRPin, LOW);
 delay(6000);
 digitalWrite(MotorPin, LOW);
}


void loop()                    
{
 for(int i=0; i<Cycles; i++){
   
   
   //Turn on Motor then push out for RamPulse Time
   Serial.println("Extending Ram");
   digitalWrite(MotorPin, HIGH);  
   digitalWrite(FRPin, HIGH);
   delay(RamPulse);
   
   //Hold the motor for RamDelay Time (-- PUSH OUT--)
   Serial.println("Holding Ram");
   digitalWrite(MotorPin, LOW);
   delay(RamDelay);
   
   //Turn on Motor then pull in for RamPulse Time
    Serial.println("Retracting Ram");
   digitalWrite(MotorPin, HIGH);  
   digitalWrite(FRPin, LOW);        
   delay(RamPulse);
   
   //Hold the motor for RamDelay Time (--PULL IN--)
   Serial.println("Holding Ram");
   Serial.println("");
   digitalWrite(MotorPin, LOW);
   delay(FillDelay);
   
   //test code
   TCount++;
   Serial.println(TCount);
 }
}


The LCD im using is a sparkfun Serial Enabled 16x2 LCD - Black on Green 5V.

Any help on how to impletmen this would be great.

thanks ben

Coding Badly


Two things will help: "finite state machine" and "blink without delay".

advancedservers

Thanks for the tips   :D

the problem Im having is not really anything to do with the lcd its more of the button press detection.

Sorry I should have explained this better!!

I have 3 buttons <Back, >Foward & OK buttons


I need to know the correct way of detecting button press's  for 3 buttons(currenty Im using the arduino example way) and when to poll them and not cause any interferance  to the loop part of the code.

thanks

Ben

MatthewS

I am new and this might not be what you are looking for but this is what I use to read my analog buttons

butt1=analogRead(button1);    //read in button 1
      if (butt1 < buttthres){   //if button one is low then button was pushed
     //do stuff here
    }
butt2..
butt3..

mowcius

#4
May 10, 2010, 09:43 am Last Edit: May 10, 2010, 09:44 am by mowcius Reason: 1
Right, I would say you want to implement a menu code routine.

Ignore the loop code. Stick code at the end of your setup routing to call Menu(); then in the menu, call other sections of code for the different things you want to do.

My code for a 20x4 serial LCD might help you. I was only using one button for moving on an option and holding it for selecting.

Code: [Select]
void Menu(){                      
 LCDclear();  
 LCDserial.print("Program Menu");
 LCDsecond();    
 switch (programnumber) {                  
 case 1:                                
   LCDserial.print("Race Timer");
   LCDthird();
   LCDserial.print("Track: ");
   LCDserial.print(distance);
   LCDserial.print("m");    
   break;                              
 case 2:
   LCDserial.print("Reaction Tester");
   break;
 case 3:
   LCDserial.print("Calibration Mode");
   break;
 default:
   LCDserial.print("Race Timer");
   LCDthird();
   LCDserial.print("Track: ");
   LCDserial.print(distance);
   LCDserial.print("m");
 }

 LCDserial.print(0xFE, BYTE);
 LCDserial.print(226, BYTE);
 LCDserial.print("Next >");
 while(digitalRead(starterbutton) == HIGH){}
 delay(5);
 while(digitalRead(starterbutton) == LOW){}
 menudelaycount = 0;
   while(menudelaycount <500){
   delay(1);
   menudelaycount++;
   if(digitalRead(starterbutton) == LOW){
     programnumber++;
   if(programnumber > 3){
     programnumber = 1;
   }
   Menu();
   }
 }
 switch (programnumber) {
     case 1:
       Race();
       break;
     case 2:
       Reaction();
       break;
     case 3:
       Calibration();
       break;
     default:
       Race();
     }
}


The LCDsecond(); is a seperate routine for selecting the LCD line.

The code waits until you press a button. When you press the button it times how long you press it for. If it is longer than a certain time then it 'selects' that option, if not then it moves on one option.

I can help more if you need it.

Mowcius

advancedservers

Hi Mowcius,

Thanks for your help there are a few things I need to clarify.

I like the way you use 1 button!! but when you press the button does it go LOW?

I dont understand how to call this code??

In psuedo code

Code: [Select]
has button been pressed
  if yes
    menu();
  else
     return();


that would work ok in the loop but how does this work from setup().

Do you just continually loop until its completed??

Sorry if this sounds stupid I just cant get my head around it :-[

and finally  ;D where in my code posted above do i call to see if a button has been pressed??

thanks again

fdufnews


mowcius

#7
May 10, 2010, 02:11 pm Last Edit: May 10, 2010, 02:11 pm by mowcius Reason: 1
Ok, the clarify bits of my code

void Menu(){ //starts the menu subroutine from being called in setup
 LCDclear();  //clears the LCD
 LCDserial.print("Program Menu"); //prints to the lcd on first line
 LCDsecond(); //prints to lcd on second line etc...
 switch (programnumber) { //reads the program number (set as 1 in setup)
 case 1:            //displays the first menu item              
   LCDserial.print("Race Timer");
   LCDthird();
   LCDserial.print("Track: ");
   LCDserial.print(distance);
   LCDserial.print("m");    
   break;
 case 2:        //displays the second menu item
   LCDserial.print("Reaction Tester");
   break;
 case 3:       //displays the third menu item
   LCDserial.print("Calibration Mode");
   break;
 default:      //if errors then it goes to default item is the race code.
   LCDserial.print("Race Timer");
   LCDthird();
   LCDserial.print("Track: ");
   LCDserial.print(distance);
   LCDserial.print("m");
 }

 LCDserial.print(0xFE, BYTE); //Command code
 LCDserial.print(226, BYTE);  //Goes to specific point to display code
 LCDserial.print("Next >");    //next code in specific position
 while(digitalRead(starterbutton) == HIGH){} //while button is high do nothing: {} (helps with debounce after button has been pressed)
 delay(5);
 while(digitalRead(starterbutton) == LOW){} //wait until button has been pressed (taken high).
 menudelaycount = 0; //clear menu delay
   while(menudelaycount <500){ //do this code within the 500 limit (over 500ms because of other stuff that is done and takes time)
   delay(1);
   menudelaycount++;
   if(digitalRead(starterbutton) == LOW){ //if the button goes low (button released) within this loop then it advances one menu option
     programnumber++; //increase the menu option
   if(programnumber > 3){
     programnumber = 1;
   }
   Menu(); //then run menu code from start: the change in program number makes it change the option it is on.
   }
 }
 switch (programnumber) { //if the button is not let go within the menu time stated above then it does not loop back and then selects the subroutine from this switch case:
     case 1:
       Race();
       break;
     case 2:
       Reaction();
       break;
     case 3:
       Calibration();
       break;
     default:
       Race();
     }
}

The switch is held low by a pulldown resistor, it waits until it is low (in case someone is holding the button), then it then waits for a button press.
When it senses a button press, it starts a looping routine for 500 (not milliseconds due to other things that happen apart from the delay(1); ).
If the button is let go within that time (normal quick press), it advances the menu item on. If it is held the whole time then it takes the program number, gets to the end of the code and runs the selected program.

You probably want to do it with an up/down menu on your 16x2 rather than the 20x4 serial LCD I did it on but the code could be pretty similar.

If you wanted to do it up/down/select then that should not be too difficult either.
Assign each program part a seperate sub-routine and a number, then display the first 2 programs (numbers i and i+1), if you press the down button, advance the i value and run the display routine again. If you press the up button, decrease the i value (unless it is 1) and run again. Upon select button press, read the program number and run a switch case routine to select the corresponding sub-routine.

Hope that helps you,

Mowcius  :)

advancedservers

#8
May 11, 2010, 06:05 am Last Edit: May 11, 2010, 06:09 am by advancedservers Reason: 1
Thank Mowcius!!

I have apadapted your code into my program and the mainmenu() sub is working.

I am having problems with the fillMenu() sub.  Once I pick a delay time
it types the serial command the same amount of time (code highlighted below)

Code: [Select]
void mainMenu()
{
//LCDclear();  //clears the LCD
// Serial.println("Program Menu"); //prints to the lcd on first line -------------------------------------------------------------------------------------------> NOT NEEDED???
// LCDsecond(); //prints to lcd on second line etc... -------------------------------------------------------------------------------------------> NOT NEEDED???
switch (programnumber) { //reads the program number (set as 1 in setup)
case 1:            //displays the first menu item              
  Serial.println("Set Fill Delay");
  break;
case 2:        //displays the second menu item
  Serial.println("Reset Fill Delay");
  break;
case 3:       //displays the third menu item
  Serial.println("Ram Pulse");
  break;
case 4:            //displays the first menu item              
  Serial.println("Start");
  break;
case 5:        //displays the second menu item
  Serial.println("Pause");  
  break;
case 6:       //displays the third menu item
  Serial.println("Stop");
  break;
default:      //if errors then it goes to default item
  Serial.println("Fill Delay");
}

//Serial.println(0xFE, BYTE); //Command code-------------------------------------------------------------------------------------------> NOT NEEDED???
//Serial.println(226, BYTE);  //Goes to specific point to display code-------------------------------------------------------------------------------------------> NOT NEEDED???
Serial.println("Next >");    //next code in specific position
while(digitalRead(menuButton) == HIGH){} //while button is high do nothing: {} (helps with debounce after button has been pressed)
delay(5);
while(digitalRead(menuButton) == LOW){} //wait until button has been pressed (taken high).
  menudelaycount = 0; //clear menu delay
  while(menudelaycount <500){ //do this code within the 500 limit (over 500ms because of other stuff that is done and takes time)
  delay(1);
  menudelaycount++;
  if(digitalRead(menuButton) == LOW){ //if the button goes low (button released) within this loop then it advances one menu option
    programnumber++; //increase the menu option
  if(programnumber > 6){
    programnumber = 1;
  }
  mainMenu(); //then run menu code from start: the change in program number makes it change the option it is on.
  }
}
switch (programnumber) { //if the button is not let go within the menu time stated above then it does not loop back and then selects the subroutine from this switch case:
    case 1:
      Serial.println("Fill Func");
      fillMenu();
      break;
    case 2:
       Serial.println("Delay Func");
      break;
    case 3:
       Serial.println("Pulse Func");
      break;
     case 4:
       Serial.println("Start Func");
      break;
    case 5:
       Serial.println("Pause Func");
      break;
    case 6:
       Serial.println("Stop Func");
      break;
    default:
       Serial.println("Default Race");
    }
}

void fillMenu()
{
  FillDelay = FillDelay +3000;
  Serial.println(FillDelay);
 
  Serial.println("Next >");    
  while(digitalRead(menuButton) == HIGH){}
    delay(5);
  while(digitalRead(menuButton) == LOW){}
    menudelaycount = 0;
  while(menudelaycount <500){
  delay(1);
  menudelaycount++;
  if(digitalRead(menuButton) == LOW){
    //programnumber++;
  if(FillDelay > 120000){
    FillDelay = 0;
  }
  fillMenu();
 }
}
  [glow]Serial.print("Fill Delay set @ ");
  Serial.print(FillDelay);[/glow]
}


Here is my Serial Output (I dont yet have my LCD some Im using Serial insted)

Set Fill Delay
Next >
Fill Func
4000
Next >
7000
Next >
Fill Delay set @ 7000Fill Delay set @ 7000Extending Ram
Holding Ram
Retracting Ram
Holding Ram


as you can see above I pressed the button twice and it prints the serial string twice. If i press the button 3 times it prints the serial string 3 times......

thanks again for all your help

Ben


mowcius

I will take a look later when I have a little more time so check back in a few hours :)

Mowcius


mowcius

#11
May 11, 2010, 01:31 pm Last Edit: May 11, 2010, 01:42 pm by mowcius Reason: 1
Ok, not quite sure what you want to do exactly now.
This is the menu you want to do:
Quote
1. Fill Delay Time
2. Ram Delay Time
3. Ram Pulse Time
4. Start
5. Pause
6. Stop


Just ignore what I said before about one button.
Do you want to have 3 buttons, one left, one right and one select?
Do you want to have 3 buttons, one up, one down and one select?
Or do you want a single scrolling menu on one button (seperate screen change for each item or scrolling up/down?)

The only reason why I used the one button was becuase I was short on I/O pins. If I had had more pins then I would have done a better menu which scrolled up/down, with 3 buttons :)

If you want a scrolly up down menu then this is what I am thinking. It will also help me with coding a scrolling menu on my 40x4 LCD so I'm not just doing it for you :P  ;D

My idea:
First screen:
----------------------------------
Fill Delay     <
Ram Delay
----------------------------------

After one press of the down button:
----------------------------------
Fill Delay
Ram Delay      <
----------------------------------

After the next press of the down button:
----------------------------------
Ram Delay
Ram Pulse      <
----------------------------------

If you then press the up button:
----------------------------------
Ram Delay      <
Ram Pulse
----------------------------------

If you get to the botton of the menu:
----------------------------------
Stop            <
----------------
----------------------------------

Maybe make it so if you get to the top it cycles back round to the start..

What do you think?

The code you posted has a load of errors so I am not going to try and correct it at the moment.
You have the basics but I think the type of menu I described above would be more suitable.
If you just want a single button (but not so obvious as to how to use) menu then I can sort out that code for you.
When I get home, I will also have my serial LCD to play on and test with.

Mowcius

advancedservers

Hi Mowcius

That would be GREAT!!!!

I just want to thank you for all your help.  I will have my LCD tommorrow too!!

thanks

ben

mowcius

I presume you are wanting a 2 line up/down scrolling menu then.

I will see what I can do.

Mowcius

mowcius

Here is my 15 minutes...

It is not very good code wise but it works... Just...

You might need to change the position code for the 16x2 serial LCD but it should help anyway.

Quote
#include <NewSoftSerial.h>

NewSoftSerial LCDserial(0, 2);

int prognumber = 1;
int line = 1;

int down = 8;
int select = 9;
int up = 10;

void menu();
void LCDclear();
void LCDfirst();
void LCDsecond();
void LCDmenudisplay();

void first();
void second();
void third();
void fourth();
void fifth();

void setup() {
  LCDserial.begin(9600);
  menu();
}

void loop() {
  //nothing to loop but is required in every sketch.
}

void LCDclear(){
  LCDserial.print(0xFE, BYTE);      
  LCDserial.print(0x01, BYTE);
}

void LCDfirst(){
  LCDserial.print(0xFE, BYTE);
  LCDserial.print(128, BYTE);
}
void LCDsecond(){
  LCDserial.print(0xFE, BYTE);
  LCDserial.print(192, BYTE);
}

void menu() {
  while(digitalRead(up) == HIGH or digitalRead(down) == HIGH or digitalRead(select) == HIGH){};
  delay(50);
  LCDclear();
  LCDfirst();
  if(prognumber > 1){
  prognumber--;
  }
  else{
    prognumber == 1;
  }
  LCDmenudisplay();
  LCDsecond();
  prognumber++;
  LCDmenudisplay();
  if(line == 1){
    LCDserial.print(0xFE, BYTE);
    LCDserial.print(143, BYTE);
    LCDserial.print("<");
  }
  else {
    LCDserial.print(0xFE, BYTE);
    LCDserial.print(207, BYTE);
    LCDserial.print("<");
  }
  while(digitalRead(up) == LOW && digitalRead(down) == LOW && digitalRead(select) == LOW){}
  if(digitalRead(down) == HIGH){
  line++;
  if(line > 2){
  line = 2;
  prognumber++;
  if(prognumber > 4){
  prognumber = 5;
  }
  }
  menu();
  }
  else if(digitalRead(up) == HIGH){
  line--;
  if(line < 1){
  line = 1;
  prognumber--;
  }
  menu();
  }
  else if(digitalRead(select) == HIGH){
  if(line == 1){
    prognumber--;
  }
  switch (prognumber) {                  
  case 1:                                
    first(); 
    break;                              
  case 2:
    second();
    break;
  case 3:
    third();
    break;
  case 4:
    fourth();
    break;
  case 5:
    fifth();
    break;
  default:
    first();
  }
  }
}

void LCDmenudisplay() {
  switch (prognumber) {                  
  case 1:                                
    LCDserial.print("First");  
    break;                              
  case 2:
    LCDserial.print("Second");
    break;
  case 3:
    LCDserial.print("Third");
    break;
  case 4:
    LCDserial.print("Fourth");
    break;
  case 5:
    LCDserial.print("Fifth");
    break;
  default:
    LCDserial.print("First");
  }
  
}

void first() {
  digitalWrite(13, HIGH);
  menu();
}

void second() {
  menu();
}

void third() {
  menu();
}

void fourth() {
  menu();
}

void fifth() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  menu();
}


Mowcius

Go Up