Go Down

Topic: Menu help (Read 591 times) previous topic - next topic

liquidarts

Am currently working on a lighting controller and am really lost....Am using a sketch started by harlequin on here for the menu.

He left off with still a lot of work to be done but was hoping someone could give me a push in the right direction with the coding.

His code
Code: [Select]
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);//delares the LCD Pins for the liquidCrystal library methods.
int x = 0;//Used for testing purposes to show the LED strength
int menuOption = 3;//used for the Switch statement, each menu has its own value,
int tankTemp = 97.6;//used for testing purposes, in real build this value with come from a temp probe
int timeHour = 12;//used for testing purposes, in real build RTC wil give this value
int timeMinute = 30;//used for testing purposes, in real build RTC wil give this value
//-----------------------------------------------------
void setup() {//ran once AKA welcome screen
  lcd.begin(16, 2);//sets the LCD screen dimentions up
  lcd.print(" Lighting Control ");
  lcd.setCursor(0,1);
  lcd.print("by ");
  delay(5000);
  lcd.clear();
}
//-----------------------------------------------------
void loop() {
  switch(menuOption){//loads each menu depending on what value "menuOption" has at the start of the loop.
   
    case 1:  mainMenuOneOne();break;
    case 2:  mainMenuOneTwo();break;
   
    case 3:  mainMenuTwoOne();break;
    case 4:  mainMenuTwoTwo();break;
   
    case 5:  setIntensityOne();break;
    case 6:  setIntensityTwo();break;
    case 7:  setIntensityThree();break;
   
    case 8:  setLightsOne();break;
    case 9:  setLightsTwo();break;
   
    case 10: setTimePeriodOne();break;
    case 11: setTimePeriodTwo();break;
   
    default: idleMenu();break;
  } 
}
//-----------------------------------------------------
void idleMenu(){//case 0
  //this is the idle screen which will be the ran as default, when the user hasnt pressed anything, after X amount of time if the user hasnt pressed anything load this menuOption
  lcd.setCursor(0,0);
  lcd.print("Temp:"); lcd.print(tankTemp);  lcd.print("*F ");//temp half, top line
  lcd.print(timeHour); lcd.print(":"); lcd.print(timeMinute);// time half, top line
  lcd.setCursor(0,1);
  lcd.print("WHT:");lcd.print(x);lcd.print(" BLU:");lcd.print(x);
  if(x <100){//used in testing, real value will come from the led's/set by the user in the SET intensity menu option
    x++;
    delay(5000);
    lcd.clear();
  }
  //If any button pressed, load mainMenuOne()/ menuOption = 1;
}
//-----------------------------------------------------

void mainMenuOneOne(){ //case 1
  //main menu,
  lcd.setCursor(0,0);
  lcd.print("1.SET LIGHTS   *");
  lcd.setCursor(0,1);
  lcd.print("2.VIEW PERAMS");
  lcd.setCursor(0,0);
   
   
  /*if V pressed load menuOptionOneTwo();
    if ^ pressed do nothing
    if < pressed load idleMenu()/ menuOption = 0;
    if > pressed load setLightsOne();/ menuOption = 8;
    if * pressed load setLightsOne();/ menuOption = 9;
  */
}
void mainMenuOneTwo(){ //case 2
  //main menu,
  lcd.setCursor(0,0);
  lcd.print("1.SET LIGHTS");
  lcd.setCursor(0,1);
  lcd.print("2.VIEW PERAMS  *");
  lcd.setCursor(0,0);
 
   
  /*if V pressed load menuOptionTwoOne()/menuOption = 3;
    if ^ pressed load menuOptionOneOne()/menuOption = 2;
    if < pressed load idleMenu()/menuOption = 0;
    if > pressed load viewPerams(); not coded yet
    if * pressed load viewPerams(); not coded yet
  */
}
void mainMenuTwoOne(){// case 3
  lcd.setCursor(0,0);
  lcd.print("3.BLANK        *");
  //flashing pixel
  lcd.setCursor(0,1);
  lcd.print("4.BLANK");
  lcd.setCursor(0,0);


  /*if V pressed load menuOptionTwoTwo()/menuOption = 4;
    if ^ pressed load menuOptionOneTwo()/menuOption = 2;
    if < pressed load idleMenu()/menuOption = 0;
    if > pressed load 3rd menu;/not coded yet
    if * pressed load 3rd menu()/not coded yet
  */
}

void mainMenuTwoTwo(){// case 4
  lcd.setCursor(0,0);
  lcd.print("3.BLANK");
  lcd.setCursor(0,1);
  lcd.print("4.BLANK        *");
  //flashing pixel
  lcd.setCursor(0,0);


  /*if V pressed do nothing;
    if ^ pressed load menuOptionTwoOne()/menuOption = 3;
    if < pressed load idleMenu()/menuOption = 0;
    if > pressed load 4rd menu;/not coded yet
    if * pressed load 4th menu();/not coded yet
  */
}

//----------------------------------------------------

void setLightsOne(){// case 8
  lcd.setCursor(0,0);
  lcd.print("1.SET INTENSITY*");
  lcd.setCursor(0,1);
  lcd.print("2.SET TIMES");
  lcd.setCursor(0,0);


/*if V pressed load setLightsTwo()/menuOption = 9;
    if ^ pressed do nothing
    if < pressed load idleMenu()//menuOption = 0;
    if > pressed load setintensityOne()/menuOption = 5;
    if * pressed load setLightsTwo()/menuOption = 9;
  */
}


void setLightsTwo(){//case 9
  lcd.setCursor(0,0);
  lcd.print("1.SET INTENSITY");
  lcd.setCursor(0,1);
  lcd.print("2.SET TIMES    *");
  //flash pixel
  lcd.setCursor(0,0);


/*if V pressed do nothing;
    if ^ pressed load setLightsOne()/menuOption = 8;
    if < pressed load mainMenuOneOne()/menuOption = 1;
    if > pressed load setTimeOne()/menuOption = 10;
    if * pressed load setTimeOne()/menuOption = 10;
  */
}

//----------------------------------------------------
void setIntensityOne(){// case 5
  lcd.setCursor(0,0);
  lcd.print("1. SET BLUE MAX");
  lcd.setCursor(0,1);
  lcd.print("   100%");
  lcd.setCursor(0,0);
 
 
   /*if V pressed value-- While => 0;
    if ^ pressed value++ while <= 100;
    if < pressed do nothing;
    if > pressed load setIntenistyTwo()//menuOption = 6;
    if * pressed load setIntenistyTwo()/menuOption = 6;
  */
}

void setIntensityTwo(){//case 6
  lcd.setCursor(0,0);
  lcd.print("1. SET WHITE MAX");
  lcd.setCursor(0,1);
  lcd.print("   100%");
  lcd.setCursor(0,0);
 
 
  /*if V pressed value-- While => 0;
    if ^ pressed value++ while <= 100;
    if < pressed do nothing;
    if > pressed load setIntenistyThree()/menuOption = 7;
    if * pressed load setIntenistyThree()//menuOption = 7;
  */
}

void setIntensityThree(){//case 7
  lcd.setCursor(0,0);
  lcd.print("LIGHTS SET");
  lcd.setCursor(0,1);
  delay(5000);
  lcd.setCursor(0,0);
  menuOption = 1;


  /*if V pressed do nothing;
    if ^ pressed do nothing;
    if < pressed do nothing;
    if > pressed load mainMenuOne()/menuOption = 1;
    if * pressed load mainMenuOne()/menuOption = 1;
  */
}
//------------------------------------------------- 
void setTimePeriodOne(){//case 10
  lcd.setCursor(0,0);
  lcd.print("1.SET START    *");
  lcd.setCursor(0,1);
  lcd.print("2.SET END");
  lcd.setCursor(0,0);
 
  /*if V pressed load setTimePeriodTwo()/menuOption = 11;
    if ^ pressed do nothing;
    if < pressed load setLightsTwo()/menuOption = 9;
    if > pressed load setStartTime/not coded yet;
    if * pressed load setStartTime)/not coded yet
  */
}

void setTimePeriodTwo(){//case 11
  lcd.setCursor(0,0);
  lcd.print("1.SET START");
  lcd.setCursor(0,1);
  lcd.print("2.SET END      *");
  lcd.setCursor(0,0);

   /*if V pressed do nothing;
    if ^ pressed load setTimePeriodOne/menuOption = 10;
    if < pressed load setLightsTwo()/menuOption = 9;
    if > pressed load setEndTime/not coded yet
    if * pressed load setEndTime)/not coded yet
  */
}
//-------------------------------------------------



liquidarts

Am looking to add the lighting coding from the sketch I have so that the lighting times and variables can be set by a user using it.Once someone shows me how to set up the lighting code part i'm sure I can figure out the rest.

Code: [Select]
/*

originally written by Christian, cptbjorn@gmail.com

*/


#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 //set rtc
#include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  R E L A Y   P A R T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S I M P L E   O N   A N D   O F F   F E A T U R E |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



const int ledPin1 =  2;          // pin number for relay 1
const int ledPin2 =  8;          // pin number for relay 2


int ledState1 = LOW;             
int ledState2 = LOW;
long previousMillis1 = 0;       
long previousMillis2 = 0;
long interval1 = 30000;          // interval at which to blink (milliseconds) for RELAY1
long interval2 = 50000; // interval at which to blink (milliseconds) for RELAY2


/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L E D   D I M M I N G   P A R T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  F A D E S   I N   A N D   O U T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



int blueramptime = 60 ;    // time for blue LEDs to dim on and off in minutes
int whiteramptime = 180 ;  // time for white LEDs to dim on and off in minutes
int bluemin = 0 ;          // minimmum dimming value of blue LEDs, range of 0-255
int bluemax = 255 ;        // maximum dimming value of blue LEDs, range of 0-255
int whitemin = 0 ;         // minimum dimming value of white LEDs, range of 0-255
int whitemax = 255 ;       // maximum dimming value of white LEDs, range of 0-255
int photoperiod = 240 ;    // amount of time array is on at full power in minutes
int ontime = 10 ;          // time of day (hour, 24h clock) to begin photoperiod fade in
int blue = 3;              // blue LEDs connected to digital pin 3 (pwm)
int white = 11;            // white LEDs connected to digital pin 11 (pwm)


int bluepercent[11] = { 0, 1, 2, 5, 8 ,12, 18, 27, 44, 80, 255 };   // this line is needed if you are using meanwell ELN60-48D
int whitepercent[11] = { 0, 1, 2, 5, 8 ,12, 18, 27, 44, 80, 255 };   // these are the values in 10% increments

//int bluepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };   // this line is needed if you are using meanwell ELN60-48P
//int whitepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };   // these are the values in 10% increments




// int pwm_one = 10;       // extra pwm pin for future use
// int pwm_one = 9;        // extra pwm pin for future use


LiquidCrystal lcd(12, 13, 4, 5, 6, 7);   // typically 8, 9, 4, 5, 6, 7
                                         // have to change to free up more pwm pins



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  R T C   C L O C K   D S 1 3 0 7  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



byte decToBcd(byte val)    // Convert normal decimal numbers to binary coded decimal
{
  return ( (val/10*16) + (val%10) );
}


byte bcdToDec(byte val)    // Convert binary coded decimal to normal decimal numbers
{
  return ( (val/16*10) + (val%16) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
  Wire.send(decToBcd(minute));
  Wire.send(decToBcd(hour));   // If you want 12 hour am/pm you need to set
  // bit 6 (also need to change readDateDs1307)
  Wire.send(decToBcd(dayOfWeek));
  Wire.send(decToBcd(dayOfMonth));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second = bcdToDec(Wire.receive() & 0x7f);
  *minute = bcdToDec(Wire.receive());
  *hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
  *dayOfWeek = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month = bcdToDec(Wire.receive());
  *year = bcdToDec(Wire.receive());
}



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  O N E S E C O N D |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



void onesecond() //function that runs once per second while program is running
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  lcd.setCursor(0, 0);
  if(hour>0)
  {
    if(hour<=12)
    {
      lcd.print(hour, DEC);
    }
    else
    {
      lcd.print(hour-12, DEC);
    }
  }
  else
  {
    lcd.print("12");
  }
  lcd.print(":");
  if (minute < 10) {
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  lcd.print(":");
  if (second < 10) {
    lcd.print("0");
  }
  lcd.print(second, DEC);
  if(hour<12)
  {
    lcd.print("am");
  }
  else
  {
    lcd.print("pm");
  }
  lcd.print(" ");
  delay(1000);
}




/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  R E L A Y 1 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/




void relay1()  //FUNCTION TO TURN ON AND OFF RELAY 1.
{
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis1 > interval1)
  {
    previousMillis1 = currentMillis;   
    if (ledState1 == LOW)
      ledState1 = HIGH;
    else
      ledState1 = LOW;
    digitalWrite(ledPin1, ledState1);
  }
}



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  R E L A Y 2 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/




void relay2()
{
  unsigned long currentMillis2 = millis();

  if(currentMillis2 - previousMillis2 > interval2)
  {
    previousMillis2 = currentMillis2;   
    if (ledState2 == LOW)
      ledState2 = HIGH;
    else
      ledState2 = LOW;
    digitalWrite(ledPin2, ledState2);
  }
}




/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S E T U P  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/


void setup() {
  pinMode(ledPin1, OUTPUT);    // set the digital pin as output:
  pinMode(ledPin2, OUTPUT);    // set the digital pin as output:
 


  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S E T U P - D I S P L A Y |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();

  // Change these values to what you want to set your clock to.
  // You probably only want to set your clock once and then remove
  // the setDateDs1307 call.
  second = 56;
  minute = 57;
  hour = 23;
  dayOfWeek = 6;  // Sunday is 0
  dayOfMonth = 26;
  month = 2;
  year = 11;
  //setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

  analogWrite(blue, bluemin);
  analogWrite(white, whitemin);
  lcd.begin(16, 2); // set up the LCD's number of rows and columns:
  //  lcd.print("12:00 80.6"); // Print a message to the LCD.
  //  lcd.print(char(223));
  lcd.setCursor(0, 1);
  lcd.print("blue:");
  lcd.print(33*bluemin/85);
  lcd.setCursor(8, 1);
  lcd.print("white:");
  lcd.print(33*whitemin/85); 
}




/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/




void loop()
{
  onesecond();
  relay2();
  relay1();







liquidarts

Part two of the code
Code: [Select]

  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - D I M   F U N C T I O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  int daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
     

  int bluerampup;
     if (daybyminute >= (ontime*60))
       bluerampup = (((ontime*60) + blueramptime) - daybyminute);
     else
       bluerampup = blueramptime;
       
  int whiterampup;
    if (daybyminute >= (ontime*60 + blueramptime))
       whiterampup = (((ontime*60) + blueramptime + whiteramptime) - daybyminute);
     else
       whiterampup = whiteramptime;

  int whiterampdown;
    if (((ontime * 60) + photoperiod + blueramptime + whiteramptime) <= daybyminute)
      whiterampdown = (((ontime*60) + photoperiod + blueramptime + 2*whiteramptime) - daybyminute);
    else
      whiterampdown = whiteramptime;
     
  int bluerampdown;
    if (((ontime * 60) + photoperiod + blueramptime + 2*whiteramptime) <= daybyminute)
      bluerampdown = (((ontime*60) + photoperiod + 2*blueramptime + 2*whiteramptime) - daybyminute);
    else
      bluerampdown = blueramptime;






  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - F A D E  I N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/


if (daybyminute >= (ontime*60))
  {
    if (daybyminute <= ((ontime*60) + blueramptime + (whiteramptime/10*9))) //if time is in range of fade in, start fading in + (whiteramptime/10*9)
    {
      // fade blue LEDs in from min to max.
      for (int i = 1; i <= 10; i++) // setting ib value for 10% increment. Start with 0%
      {
        analogWrite(blue, bluepercent[i]);
        lcd.setCursor(5, 1);
        lcd.print(i);
        lcd.print(" ");
     
        int countdown = ((bluerampup*60)/10); // calculates seconds to next step
        while (countdown>0)
          {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      }     

      // fade white LEDs in from min to max.
      for (int i = 1; i <= 10; i++) // setting i value for 10% increment. Start with 0%
      {
        analogWrite(white, whitepercent[i]);
        lcd.setCursor(14, 1);
        lcd.print(i);
        lcd.print(" ");

        int countdown = ((whiterampup*60)/10); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      }
    }
  }


  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - M A X  V A L U E |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



if (daybyminute >= ((ontime * 60) + blueramptime + whiteramptime))
  {
    if ( daybyminute < ((ontime * 60) + blueramptime + whiteramptime + photoperiod)) // if time is in range of photoperiod, turn lights on to maximum fade value
    {
      analogWrite(blue, 255);
        lcd.setCursor(5, 1);
        lcd.print(10);
        lcd.print(" ");
      analogWrite(white, 255);
        lcd.setCursor(14, 1);
        lcd.print(10);
        lcd.print(" ");
     
    }
  }



  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - F A D E  O U T |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



  if (((ontime * 60) + photoperiod + blueramptime + whiteramptime) <= daybyminute)
  {
    if (((ontime * 60) + photoperiod + whiteramptime + 2*blueramptime + (blueramptime/10*9)) >= daybyminute)
    {
      // fade white LEDs out from max to min in increments of 1 point:
      for (int i = 10; i >= 0; i--) // setting i value for 10% increment. Start with 10%
      {
        analogWrite(blue, 255);
        lcd.setCursor(5, 1);
        lcd.print(10);
        lcd.print(" ");
       
        analogWrite(white, whitepercent[i]);
        lcd.setCursor(14, 1);
        lcd.print(i);
        lcd.print(" "); 

        int countdown = ((whiterampdown*60)/10); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }

      }

      // fade blue LEDs out from max to min in increments of 1 point:
      for (int i = 10; i >= 0; i--) // setting i value for 10% increment. Start with 10%
      {
        analogWrite(blue, bluepercent[i]);
        lcd.setCursor(5, 1);
        lcd.print(i);
        lcd.print(" ");

        int countdown = ((bluerampdown*60)/10); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      }
    }
  }


}  // END LOOP


Go Up