Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *************************************************
- * "BEACH BUDDY" SUNBURN TIMER CALCULATOR CODE *
- *************************************************
- Katie Butler
- keb47@students.uwf.edu
- Digital Multimedia
- Final Project
- Summer 2014
- ************************************************
- Includes code pulled from:
- http://www.coagula.org/content/pages/tutorial-manage-menu-and-lcd-display-arduino
- Prior to compiling/uploading, install the MenuBackend library by Alexander Brevig:
- http://www.arduino.cc/playground/uploads/Profiles/MenuBackend_1-4.zip
- *************************************************/
- #include <MenuBackend.h> //MenuBackend library - copyright by Alexander Brevig
- #include <LiquidCrystal.h> //this library is included in the Arduino IDE
- const int buttonPinLeft = 8; // pin for the Left button
- const int buttonPinRight = 9; // pin for the Right button
- const int buttonPinEnter = 10; // pin for the Enter button
- const int buttonPinEsc = 11; // pin for the Esc button
- int lastButtonPushed = 0;
- float skintoneValue;
- float spfValue;
- float sensorValue;
- float uvValue;
- float timerValue;
- float getUV(){
- float sensorValue = analogRead(1);
- if (sensorValue < 1){
- return(1);
- }
- else {
- return(sensorValue*0.071); //This is the calibrated slope
- //return(10); //Uncomment to send a test value so that a meaningful calculation is possible
- }
- }
- float timerCalculation(){
- return((skintoneValue/getUV())*spfValue);
- //return((67/4)*15); //Uncomment to test that timer calculations are displaying properly on the LCD screen
- }
- int lastButtonEnterState = LOW; // the previous reading from the Enter input pin
- int lastButtonEscState = LOW; // the previous reading from the Esc input pin
- int lastButtonLeftState = LOW; // the previous reading from the Left input pin
- int lastButtonRightState = LOW; // the previous reading from the Right input pin
- long lastEnterDebounceTime = 0; // the last time the output pin was toggled
- long lastEscDebounceTime = 0; // the last time the output pin was toggled
- long lastLeftDebounceTime = 0; // the last time the output pin was toggled
- long lastRightDebounceTime = 0; // the last time the output pin was toggled
- long debounceDelay = 500; // the debounce time
- //LCD Setup:
- //Pin 1 = GND
- //Pin 2 = 5V
- //Pin 3 = Potentiometer wiper
- //Pin 4 = Arduino #2
- //Pin 5 = GND
- //Pin 6 = Arduino #3
- //Pins 7-10 = null
- //Pin 11 = Arduino #4
- //Pin 12 = Arduino #5
- //Pin 13 = Arduino #6
- //Pin 14 = Arduino #7
- //Pin 15 = 5V
- //Pin 16 = GND
- LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
- MenuBackend menu = MenuBackend(menuUsed,menuChanged);
- MenuItem menu1Item1 = MenuItem("Skin Tone");
- MenuItem menuItem1SubItem1 = MenuItem("1 -- Fair");
- MenuItem menuItem1SubItem2 = MenuItem("2 -- Light");
- MenuItem menuItem1SubItem3 = MenuItem("3 -- Medium");
- MenuItem menuItem1SubItem4 = MenuItem("4 -- Olive");
- MenuItem menuItem1SubItem5 = MenuItem("5 -- Brown");
- MenuItem menuItem1SubItem6 = MenuItem("6 -- Dark");
- MenuItem menu1Item2 = MenuItem("Sun Protection");
- MenuItem menuItem2SubItem1 = MenuItem("SPF = 0");
- MenuItem menuItem2SubItem2 = MenuItem("SPF = 15");
- MenuItem menuItem2SubItem3 = MenuItem("SPF = 30");
- MenuItem menuItem2SubItem4 = MenuItem("SPF = 45");
- MenuItem menuItem2SubItem5 = MenuItem("SPF = 50");
- MenuItem menuItem2SubItem6 = MenuItem("SPF = 70");
- void setup(){
- pinMode(buttonPinLeft, INPUT);
- pinMode(buttonPinRight, INPUT);
- pinMode(buttonPinEnter, INPUT);
- pinMode(buttonPinEsc, INPUT);
- lcd.begin(16, 2);
- menu.getRoot().add(menu1Item1); //add Skin Tone Menu
- menu1Item1.add(menuItem1SubItem1) //add Fair
- .addRight(menuItem1SubItem2) //add Light
- .addRight(menuItem1SubItem3) //add Medium
- .addRight(menuItem1SubItem4) //add Olive
- .addRight(menuItem1SubItem5) //add Brown
- .addRight(menuItem1SubItem6); //add Dark
- menuItem1SubItem1.addAfter(menu1Item2); // add SPF Menu
- menuItem1SubItem2.addAfter(menu1Item2); // add SPF Menu
- menuItem1SubItem3.addAfter(menu1Item2); // add SPF Menu
- menuItem1SubItem4.addAfter(menu1Item2); // add SPF Menu
- menuItem1SubItem5.addAfter(menu1Item2); // add SPF Menu
- menuItem1SubItem6.addAfter(menu1Item2); // add SPF Menu
- menu1Item2.add(menuItem2SubItem1)
- .addRight(menuItem2SubItem2)
- .addRight(menuItem2SubItem3)
- .addRight(menuItem2SubItem4)
- .addRight(menuItem2SubItem5)
- .addRight(menuItem2SubItem6);
- menu.toRoot();
- lcd.setCursor(0,0);
- lcd.print(" UV Timer ");
- delay(500);
- }
- //--------------------------------------//
- void loop(){
- readButtons();
- navigateMenus();
- }
- //--------------------------------------//
- void menuChanged(MenuChangeEvent changed){
- MenuItem newMenuItem=changed.to;
- lcd.setCursor(0,1);
- if(newMenuItem.getName()==menu.getRoot()){
- lcd.setCursor(0,0);
- lcd.print(" UV Timer ");
- lcd.setCursor(0,1);
- lcd.print(" Calculate Time ");
- }
- else if(newMenuItem.getName()=="1 -- Fair"){
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Select Skin Tone");
- lcd.setCursor(0,1);
- lcd.print(" 1 --- Fair ");
- skintoneValue = 67;
- }
- else if(newMenuItem.getName()=="2 -- Light"){
- lcd.print(" 2 -- Light ");
- skintoneValue = 100;
- }
- else if(newMenuItem.getName()=="3 -- Medium"){
- lcd.print(" 3 --- Medium ");
- skintoneValue = 200;
- }
- else if(newMenuItem.getName()=="4 -- Olive"){
- lcd.print(" 4 -- Olive ");
- skintoneValue = 300;
- }
- else if(newMenuItem.getName()=="5 -- Brown"){
- lcd.print(" 5 -- Brown ");
- skintoneValue = 400;
- }
- else if(newMenuItem.getName()=="6 -- Dark"){
- lcd.print(" 6 --- Dark ");
- skintoneValue = 500;
- }
- else if(newMenuItem.getName()=="SPF = 0"){
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print(" Select SPF ");
- lcd.setCursor(0,1);
- lcd.print(" SPF = 0 ");
- spfValue = 1;
- }
- else if(newMenuItem.getName()=="SPF = 15"){
- lcd.print(" SPF = 15 ");
- spfValue = 15;
- }
- else if(newMenuItem.getName()=="SPF = 30"){
- lcd.print(" SPF = 30 ");
- spfValue = 30;
- }
- else if(newMenuItem.getName()=="SPF = 45"){
- lcd.print(" SPF = 45 ");
- spfValue = 45;
- }
- else if(newMenuItem.getName()=="SPF = 50"){
- lcd.print(" SPF = 50 ");
- spfValue = 50;
- }
- else if(newMenuItem.getName()=="SPF = 70"){
- lcd.print(" SPF = 70 ");
- spfValue = 70;
- }
- }
- //--------------------------------------//
- void menuUsed(MenuUseEvent used){
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print(" Set Your Timer ");
- lcd.setCursor(0,1);
- lcd.print(" for ");lcd.print(int(timerCalculation())); lcd.print(" mins ");
- delay(15000);
- menu.toRoot();
- }
- //--------------------------------------//
- void readButtons(){ //read buttons status
- int reading;
- int buttonEnterState=LOW; // the current reading from the Enter input pin
- int buttonEscState=LOW; // the current reading from the input pin
- int buttonLeftState=LOW; // the current reading from the input pin
- int buttonRightState=LOW; // the current reading from the input pin
- /*-----------------------------------------*/
- //ENTER button:
- reading = digitalRead(buttonPinEnter);
- if (reading != lastButtonEnterState) {
- lastEnterDebounceTime = millis();
- }
- if ((millis() - lastEnterDebounceTime) > debounceDelay) {
- buttonEnterState=reading;
- lastEnterDebounceTime=millis();
- }
- lastButtonEnterState = reading;
- //ESCAPE button:
- reading = digitalRead(buttonPinEsc);
- if (reading != lastButtonEscState) {
- lastEscDebounceTime = millis();
- }
- if ((millis() - lastEscDebounceTime) > debounceDelay) {
- buttonEscState = reading;
- lastEscDebounceTime=millis();
- }
- lastButtonEscState = reading;
- //RIGHT button:
- reading = digitalRead(buttonPinRight);
- if (reading != lastButtonRightState) {
- lastRightDebounceTime = millis();
- }
- if ((millis() - lastRightDebounceTime) > debounceDelay) {
- buttonRightState = reading;
- lastRightDebounceTime =millis();
- }
- lastButtonRightState = reading;
- //LEFT button:
- reading = digitalRead(buttonPinLeft);
- if (reading != lastButtonLeftState) {
- lastLeftDebounceTime = millis();
- }
- if ((millis() - lastLeftDebounceTime) > debounceDelay) {
- buttonLeftState = reading;
- lastLeftDebounceTime=millis();
- }
- lastButtonLeftState = reading;
- //Records which button has been pressed
- if (buttonEnterState==HIGH){
- lastButtonPushed=buttonPinEnter;
- }
- else if(buttonEscState==HIGH){
- lastButtonPushed=buttonPinEsc;
- }
- else if(buttonRightState==HIGH){
- lastButtonPushed=buttonPinRight;
- }
- else if(buttonLeftState==HIGH){
- lastButtonPushed=buttonPinLeft;
- }
- else{
- lastButtonPushed=0;
- }
- }
- void navigateMenus() {
- MenuItem currentMenu=menu.getCurrent();
- switch (lastButtonPushed){
- case buttonPinEnter:
- if(!(currentMenu.moveDown())){ //if the current menu has a child and ENTER has been pressed, then menu navigate to item below
- menu.use();
- }
- else{ //otherwise, if menu has no child and ENTER has been pressed, the current menu is used
- menu.moveDown();
- }
- break;
- case buttonPinEsc:
- menu.toRoot(); //back to main
- break;
- case buttonPinRight:
- menu.moveRight();
- break;
- case buttonPinLeft:
- menu.moveLeft();
- break;
- }
- lastButtonPushed=0; //reset the lastButtonPushed variable
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement