Pages: 1 [2]   Go Down
Author Topic: Menu Driven Program  (Read 15321 times)
0 Members and 1 Guest are viewing this topic.
North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the void first(){   } etc are where you would stick the pieces of code.

No doubt someone can write something much better (and so could I with more time and less tiredness).

The newsoftserial could be changed to the softwareserial library with no problems but the newsoftserial library is better...

I was testing it using the top 2 lines of my 40x4 so that is why the positioning is probably off for the second line text...

The code basically does what I explained a few posts up but you will see if you try it.
Look at serial LCD datasheet to find out the positioning values for the 16x2 LCD.

Mowcius

Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You still around reading this?

Mowcius
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Mowcius Sorry Yes Im still reading this I have been away and haven't had a chance to come back here.

I have been playing with the first code you gave me ( 1 Button) and I have it working fully.

Do you mind having a quick look to see if there are an obvious errors/problems.

Code:
/* Menu Code by Adapated from Mowcius Code
*/

#include <NewSoftSerial.h>
#include <EEPROM.h>

NewSoftSerial LCDserial(0, 3);


// START Menu Stuff
int menuButton = 2;    
int programnumber = 1;
int menudelaycount = 0;
int menudelaycount2 = 0;
// End Menu Stuff

long ramCycle = 0;

int FRPin =  8;  
int MotorPin = 7;

long fillDelay = 1000;
long holdDelay = 500;
int ramPulse = 500;

void setup()   {  
  LCDserial.begin(9600);
  Serial.begin(9600);
  LCDclear();
  selectLineOne();
  LCDserial.print("Azzurra Bathroom");
  selectLineTwo();
  LCDserial.print("Testing Rig V1.0");
  delay(5000);
  pinMode(menuButton, INPUT);
  
  pinMode(FRPin, OUTPUT);  
  pinMode(MotorPin, OUTPUT);
  
  //Pull Ram in fully
  retractRam();
  
  mainMenu();
}

void loop(){
}


void start()                    
{
  delay(3000);
  while(digitalRead(menuButton) == LOW){
    
    LCDclear();
  
    //Turn on Motor then push out for ramPulse Time
    selectLineOne();
    LCDserial.print("Extending Ram");
    digitalWrite(MotorPin, HIGH);  
    digitalWrite(FRPin, HIGH);
    delay(ramPulse);
    checkPress();
    
    //Hold the motor for holdDelay Time (-- PUSH OUT--)
    LCDclear();
    selectLineOne();
    LCDserial.print("Holding Ram");
    digitalWrite(MotorPin, LOW);
    delay(holdDelay);
    checkPress();
    
    //Turn on Motor then pull in for ramPulse Time
    LCDclear();
    selectLineOne();
    LCDserial.print("Retracting Ram");
    digitalWrite(MotorPin, HIGH);  
    digitalWrite(FRPin, LOW);        
    delay(ramPulse);
    checkPress();
    
    //Hold the motor for holdDelay Time (--PULL IN--) & Print Current Ram Count
    LCDclear();
    selectLineOne();
    LCDserial.print("Holding Ram");
    digitalWrite(MotorPin, LOW);
    ramCycle++;
    LCDclear();
    selectLineOne();
    LCDserial.print("Ram Count   ");
    selectLineTwo();
    LCDserial.print(ramCycle);
    delay(fillDelay);
    checkPress();
    
  }
 
}

void retractRam()
{
  LCDclear();
  selectLineOne();
  LCDserial.print("Retracting Ram");
  selectLineTwo();
  LCDserial.print("to Home Position");
  digitalWrite(MotorPin, HIGH);
  digitalWrite(FRPin, LOW);
  delay(6000);
  digitalWrite(MotorPin, LOW);
}

void mainMenu()
{
 LCDclear();  //clears the LCD
 selectLineOne();
 switch (programnumber) { //reads the program number (set as 1 in setup)
 case 1:            //displays the first menu item              
   LCDserial.print("Set Fill Delay");
   break;
 case 2:        //displays the second menu item
   LCDserial.print("Hold Delay");
   break;
 case 3:       //displays the third menu item
   LCDserial.print("Ram Pulse");
   break;
 case 4:            //displays the first menu item              
   LCDserial.print("Start");
   break;
 case 5:       //displays the third menu item
   LCDserial.print("Stop");
   break;
 default:      //if errors then it goes to default item
   LCDserial.print("Fill Delay");
}
 selectLineTwo();
 LCDserial.print("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 > 5){
     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:
       //LCDserial.print("Fill Func");
       fillMenu();
       break;
     case 2:
       // LCDserial.print("Delay Func");
        delayMenu();
       break;
     case 3:
        //LCDserial.print("Pulse Func");
        pulseMenu();
       break;
      case 4:
        LCDclear();
        selectLineOne
        LCDserial.print("Starting Test");
        start();
       break;
     case 5:
        LCDserial.print("Stop Func");
       break;
     default:
        LCDserial.print("Default Start Func");
     }
}

void fillMenu()
{
   LCDclear();
   selectLineOne();
   LCDserial.print(fillDelay);
   selectLineTwo();
   LCDserial.print("Next >");    
  
   while(digitalRead(menuButton) == HIGH){}
     delay(5);
   while(digitalRead(menuButton) == LOW){}
     menudelaycount = 0;
   while(menudelaycount <500){
     delay(1);
     menudelaycount++;
     if(digitalRead(menuButton) == LOW){
        fillDelay = fillDelay +3000;
     if(fillDelay > 120000){
       fillDelay = 0;
     }
   fillMenu();
   }
 }
   LCDclear();
   selectLineOne();
   LCDserial.print("Fill Delay set @ ");
   selectLineTwo();
   LCDserial.print(fillDelay);
   delay(3000);
   mainMenu();
}

void delayMenu()
{
  LCDclear();
  selectLineOne();
  LCDserial.print(holdDelay);
  selectLineTwo();
  LCDserial.print("Next >");    
  
   while(digitalRead(menuButton) == HIGH){}
     delay(5);
   while(digitalRead(menuButton) == LOW){}
     menudelaycount = 0;
   while(menudelaycount <500){
     delay(1);
     menudelaycount++;
     if(digitalRead(menuButton) == LOW){
        holdDelay = holdDelay +500;
     if(holdDelay > 60000){
       holdDelay = 0;
     }
   delayMenu();
   }
 }
   LCDclear();
   selectLineOne();
   LCDserial.print("Hold Delay set @ ");
   selectLineTwo();
   LCDserial.print(holdDelay);
   delay(3000);
   mainMenu();
}

void pulseMenu()
{
   LCDclear();
   selectLineOne();  
   LCDserial.print(ramPulse);
   selectLineTwo();
   LCDserial.print("Next >");    
  
   while(digitalRead(menuButton) == HIGH){}
     delay(5);
   while(digitalRead(menuButton) == LOW){}
     menudelaycount = 0;
   while(menudelaycount <500){
     delay(1);
     menudelaycount++;
     if(digitalRead(menuButton) == LOW){
        ramPulse = ramPulse +250;
     if(ramPulse > 6000){
       ramPulse = 0;
     }
   pulseMenu();
   }
 }
   LCDclear();
   selectLineOne();
   LCDserial.print("Ram Pulse set @ ");
   selectLineTwo();
   LCDserial.print(ramPulse);
   delay(3000);
   mainMenu();
}

void checkPress()
{
  if (digitalRead(menuButton) == HIGH)
  {
    retractRam();
    LCDclear();
    selectLineOne();
    LCDserial.print("Paused");
  //  selectLineTwo();
   // LCDserial.print("Press button to goto menu");    
    delay(500);
    while(digitalRead(menuButton) == LOW){}
    mainMenu();
    
  }
}

void selectLineOne(){  //puts the cursor at line 0 char 0.
  LCDserial.print(0xFE, BYTE);   //command flag
  LCDserial.print(128, BYTE);    //position
}

void selectLineTwo(){  //puts the cursor at line 2 char 0.
  LCDserial.print(0xFE, BYTE);   //command flag
  LCDserial.print(192, BYTE);    //position
}

void LCDclear(){
  LCDserial.print(0xFE, BYTE);   //command flag
  LCDserial.print(0x01, BYTE);   //clear command
  }

Thanks again

Ben
« Last Edit: May 13, 2010, 04:51:06 pm by advancedservers » Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well not that I can see from a very quick look. If it is working then it is probably ok.

If I get time then I will load up your code on my arduino and LCD later to see what it looks like and if I can suggest any changes/improvments.

If you get the time then you might still want to take a look at the scroling menu I wrote to see if it suits you better.

Mowcius
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Mowcius,

thanks Im going to load it up tomorrow to see how it works.

thanks

Ben
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Mowcius,

I loaded your code up & it looks great and seemed to work fine, but after about 10 - 20 key presses it just hangs.

I have the buttons wired 5v to one side if the switch and the Digital pin with a 10K resistor pulling low the other side of the switch.

any ideas????

Thanks

Ben
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I loaded your code up & it looks great and seemed to work fine, but after about 10 - 20 key presses it just hangs.
Yeah I had an issue with that too. I thought it might be just my wiring at the time but it appears not.

I think I may need to stick in a few delays somewhere. I will take another look some time.

Mowcius
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Mowcius Look foward to your reply.

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Mowcius,

I think I found the error in your code

Code:
 if(line > 2){
  line = 2;
  prognumber++;
  [glow]}[/glow]
  if(prognumber > 4){
  prognumber = 5;
  }
[glow]//}[/glow]
  menu();
  }

I moved the } bracket from the commented out section to the other highlighted section and now I doesn't lock up.

I think what was happening is that when you press 2 button simultaneously it would make the line number = 1 and the prognumber >4 so the if statement would allow it to go though..

What do you think

Ben
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry Scratch that it still does it smiley-sad!!!!
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, sorry I have not had time to look at it yet.

I will post it up as a seperate topic and see if anyone else has any ideas...

Mowcius
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code issue was sorted here:
Quote

Code:
#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);
}

void loop() {
  menu();
}

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 || digitalRead(down) == HIGH || digitalRead(select) == HIGH){};
  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;
      }
    }
  }
  else if(digitalRead(up) == HIGH){
    line--;
    if(line < 1){
      line = 1;
      prognumber--;
    }
  }
  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();
    }
  }
  delay(50);
}

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);
}

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

void third() {
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
}

void fourth() {
  digitalWrite(13, HIGH);
  delay(100);
  digitalWrite(13, LOW);
  delay(100);
  digitalWrite(13, HIGH);
  delay(100);
  digitalWrite(13, LOW);
}

void fifth() {
  digitalWrite(13, LOW);
}    


 smiley

Mowcius
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino Noob - Learning :)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cheers for the code!

Trying to make my own adaptation to this, but not going very well so far smiley-sad
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Post your code and I'll have a look

Logged

Pages: 1 [2]   Go Up
Jump to: