Advertisement
keb47

beach buddy - sunburn timer code

Aug 5th, 2014
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.76 KB | None | 0 0
  1. /*
  2. *************************************************
  3. * "BEACH BUDDY" SUNBURN TIMER CALCULATOR CODE   *
  4. *************************************************
  5. Katie Butler
  6. keb47@students.uwf.edu                                            
  7. Digital Multimedia
  8. Final Project
  9. Summer 2014
  10. ************************************************
  11. Includes code pulled from:
  12. http://www.coagula.org/content/pages/tutorial-manage-menu-and-lcd-display-arduino
  13.  
  14. Prior to compiling/uploading, install the MenuBackend library by Alexander Brevig:
  15. http://www.arduino.cc/playground/uploads/Profiles/MenuBackend_1-4.zip
  16. *************************************************/
  17.  
  18. #include <MenuBackend.h>      //MenuBackend library - copyright by Alexander Brevig
  19. #include <LiquidCrystal.h>    //this library is included in the Arduino IDE
  20.  
  21. const int buttonPinLeft = 8;       // pin for the Left button
  22. const int buttonPinRight = 9;      // pin for the Right button
  23. const int buttonPinEnter = 10;     // pin for the Enter button
  24. const int buttonPinEsc = 11;       // pin for the Esc button
  25.  
  26. int lastButtonPushed = 0;
  27.  
  28. float skintoneValue;
  29. float spfValue;
  30. float sensorValue;
  31. float uvValue;
  32. float timerValue;
  33.  
  34. float getUV(){
  35.   float sensorValue = analogRead(1);
  36.   if (sensorValue < 1){
  37.     return(1);
  38.   }
  39.   else {
  40.   return(sensorValue*0.071); //This is the calibrated slope
  41.   //return(10); //Uncomment to send a test value so that a meaningful calculation is possible
  42.   }
  43. }
  44.  
  45. float timerCalculation(){
  46.   return((skintoneValue/getUV())*spfValue);
  47.   //return((67/4)*15); //Uncomment to test that timer calculations are displaying properly on the LCD screen
  48. }
  49.  
  50. int lastButtonEnterState = LOW;     // the previous reading from the Enter input pin
  51. int lastButtonEscState = LOW;       // the previous reading from the Esc input pin
  52. int lastButtonLeftState = LOW;      // the previous reading from the Left input pin
  53. int lastButtonRightState = LOW;     // the previous reading from the Right input pin
  54.  
  55. long lastEnterDebounceTime = 0;    // the last time the output pin was toggled
  56. long lastEscDebounceTime = 0;      // the last time the output pin was toggled
  57. long lastLeftDebounceTime = 0;     // the last time the output pin was toggled
  58. long lastRightDebounceTime = 0;    // the last time the output pin was toggled
  59. long debounceDelay = 500;          // the debounce time
  60.  
  61. //LCD Setup:
  62. //Pin 1 = GND
  63. //Pin 2 = 5V
  64. //Pin 3 = Potentiometer wiper
  65. //Pin 4 = Arduino #2
  66. //Pin 5 = GND
  67. //Pin 6 = Arduino #3
  68. //Pins 7-10 = null
  69. //Pin 11 = Arduino #4
  70. //Pin 12 = Arduino #5
  71. //Pin 13 = Arduino #6
  72. //Pin 14 = Arduino #7
  73. //Pin 15 = 5V
  74. //Pin 16 = GND
  75.  
  76. LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
  77.  
  78. MenuBackend menu = MenuBackend(menuUsed,menuChanged);
  79. MenuItem menu1Item1 = MenuItem("Skin Tone");
  80.   MenuItem menuItem1SubItem1 = MenuItem("1 -- Fair");
  81.   MenuItem menuItem1SubItem2 = MenuItem("2 -- Light");
  82.   MenuItem menuItem1SubItem3 = MenuItem("3 -- Medium");
  83.   MenuItem menuItem1SubItem4 = MenuItem("4 -- Olive");
  84.   MenuItem menuItem1SubItem5 = MenuItem("5 -- Brown");
  85.   MenuItem menuItem1SubItem6 = MenuItem("6 -- Dark");
  86.     MenuItem menu1Item2 = MenuItem("Sun Protection");
  87.       MenuItem menuItem2SubItem1 = MenuItem("SPF = 0");
  88.       MenuItem menuItem2SubItem2 = MenuItem("SPF = 15");
  89.       MenuItem menuItem2SubItem3 = MenuItem("SPF = 30");
  90.       MenuItem menuItem2SubItem4 = MenuItem("SPF = 45");
  91.       MenuItem menuItem2SubItem5 = MenuItem("SPF = 50");
  92.       MenuItem menuItem2SubItem6 = MenuItem("SPF = 70");
  93.  
  94. void setup(){
  95.  
  96.   pinMode(buttonPinLeft, INPUT);
  97.   pinMode(buttonPinRight, INPUT);
  98.   pinMode(buttonPinEnter, INPUT);
  99.   pinMode(buttonPinEsc, INPUT);
  100.  
  101.   lcd.begin(16, 2);
  102.  
  103.   menu.getRoot().add(menu1Item1);          //add Skin Tone Menu
  104.   menu1Item1.add(menuItem1SubItem1)        //add Fair
  105.     .addRight(menuItem1SubItem2)           //add Light
  106.       .addRight(menuItem1SubItem3)         //add Medium
  107.         .addRight(menuItem1SubItem4)       //add Olive
  108.           .addRight(menuItem1SubItem5)     //add Brown
  109.             .addRight(menuItem1SubItem6);  //add Dark
  110.   menuItem1SubItem1.addAfter(menu1Item2);   // add SPF Menu
  111.   menuItem1SubItem2.addAfter(menu1Item2);   // add SPF Menu
  112.   menuItem1SubItem3.addAfter(menu1Item2);   // add SPF Menu
  113.   menuItem1SubItem4.addAfter(menu1Item2);   // add SPF Menu
  114.   menuItem1SubItem5.addAfter(menu1Item2);   // add SPF Menu
  115.   menuItem1SubItem6.addAfter(menu1Item2);   // add SPF Menu
  116.   menu1Item2.add(menuItem2SubItem1)
  117.     .addRight(menuItem2SubItem2)
  118.       .addRight(menuItem2SubItem3)
  119.         .addRight(menuItem2SubItem4)
  120.           .addRight(menuItem2SubItem5)
  121.             .addRight(menuItem2SubItem6);
  122.   menu.toRoot();
  123.   lcd.setCursor(0,0);  
  124.   lcd.print("    UV Timer    ");
  125.   delay(500);
  126. }
  127. //--------------------------------------//
  128.  
  129. void loop(){
  130.  
  131.   readButtons();
  132.   navigateMenus();
  133.  
  134. }
  135.  
  136. //--------------------------------------//
  137.  
  138. void menuChanged(MenuChangeEvent changed){
  139.  
  140.   MenuItem newMenuItem=changed.to;
  141.  
  142.   lcd.setCursor(0,1);
  143.  
  144.   if(newMenuItem.getName()==menu.getRoot()){
  145.     lcd.setCursor(0,0);
  146.     lcd.print("    UV Timer    ");
  147.     lcd.setCursor(0,1);
  148.     lcd.print(" Calculate Time ");  
  149.   }
  150.   else if(newMenuItem.getName()=="1 -- Fair"){
  151.     lcd.clear();
  152.     lcd.setCursor(0,0);
  153.     lcd.print("Select Skin Tone");
  154.     lcd.setCursor(0,1);
  155.     lcd.print("   1 --- Fair   ");
  156.     skintoneValue = 67;
  157.   }
  158.   else if(newMenuItem.getName()=="2 -- Light"){
  159.     lcd.print("   2 -- Light   ");
  160.     skintoneValue = 100;
  161.   }
  162.   else if(newMenuItem.getName()=="3 -- Medium"){
  163.     lcd.print("  3 --- Medium  ");
  164.     skintoneValue = 200;
  165.   }
  166.   else if(newMenuItem.getName()=="4 -- Olive"){
  167.     lcd.print("   4 -- Olive   ");
  168.    skintoneValue = 300;
  169.   }
  170.   else if(newMenuItem.getName()=="5 -- Brown"){
  171.     lcd.print("   5 -- Brown   ");
  172.     skintoneValue = 400;
  173.   }
  174.   else if(newMenuItem.getName()=="6 -- Dark"){
  175.     lcd.print("   6 --- Dark   ");
  176.     skintoneValue = 500;
  177.   }
  178.   else if(newMenuItem.getName()=="SPF = 0"){
  179.     lcd.clear();
  180.     lcd.setCursor(0,0);
  181.     lcd.print("   Select SPF   ");
  182.     lcd.setCursor(0,1);
  183.     lcd.print("    SPF = 0     ");
  184.     spfValue = 1;
  185.   }
  186.   else if(newMenuItem.getName()=="SPF = 15"){
  187.     lcd.print("    SPF = 15    ");
  188.     spfValue = 15;
  189.   }
  190.   else if(newMenuItem.getName()=="SPF = 30"){
  191.     lcd.print("    SPF = 30    ");
  192.     spfValue = 30;
  193.   }
  194.   else if(newMenuItem.getName()=="SPF = 45"){
  195.     lcd.print("    SPF = 45    ");
  196.     spfValue = 45;
  197.   }
  198.   else if(newMenuItem.getName()=="SPF = 50"){
  199.     lcd.print("    SPF = 50    ");
  200.     spfValue = 50;
  201.   }
  202.   else if(newMenuItem.getName()=="SPF = 70"){
  203.     lcd.print("    SPF = 70    ");
  204.     spfValue = 70;
  205.   }
  206. }
  207.  
  208. //--------------------------------------//
  209.  
  210. void menuUsed(MenuUseEvent used){
  211.   lcd.clear();
  212.   lcd.setCursor(0,0);
  213.   lcd.print(" Set Your Timer ");
  214.   lcd.setCursor(0,1);
  215.   lcd.print(" for ");lcd.print(int(timerCalculation())); lcd.print(" mins ");
  216.   delay(15000);
  217.   menu.toRoot();
  218. }
  219.  
  220. //--------------------------------------//
  221.  
  222. void  readButtons(){  //read buttons status
  223.   int reading;
  224.   int buttonEnterState=LOW;             // the current reading from the Enter input pin
  225.   int buttonEscState=LOW;             // the current reading from the input pin
  226.   int buttonLeftState=LOW;             // the current reading from the input pin
  227.   int buttonRightState=LOW;             // the current reading from the input pin
  228.  
  229. /*-----------------------------------------*/
  230. //ENTER button:
  231.   reading = digitalRead(buttonPinEnter);
  232.   if (reading != lastButtonEnterState) {
  233.     lastEnterDebounceTime = millis();
  234.   }
  235.   if ((millis() - lastEnterDebounceTime) > debounceDelay) {
  236.     buttonEnterState=reading;
  237.     lastEnterDebounceTime=millis();
  238.   }
  239.   lastButtonEnterState = reading;
  240.  
  241. //ESCAPE button:
  242.   reading = digitalRead(buttonPinEsc);
  243.   if (reading != lastButtonEscState) {
  244.     lastEscDebounceTime = millis();
  245.   }
  246.   if ((millis() - lastEscDebounceTime) > debounceDelay) {
  247.     buttonEscState = reading;
  248.     lastEscDebounceTime=millis();
  249.   }
  250.   lastButtonEscState = reading;
  251.  
  252. //RIGHT button:
  253.   reading = digitalRead(buttonPinRight);
  254.   if (reading != lastButtonRightState) {
  255.     lastRightDebounceTime = millis();
  256.   }
  257.   if ((millis() - lastRightDebounceTime) > debounceDelay) {
  258.     buttonRightState = reading;
  259.     lastRightDebounceTime =millis();
  260.   }
  261.   lastButtonRightState = reading;                  
  262.  
  263.  
  264. //LEFT button:              
  265.   reading = digitalRead(buttonPinLeft);
  266.   if (reading != lastButtonLeftState) {
  267.     lastLeftDebounceTime = millis();
  268.   }
  269.   if ((millis() - lastLeftDebounceTime) > debounceDelay) {
  270.     buttonLeftState = reading;
  271.     lastLeftDebounceTime=millis();
  272.   }
  273.   lastButtonLeftState = reading;  
  274.  
  275. //Records which button has been pressed
  276.   if (buttonEnterState==HIGH){
  277.     lastButtonPushed=buttonPinEnter;
  278.   }
  279.   else if(buttonEscState==HIGH){
  280.     lastButtonPushed=buttonPinEsc;
  281.   }
  282.   else if(buttonRightState==HIGH){
  283.     lastButtonPushed=buttonPinRight;
  284.   }
  285.   else if(buttonLeftState==HIGH){
  286.     lastButtonPushed=buttonPinLeft;
  287.   }
  288.   else{
  289.     lastButtonPushed=0;
  290.   }                  
  291. }
  292.  
  293. void navigateMenus() {
  294.   MenuItem currentMenu=menu.getCurrent();
  295.  
  296.   switch (lastButtonPushed){
  297.   case buttonPinEnter:
  298.     if(!(currentMenu.moveDown())){  //if the current menu has a child and ENTER has been pressed, then menu navigate to item below
  299.       menu.use();
  300.     }
  301.     else{  //otherwise, if menu has no child and ENTER has been pressed, the current menu is used
  302.       menu.moveDown();
  303.     }
  304.     break;
  305.   case buttonPinEsc:
  306.     menu.toRoot();  //back to main
  307.     break;
  308.   case buttonPinRight:
  309.     menu.moveRight();
  310.     break;      
  311.   case buttonPinLeft:
  312.     menu.moveLeft();
  313.     break;      
  314.   }
  315.  
  316.   lastButtonPushed=0; //reset the lastButtonPushed variable
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement