Advertisement
ClarkeRubber

Arduino Wave Generator - V5.2

Oct 1st, 2012
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 39.09 KB | None | 0 0
  1. #include <LiquidCrystal.h>   // include LCD library
  2. /*--------------------------------------------------------------------------------------
  3.  Defines
  4.  --------------------------------------------------------------------------------------*/
  5. // Pins in use
  6. #define BUTTON_ADC_PIN           A0  // A0 is the button ADC input
  7. #define LCD_BACKLIGHT_PIN         3  // D3 controls LCD backlight
  8. // ADC readings expected for the 5 buttons on the ADC input
  9. #define RIGHT_10BIT_ADC           0  // right
  10. #define UP_10BIT_ADC            145  // up
  11. #define DOWN_10BIT_ADC          329  // down
  12. #define LEFT_10BIT_ADC          505  // left
  13. #define SELECT_10BIT_ADC        741  // right
  14. #define BUTTONHYSTERESIS         10  // hysteresis for valid button sensing window
  15. //return values for ReadButtons()
  16. #define BUTTON_NONE               0  //
  17. #define BUTTON_RIGHT              1  //
  18. #define BUTTON_UP                 2  //
  19. #define BUTTON_DOWN               3  //
  20. #define BUTTON_LEFT               4  //
  21. #define BUTTON_SELECT             5  //
  22. //some example macros with friendly labels for LCD backlight/pin control, tested and can be swapped into the example code as you like
  23. #define LCD_BACKLIGHT_OFF()     digitalWrite( LCD_BACKLIGHT_PIN, LOW )
  24. #define LCD_BACKLIGHT_ON()      digitalWrite( LCD_BACKLIGHT_PIN, HIGH )
  25. #define LCD_BACKLIGHT(state)    { if( state ){digitalWrite( LCD_BACKLIGHT_PIN, HIGH );}else{digitalWrite( LCD_BACKLIGHT_PIN, LOW );} }
  26. /*--------------------------------------------------------------------------------------
  27.  Variables
  28.  --------------------------------------------------------------------------------------*/
  29. byte buttonJustPressed  = false;         //this will be true after a ReadButtons() call if triggered
  30. byte buttonJustReleased = false;         //this will be true after a ReadButtons() call if triggered
  31. byte buttonWas          = BUTTON_NONE;   //used by ReadButtons() for detection of button events
  32. byte button;
  33.  
  34. byte char_arrow_left[8] = {
  35.   B00011,
  36.   B00111,
  37.   B01111,
  38.   B11111,
  39.   B11111,
  40.   B01111,
  41.   B00111,
  42.   B00011};
  43.  
  44. byte char_arrow_right[8] = {
  45.   B11000,
  46.   B11100,
  47.   B11110,
  48.   B11111,
  49.   B11111,
  50.   B11110,
  51.   B11100,
  52.   B11000};
  53.  
  54. int vibrator_pin = 10;
  55.  
  56. int button_wait_time_short = 60;
  57. int button_wait_time_long = 250;
  58.  
  59. String start = "-----START!-----";
  60. String blank_start = "-----      -----";
  61.  
  62. const float pi = 3.14159;
  63.  
  64. unsigned long time;
  65.  
  66. int prev_time, flasher, prev_flash;
  67.  
  68. LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );   //Pins for the freetronics 16x2 LCD shield. LCD: ( RS, E, LCD-D4, LCD-D5, LCD-D6, LCD-D7 )
  69.  
  70. /*---------Main Menu---------*/
  71. int header_main = 0;
  72. int prev_header_main;
  73.  
  74. char* menu_main[] = {
  75.   "Constant", "Wave", "Wave Of Waves", "Random"};
  76. int count_items_main = 4;
  77. /*---------------------------*/
  78.  
  79. /*-------Constant Menu-------*/
  80. int constant_level = 255;
  81.  
  82. int header_constant = 0;
  83.  
  84. char* menu_constant[] = {
  85.   "Level:"};
  86.  
  87. int count_items_constant = 2;
  88. /*---------------------------*/
  89.  
  90. /*---------Wave Menu---------*/
  91. int header_wave = 0;
  92.  
  93. int wave_type = 0;
  94.  
  95. long wave_minimum = 0;
  96.  
  97. long wave_maximum = 255;
  98.  
  99. long wave_frequency = 4; //4 sec, just a preference
  100. const long wave_max_frequency = 9999; //9999 sec = 170mins
  101.  
  102. char* menu_wave[] = {
  103.   "Type:", "MinLvl:", "MaxLvl:", "Period:"};
  104. int count_items_wave = 5;
  105.  
  106. char* type_wave[] = {
  107.   "Cosine", "Sawtooth", "Triangle", "Square"};
  108. int count_items_type_wave = 4;
  109. /*---------------------------*/
  110.  
  111. /*-------Wave of Waves-------*/
  112. int header_wave_waves = 0;
  113.  
  114. long wave_waves_min = 140;
  115.  
  116. long wave_waves_max = 255;
  117.  
  118. long wave_waves_multiplier = 10; //relative multiplier on the amount of cycles (there will always be an infinite amount, this just changes the rate.)
  119. const long wave_waves_multiplier_max = 999;
  120.  
  121. long wave_waves_wavelength = 180; //seconds
  122. const long wave_waves_wavelength_max = 9999; //17 mins
  123.  
  124. char* menu_wave_waves[] = {
  125.   "MinLvl:", "MaxLvl:", "Multix:", "Period:"};
  126. int count_items_wave_waves = 5;
  127. /*---------------------------*/
  128.  
  129. /*---------Rand Menu---------*/
  130. int header_random = 0;
  131.  
  132. long random_minimum = 0;
  133. long random_maximum = 255;
  134.  
  135. long random_min_freq = 1; //1 second
  136. long random_max_freq = 20; //20 seconds
  137.  
  138. const long random_max_freq_max = 9999; //1000 seconds. Roughly 17mins, 1000 so it doesn't overflow the screen
  139.  
  140. long random_time_min = 1;
  141. long random_time_max = 120; //120 seconds = 2min
  142.  
  143. const long random_time_max_max = 9999; //1000sec
  144.  
  145. unsigned long prev_random_time_max;
  146.  
  147. char* menu_random[] = {
  148.   "MinLvl:", "MaxLvl:", "MinTime:", "MaxTime:", "MinPeriod:", "MaxPeriod:"};
  149. int count_items_random = 7;
  150. /*---------------------------*/
  151.  
  152. void arrow_print(char* string_one, String string_two){
  153.   if(flash()){
  154.     lcd.print(String(string_one));
  155.     lcd.write(1);
  156.     lcd.print(String(string_two));
  157.     lcd.write(2);
  158.   }else{
  159.     lcd.print(String(string_one) + String(" ") + String(string_two));
  160.   }
  161. }
  162.  
  163. void start_print(){
  164.   if(flash()){
  165.     lcd.print(String(start));
  166.   }
  167.   else{
  168.     lcd.print(String(blank_start));
  169.   }
  170. }
  171.  
  172. int flash(){
  173.   int timestamp;
  174.   timestamp = ( (millis() / 500) % 100 );
  175.   if(timestamp != prev_time){
  176.     if(flasher == 1){
  177.       flasher = 0;
  178.     }
  179.     else{
  180.       flasher = 1;
  181.     }
  182.   }
  183.   prev_time = timestamp;
  184.   return flasher;
  185. }
  186.  
  187. void display_menu_constant(){
  188.   delay(button_wait_time_long);
  189.   boolean update = true;
  190.  
  191.   byte constant_button;
  192.  
  193.   do{
  194.     constant_button = ReadButtons();
  195.     if(update || flash() != prev_flash){
  196.       for(int x = 0; x < 2; x++){
  197.         if(header_constant + x >= count_items_constant){
  198.           lcd.setCursor(0, x);
  199.           lcd.print(String("                "));
  200.         }
  201.         else{
  202.           lcd.setCursor(0, x);
  203.           lcd.print(String("                "));
  204.           lcd.setCursor(0, x);
  205.           if(header_constant + x == 0){
  206.             if(header_constant == 0){
  207.               arrow_print(menu_constant[0], String(constant_level));
  208.             }else{
  209.               lcd.print(String(menu_constant[0]) + String(" ") + String(constant_level));
  210.             }
  211.           }
  212.           else if(header_constant + x == 1){
  213.             if(header_constant == 1){
  214.               start_print();
  215.             }
  216.             else{
  217.               lcd.print(String(start));
  218.             }
  219.           }
  220.         }
  221.       }
  222.       prev_flash = flash();
  223.       update = false;
  224.     }
  225.  
  226.     switch( constant_button )
  227.     {
  228.     case BUTTON_NONE:
  229.       {
  230.         break;
  231.       }
  232.     case BUTTON_RIGHT:
  233.       {
  234.         //Increment Selection
  235.         if(header_constant == 0 && constant_level + 1 <= 255){
  236.           constant_level++;
  237.           delay(button_wait_time_short);
  238.           update = true;
  239.         }
  240.         break;
  241.       }
  242.     case BUTTON_UP:
  243.       {
  244.         if(header_constant-1 >= 0){
  245.           header_constant -= 1;
  246.           update = true;
  247.         }
  248.         delay(button_wait_time_long);
  249.         break;
  250.       }
  251.     case BUTTON_DOWN:
  252.       {
  253.         if(header_constant+1 < count_items_constant){
  254.           header_constant++;
  255.           update = true;
  256.         }
  257.         delay(button_wait_time_long);
  258.         break;
  259.       }
  260.     case BUTTON_LEFT:
  261.       {
  262.         //Decrement Selection
  263.         if(header_constant == 0 && constant_level - 1 >= 0){
  264.           constant_level -= 1;
  265.           delay(button_wait_time_short);
  266.           update = true;
  267.         }
  268.         break;
  269.       }
  270.     case BUTTON_SELECT:
  271.       {  
  272.         if(header_constant == count_items_constant - 1){
  273.           delay(250);
  274.           vibrator_constant(constant_level);
  275.           update = true;
  276.           delay(button_wait_time_long);
  277.         }
  278.         break;
  279.       }
  280.     default:
  281.       {
  282.         break;
  283.       }
  284.     }
  285.   }
  286.   while(!(constant_button == BUTTON_SELECT && header_constant != count_items_constant - 1));
  287.   return;
  288. }
  289.  
  290. void vibrator_constant(int constant_level_output){
  291.   byte vibrator_constant_button;
  292.   lcd.setCursor(0,0);
  293.   lcd.print(String("                "));
  294.   analogWrite(vibrator_pin, constant_level_output);
  295.   do{
  296.     vibrator_constant_button = ReadButtons();
  297.     lcd.setCursor(0,0);
  298.     lcd.print(String("CONSTANT @ ") + String(constant_level_output));
  299.   }
  300.   while(vibrator_constant_button != BUTTON_SELECT);
  301.   analogWrite(vibrator_pin, 0);
  302.   return;
  303. }
  304.  
  305. void display_menu_wave(){
  306.   delay(button_wait_time_long);
  307.   byte wave_button;
  308.  
  309.   boolean update = true;
  310.   do{
  311.     wave_button = ReadButtons();
  312.  
  313.     if(update || flash() != prev_flash){
  314.       for(int x = 0; x < 2; x++){
  315.         if(header_wave + x >= count_items_wave){
  316.           lcd.setCursor(0, x);
  317.           lcd.print(String("                "));
  318.         }
  319.         else{
  320.           lcd.setCursor(0, x);
  321.           lcd.print(String("                "));
  322.           lcd.setCursor(0, x);
  323.           if(header_wave + x == 0){
  324.             if(header_wave == 0){
  325.               arrow_print(menu_wave[0], type_wave[wave_type]);
  326.             }else{
  327.               lcd.print(String(menu_wave[0]) + String(" ") + String(type_wave[wave_type]));
  328.             }
  329.           }
  330.           else if(header_wave + x == 1){
  331.             if(header_wave == 1){
  332.               arrow_print(menu_wave[1], String(wave_minimum));
  333.             }else{
  334.               lcd.print(String(menu_wave[1]) + String(" ") + String(wave_minimum));
  335.             }
  336.           }
  337.           else if(header_wave + x == 2){
  338.             if(header_wave == 2){
  339.               arrow_print(menu_wave[2], String(wave_maximum));
  340.             }else{
  341.               lcd.print(String(menu_wave[2]) + String(" ") + String(wave_maximum));
  342.             }
  343.           }
  344.           else if(header_wave + x == 3){
  345.             if(header_wave == 3){
  346.               arrow_print(menu_wave[3], String(wave_frequency));
  347.             }else{
  348.               lcd.print(String(menu_wave[3]) + String(" ") + String(wave_frequency));
  349.             }
  350.           }
  351.           else if(header_wave + x == 4){
  352.             if(header_wave == 4){
  353.               start_print();
  354.             }
  355.             else{
  356.               lcd.print(String(start));
  357.             }
  358.           }
  359.         }
  360.       }
  361.       prev_flash = flash();
  362.       update = false;
  363.     }
  364.  
  365.     switch( wave_button )
  366.     {
  367.     case BUTTON_NONE:
  368.       {
  369.         break;
  370.       }
  371.     case BUTTON_RIGHT:
  372.       {
  373.         //Increment Selection
  374.         if(header_wave == 0 && wave_type + 1 <= 3){
  375.           wave_type++;
  376.           delay(button_wait_time_long);
  377.           update = true;
  378.         }
  379.         else if(header_wave == 1 && wave_minimum + 1 < 256 && wave_minimum < wave_maximum){
  380.           wave_minimum++;
  381.           delay(button_wait_time_short);
  382.           update = true;
  383.         }
  384.         else if(header_wave == 2 && wave_maximum + 1 < 256){
  385.           wave_maximum++;
  386.           delay(button_wait_time_short);
  387.           update = true;
  388.         }
  389.         else if(header_wave == 3 && wave_frequency + 1 <= wave_max_frequency){
  390.           wave_frequency++;
  391.           delay(button_wait_time_short);
  392.           update = true;
  393.         }
  394.         break;
  395.       }
  396.     case BUTTON_UP:
  397.       {
  398.         if(header_wave - 1 >= 0){
  399.           header_wave -= 1;
  400.           update = true;
  401.         }
  402.         delay(button_wait_time_long);
  403.         break;
  404.       }
  405.     case BUTTON_DOWN:
  406.       {
  407.         if(header_wave + 1 < count_items_wave){
  408.           header_wave++;
  409.           update = true;
  410.         }
  411.         delay(button_wait_time_long);
  412.         break;
  413.       }
  414.     case BUTTON_LEFT:
  415.       {
  416.         //Decrement Selection
  417.         if(header_wave == 0 && wave_type > 0){
  418.           wave_type -= 1;
  419.           delay(button_wait_time_long);
  420.           update = true;
  421.         }
  422.         else if(header_wave == 1 && wave_minimum > 0){
  423.           wave_minimum -= 1;
  424.           delay(button_wait_time_short);
  425.           update = true;
  426.         }
  427.         else if(header_wave == 2 && wave_maximum > 0 && wave_maximum > wave_minimum){
  428.           wave_maximum -= 1;
  429.           delay(button_wait_time_short);
  430.           update = true;
  431.         }
  432.         else if(header_wave == 3 && wave_frequency > 1){
  433.           wave_frequency -= 1;
  434.           delay(button_wait_time_short);
  435.           update = true;
  436.         }
  437.         break;
  438.       }
  439.     case BUTTON_SELECT:
  440.       {  
  441.         if(header_wave == count_items_wave - 1){
  442.           delay(button_wait_time_long);
  443.           vibrator_wave(wave_type, wave_minimum, wave_maximum, wave_frequency);
  444.           update = true;
  445.           delay(button_wait_time_long);
  446.         }
  447.         break;
  448.       }
  449.     default:
  450.       {
  451.         break;
  452.       }
  453.     }
  454.   }
  455.   while(!(wave_button == BUTTON_SELECT && header_wave != count_items_wave - 1));
  456.   return;
  457. }
  458.  
  459.  
  460. void vibrator_wave(int vibrator_wave_type, long vibrator_wave_minimum, long vibrator_wave_maximum, long vibrator_wave_frequency){
  461.   byte vibrator_wave_button;
  462.   long vibrator_wave_output;
  463.   long vibrator_wave_temp;
  464.   String vibrator_display_output;
  465.  
  466.   lcd.setCursor(0,0);
  467.   lcd.print(String("                "));
  468.   lcd.setCursor(0,1);
  469.   lcd.print(String("                "));
  470.  
  471.   if(vibrator_wave_type == 0){
  472.  
  473.     //sine
  474.     lcd.setCursor(0,0);
  475.     lcd.print(String("COSINE: ") + String(vibrator_wave_minimum) + String("-") + String(vibrator_wave_maximum));
  476.     lcd.setCursor(0,1);
  477.     lcd.print(String("P: ") + String(vibrator_wave_frequency));
  478.     do{
  479.       vibrator_wave_button = ReadButtons();
  480.       time = millis();
  481.       vibrator_wave_output = vibrator_wave_minimum + (vibrator_wave_maximum + 1 - vibrator_wave_minimum) * ( (1 + sin(2 * pi * ( time % (vibrator_wave_frequency * 1000) ) / ( vibrator_wave_frequency * 1000 ) ) ) / 2);
  482.  
  483.       lcd.setCursor(8,1);
  484.       lcd.print(String("LVL: ") + String(vibrator_wave_output) + String("  "));
  485.  
  486.       analogWrite(vibrator_pin, vibrator_wave_output);
  487.     }
  488.     while(vibrator_wave_button != BUTTON_SELECT);
  489.     analogWrite(vibrator_pin, 0);
  490.  
  491.   }
  492.   else if(vibrator_wave_type == 1){
  493.  
  494.     //sawtooth
  495.     lcd.setCursor(0,0);
  496.     lcd.print(String("SAWTOOTH:") + String(vibrator_wave_minimum) + String("-") + String(vibrator_wave_maximum));
  497.     lcd.setCursor(0,1);
  498.     lcd.print(String("P: ") + String(vibrator_wave_frequency));
  499.     do{
  500.       vibrator_wave_button = ReadButtons();
  501.       time = millis();
  502.  
  503.       vibrator_wave_output = vibrator_wave_minimum + (vibrator_wave_maximum - vibrator_wave_minimum) * ( time % (vibrator_wave_frequency * 1000) ) / ( vibrator_wave_frequency * 1000 );
  504.  
  505.       lcd.setCursor(8,1);
  506.       lcd.print(String("LVL: ") + String(vibrator_wave_output) + String("  "));
  507.  
  508.       analogWrite(vibrator_pin, vibrator_wave_output);
  509.     }
  510.     while(vibrator_wave_button != BUTTON_SELECT);
  511.     analogWrite(vibrator_pin, 0);
  512.  
  513.   }
  514.   else if(vibrator_wave_type == 2){
  515.  
  516.     //triangle
  517.     lcd.setCursor(0,0);
  518.     lcd.print(String("TRIANGLE:") + String(vibrator_wave_minimum) + String("-") + String(vibrator_wave_maximum));
  519.     lcd.setCursor(0,1);
  520.     lcd.print(String("P: ") + String(vibrator_wave_frequency));
  521.     do{
  522.       vibrator_wave_button = ReadButtons();
  523.       time = millis();
  524.  
  525.       vibrator_wave_temp = ( vibrator_wave_maximum - vibrator_wave_minimum ) - 2 * ( vibrator_wave_maximum - vibrator_wave_minimum ) * ( time % (vibrator_wave_frequency * 1000) ) / ( vibrator_wave_frequency * 1000 );
  526.       vibrator_wave_output = vibrator_wave_maximum - abs( vibrator_wave_temp );
  527.  
  528.       lcd.setCursor(8,1);
  529.       lcd.print(String("LVL: ") + String(vibrator_wave_output) + String("  "));
  530.  
  531.       analogWrite(vibrator_pin, vibrator_wave_output);
  532.     }
  533.     while(vibrator_wave_button != BUTTON_SELECT);
  534.     analogWrite(vibrator_pin, 0);
  535.  
  536.   }
  537.   else if(vibrator_wave_type == 3){
  538.  
  539.     //square
  540.     lcd.setCursor(0,0);
  541.     lcd.print(String("SQUARE: ") + String(vibrator_wave_minimum) + String("-") + String(vibrator_wave_maximum));
  542.     lcd.setCursor(0,1);
  543.     lcd.print(String("P: ") + String(vibrator_wave_frequency));
  544.     do{
  545.       vibrator_wave_button = ReadButtons();
  546.       time = millis();
  547.  
  548.       if(time % ( vibrator_wave_frequency * 1000 ) < vibrator_wave_frequency * 500 ){
  549.         vibrator_wave_output = vibrator_wave_maximum;
  550.       }
  551.       else{
  552.         vibrator_wave_output = vibrator_wave_minimum;
  553.       }
  554.  
  555.       lcd.setCursor(8,1);
  556.       lcd.print(String("LVL: ") + String(vibrator_wave_output) + String("  "));
  557.  
  558.       analogWrite(vibrator_pin, vibrator_wave_output);
  559.     }
  560.     while(vibrator_wave_button != BUTTON_SELECT);
  561.     analogWrite(vibrator_pin, 0);
  562.  
  563.   }
  564.   return;
  565. }
  566.  
  567. void display_menu_wave_waves(){
  568.   delay(250);
  569.  
  570.   byte wave_waves_button;
  571.  
  572.   boolean update = true;
  573.  
  574.   do{
  575.     wave_waves_button = ReadButtons();
  576.  
  577.     if(update || flash() != prev_flash
  578.         ){
  579.       for(int x = 0; x < 2; x++){
  580.         if(header_wave_waves + x >= count_items_wave_waves){
  581.           lcd.setCursor(0, x);
  582.           lcd.print(String("                "));
  583.         }
  584.         else{
  585.           lcd.setCursor(0, x);
  586.           lcd.print(String("                "));
  587.           lcd.setCursor(0, x);
  588.           if(header_wave_waves + x == 0){
  589.             if(header_wave_waves == 0){
  590.               arrow_print(menu_wave_waves[0], String(wave_waves_min));
  591.             }else{
  592.               lcd.print(String(menu_wave_waves[0]) + String(" ") + String(wave_waves_min));
  593.             }
  594.           }
  595.           else if(header_wave_waves + x == 1){
  596.             if(header_wave_waves == 1){
  597.               arrow_print(menu_wave_waves[1], String(wave_waves_max));
  598.             }else{
  599.               lcd.print(String(menu_wave_waves[1]) + String(" ") + String(wave_waves_max));
  600.             }
  601.           }
  602.           else if(header_wave_waves + x == 2){
  603.             if(header_wave_waves == 2){
  604.               arrow_print(menu_wave_waves[2], String(wave_waves_multiplier));
  605.             }else{
  606.               lcd.print(String(menu_wave_waves[2]) + String(" ") + String(wave_waves_multiplier));
  607.             }
  608.           }
  609.           else if(header_wave_waves + x == 3){
  610.             if(header_wave_waves == 3){
  611.               arrow_print(menu_wave_waves[3], String(wave_waves_wavelength));
  612.             }else{
  613.               lcd.print(String(menu_wave_waves[3]) + String(" ") + String(wave_waves_wavelength));
  614.             }
  615.           }
  616.           else if(header_wave_waves + x == 4){
  617.             if(header_wave_waves == 4){
  618.               start_print();
  619.             }
  620.             else{
  621.               lcd.print(String(start));
  622.             }
  623.           }
  624.         }
  625.       }
  626.       prev_flash = flash();
  627.       update = false;
  628.     }
  629.  
  630.     switch( wave_waves_button )
  631.     {
  632.     case BUTTON_NONE:
  633.       {
  634.         break;
  635.       }
  636.     case BUTTON_RIGHT:
  637.       {
  638.         if(header_wave_waves == 0 && wave_waves_min + 1 < wave_waves_max && wave_waves_min + 1 <= 255){
  639.           wave_waves_min++;
  640.           delay(button_wait_time_short);
  641.           update = true;
  642.         }
  643.         else if(header_wave_waves == 1 && wave_waves_max + 1 <= 255){
  644.           wave_waves_max++;
  645.           delay(button_wait_time_short);
  646.           update = true;
  647.         }
  648.         else if(header_wave_waves == 2 && wave_waves_multiplier + 1 <= wave_waves_multiplier_max){
  649.           wave_waves_multiplier++;
  650.           delay(button_wait_time_short);
  651.           update = true;
  652.         }
  653.         else if(header_wave_waves == 3 && wave_waves_wavelength + 1 <= wave_waves_wavelength_max){
  654.           wave_waves_wavelength++;
  655.           delay(button_wait_time_short);
  656.           update = true;
  657.         }
  658.         break;
  659.       }
  660.     case BUTTON_UP:
  661.       {
  662.         if(header_wave_waves - 1 >= 0){
  663.           header_wave_waves -= 1;
  664.           update = true;
  665.         }
  666.         delay(button_wait_time_long);
  667.         break;
  668.       }
  669.     case BUTTON_DOWN:
  670.       {
  671.         if(header_wave_waves + 1 < count_items_wave_waves){
  672.           header_wave_waves += 1;
  673.           update = true;
  674.         }
  675.         delay(button_wait_time_long);
  676.         break;
  677.       }
  678.     case BUTTON_LEFT:
  679.       {
  680.         if(header_wave_waves == 0 && wave_waves_min - 1 >= 0){
  681.           wave_waves_min -= 1;
  682.           delay(button_wait_time_short);
  683.           update = true;
  684.         }
  685.         else if(header_wave_waves == 1 && wave_waves_max - 1 > 0 && wave_waves_max - 1 > wave_waves_min){
  686.           wave_waves_max -= 1;
  687.           delay(button_wait_time_short);
  688.           update = true;
  689.         }
  690.         else if(header_wave_waves == 2 && wave_waves_multiplier - 1 > 0){
  691.           wave_waves_multiplier -= 1;
  692.           delay(button_wait_time_short);
  693.           update = true;
  694.         }
  695.         else if(header_wave_waves == 3 && wave_waves_wavelength - 1 > 0){
  696.           wave_waves_wavelength -= 1;
  697.           delay(button_wait_time_short);
  698.           update = true;
  699.         }
  700.         break;
  701.       }
  702.     case BUTTON_SELECT:
  703.       {  
  704.         if(header_wave_waves == 4){
  705.           delay(button_wait_time_long);
  706.           vibrate_wave_waves(wave_waves_min, wave_waves_max, wave_waves_multiplier, wave_waves_wavelength);
  707.           update = true;
  708.           delay(button_wait_time_long);
  709.         }
  710.         break;
  711.       }
  712.     default:
  713.       {
  714.         break;
  715.       }
  716.     }
  717.   }while(!(wave_waves_button == BUTTON_SELECT && header_wave_waves != count_items_wave_waves - 1));
  718.   return;
  719. }
  720.  
  721. void vibrate_wave_waves(long vibrate_wave_waves_min, long vibrate_wave_waves_max, long vibrate_wave_waves_multiplier, long vibrate_wave_waves_wavelength){
  722.   byte vibrator_wave_waves_button;
  723.   long vibrate_wave_waves_output;
  724.   lcd.setCursor(0,0);
  725.   lcd.print(String("                "));
  726.   lcd.setCursor(0,1);
  727.   lcd.print(String("                "));
  728.  
  729.   lcd.setCursor(0,0);
  730.   lcd.print(String("WW:") + String(vibrate_wave_waves_min) + String("-") + String(vibrate_wave_waves_max));
  731.   lcd.setCursor(11,0);
  732.   lcd.print(String("V:"));
  733.   lcd.setCursor(0,1);
  734.   lcd.print(String("P:") + String(vibrate_wave_waves_wavelength));
  735.   lcd.setCursor(10,1);
  736.   lcd.print(String("MP:") + String(vibrate_wave_waves_multiplier));
  737.  
  738.   do{
  739.     time = millis();
  740.     vibrator_wave_waves_button = ReadButtons();
  741.    
  742.     vibrate_wave_waves_output = vibrate_wave_waves_min + (vibrate_wave_waves_max + 1 - vibrate_wave_waves_min) * ( ( 1 + cos( vibrate_wave_waves_multiplier * tan(2 * pi * ( time % ( vibrate_wave_waves_wavelength * 1000 ) ) / ( vibrate_wave_waves_wavelength * 1000 ) ) ) ) / 2 );
  743.     lcd.setCursor(13,0);
  744.     lcd.print(String(vibrate_wave_waves_output) + String("  "));
  745.    
  746.     analogWrite(vibrator_pin, vibrate_wave_waves_output);
  747.   }
  748.   while(vibrator_wave_waves_button != BUTTON_SELECT);
  749.   analogWrite(vibrator_pin, 0);
  750.   return;
  751. }
  752.  
  753. void display_menu_random(){
  754.   delay(250);
  755.  
  756.   byte random_button;
  757.  
  758.   boolean update = true;
  759.  
  760.   do{
  761.     random_button = ReadButtons();
  762.  
  763.     if(update || flash() != prev_flash){
  764.       for(int x = 0; x < 2; x++){
  765.         if(header_random + x >= count_items_random){
  766.           lcd.setCursor(0, x);
  767.           lcd.print(String("                "));
  768.         }
  769.         else{
  770.           lcd.setCursor(0, x);
  771.           lcd.print(String("                "));
  772.           lcd.setCursor(0, x);
  773.           if(header_random + x == 0){
  774.             if(header_random == 0){
  775.               arrow_print(menu_random[0], String(random_minimum));
  776.             }else{
  777.               lcd.print(String(menu_random[0]) + String(" ") + String(random_minimum));
  778.             }
  779.           }
  780.           else if(header_random + x == 1){
  781.             if(header_random == 1){
  782.               arrow_print(menu_random[1], String(random_maximum));
  783.             }else{
  784.               lcd.print(String(menu_random[1]) + String(" ") + String(random_maximum));
  785.             }
  786.           }
  787.           else if(header_random + x == 2){
  788.             if(header_random == 2){
  789.               arrow_print(menu_random[2], String(random_time_min));
  790.             }else{
  791.               lcd.print(String(menu_random[2]) + String(" ") + String(random_time_min));
  792.             }
  793.           }
  794.           else if(header_random + x == 3){
  795.             if(header_random == 3){
  796.               arrow_print(menu_random[3], String(random_time_max));
  797.             }else{
  798.               lcd.print(String(menu_random[3]) + String(" ") + String(random_time_max));
  799.             }
  800.           }
  801.           else if(header_random + x == 4){
  802.             if(header_random == 4){
  803.               arrow_print(menu_random[4], String(random_min_freq));
  804.             }else{
  805.               lcd.print(String(menu_random[4]) + String(" ") + String(random_min_freq));
  806.             }
  807.           }
  808.           else if(header_random + x == 5){
  809.             if(header_random == 5){
  810.               arrow_print(menu_random[5], String(random_max_freq));
  811.             }else{
  812.               lcd.print(String(menu_random[5]) + String(" ") + String(random_max_freq));
  813.             }
  814.           }
  815.           else if(header_random + x == 6){
  816.             if(header_random == 6){
  817.               start_print();
  818.             }
  819.             else{
  820.               lcd.print(String(start));
  821.             }
  822.           }
  823.         }
  824.       }
  825.       prev_flash = flash();
  826.       update = false;
  827.     }
  828.  
  829.     switch( random_button )
  830.     {
  831.     case BUTTON_NONE:
  832.       {
  833.         break;
  834.       }
  835.     case BUTTON_RIGHT:
  836.       {
  837.         if(header_random == 0 && random_minimum + 1 < random_maximum && random_minimum + 1 <= 255){
  838.           random_minimum++;
  839.           delay(button_wait_time_short);
  840.           update = true;
  841.         }
  842.         else if(header_random == 1 && random_maximum + 1 <= 255){
  843.           random_maximum++;
  844.           delay(button_wait_time_short);
  845.           update = true;
  846.         }
  847.         else if(header_random == 2 && random_time_min + 1 < random_time_max && random_time_min + 1 <= random_time_max_max){
  848.           random_time_min++;
  849.           delay(button_wait_time_short);
  850.           update = true;
  851.         }
  852.         else if(header_random == 3 && random_time_max + 1 <= random_time_max_max){
  853.           random_time_max++;
  854.           delay(button_wait_time_short);
  855.           update = true;
  856.         }
  857.         else if(header_random == 4 && random_min_freq + 1 < random_max_freq && random_min_freq <= random_max_freq_max){
  858.           random_min_freq++;
  859.           delay(button_wait_time_short);
  860.           update = true;
  861.         }
  862.         else if(header_random == 5 && random_max_freq + 1 <= random_max_freq_max){
  863.           random_max_freq++;
  864.           delay(button_wait_time_short);
  865.           update = true;
  866.         }
  867.         break;
  868.       }
  869.     case BUTTON_UP:
  870.       {
  871.         if(header_random-1 >= 0){
  872.           header_random -= 1;
  873.           update = true;
  874.         }
  875.         delay(button_wait_time_long);
  876.         break;
  877.       }
  878.     case BUTTON_DOWN:
  879.       {
  880.         if(header_random+1 < count_items_random){
  881.           header_random += 1;
  882.           update = true;
  883.         }
  884.         delay(button_wait_time_long);
  885.         break;
  886.       }
  887.     case BUTTON_LEFT:
  888.       {
  889.         if(header_random == 0 && random_minimum - 1 >= 0){
  890.           random_minimum -= 1;
  891.           delay(button_wait_time_short);
  892.           update = true;
  893.         }
  894.         else if(header_random == 1 && random_maximum - 1 > random_minimum && random_maximum - 1 >= 0){
  895.           random_maximum -= 1;
  896.           delay(button_wait_time_short);
  897.           update = true;
  898.         }
  899.         else if(header_random == 2 && random_time_min - 1 >= 1){
  900.           random_time_min -= 1;
  901.           delay(button_wait_time_short);
  902.           update = true;
  903.         }
  904.         else if(header_random == 3 && random_time_max - 1 > random_time_min && random_time_max - 1 >= 0){
  905.           random_time_max -= 1;
  906.           delay(button_wait_time_short);
  907.           update = true;
  908.         }
  909.         else if(header_random == 4 && random_min_freq - 1 >= 1){
  910.           random_min_freq -= 1;
  911.           delay(button_wait_time_short);
  912.           update = true;
  913.         }
  914.         else if(header_random == 5 && random_max_freq - 1 > random_min_freq && random_max_freq >= 1){
  915.           random_max_freq -= 1;
  916.           delay(button_wait_time_short);
  917.           update = true;
  918.         }
  919.         break;
  920.       }
  921.     case BUTTON_SELECT:
  922.       {  
  923.         if(header_random == 6){
  924.           delay(button_wait_time_long);
  925.           sub_random(random_minimum, random_maximum, random_time_min, random_time_max, random_min_freq, random_max_freq);
  926.           update = true;
  927.           delay(button_wait_time_long);
  928.         }
  929.         break;
  930.       }
  931.     default:
  932.       {
  933.         break;
  934.       }
  935.     }
  936.  
  937.   }
  938.   while(!(random_button == BUTTON_SELECT && header_random != count_items_random - 1));
  939.   return;
  940. }
  941.  
  942. void sub_random(long sub_random_minimum, long sub_random_maximum, long sub_random_time_min, long sub_random_time_max, long sub_random_min_freq, long sub_random_max_freq){
  943.   boolean sub_random_break = false;
  944.   long sub_random_output_minimum, sub_random_output_maximum, sub_random_output_freq;
  945.   long sub_random_temp_1, sub_random_temp_2, sub_random_output_time;
  946.   int sub_random_mode;
  947.   do{
  948.     sub_random_temp_1 = random(sub_random_minimum, sub_random_maximum + 1);
  949.     sub_random_temp_2 = random(sub_random_minimum, sub_random_maximum + 1);
  950.     if(sub_random_temp_1 >= sub_random_temp_2){
  951.       sub_random_output_minimum = sub_random_temp_2;
  952.       sub_random_output_maximum = sub_random_temp_1;
  953.     }
  954.     else{
  955.       sub_random_output_minimum = sub_random_temp_1;
  956.       sub_random_output_maximum = sub_random_temp_2;
  957.     }
  958.  
  959.     sub_random_output_freq = random( sub_random_min_freq, sub_random_max_freq + 1 );
  960.  
  961.     sub_random_output_time = random( sub_random_time_min * 1000, sub_random_time_max * 1000 + 1 );
  962.  
  963.     sub_random_mode = random(1, 7);
  964.  
  965.     sub_random_break = random_vibrator_wave(sub_random_mode, sub_random_output_minimum, sub_random_output_maximum, sub_random_output_freq, sub_random_output_time);
  966.  
  967.   }
  968.   while(sub_random_break == false);
  969.   return;
  970. }
  971.  
  972. boolean random_vibrator_wave(int random_wave_type, long random_wave_minimum, long random_wave_maximum, long random_wave_frequency, long random_wait){
  973.   byte random_wave_button;
  974.   long random_wave_output;
  975.   long random_wave_temp;
  976.   long time_left;
  977.   unsigned long random_start_time = millis();
  978.   boolean random_cancel = false;
  979.  
  980.   lcd.setCursor(0,0);
  981.   lcd.print(String("                "));
  982.   lcd.setCursor(0,1);
  983.   lcd.print(String("                "));
  984.  
  985.   if(random_wave_type == 1){
  986.  
  987.     //cosine
  988.     lcd.setCursor(0,0);
  989.     lcd.print(String("    COS: ") + String(random_wave_minimum) + String("-") + String(random_wave_maximum));
  990.     lcd.setCursor(0,1);
  991.     lcd.print(String("P: ") + String(random_wave_frequency));
  992.     do{
  993.       time_left = (random_wait + random_start_time - time) / 1000;
  994.       random_wave_button = ReadButtons();
  995.       time = millis();
  996.       random_wave_output = random_wave_minimum + (random_wave_maximum + 1 - random_wave_minimum) * ( (1 + sin(2 * pi * ( time % (random_wave_frequency * 1000) ) / ( random_wave_frequency * 1000 ) ) ) / 2);
  997.  
  998.       lcd.setCursor(0,0);
  999.       lcd.print(String(time_left) + String(" "));
  1000.  
  1001.       lcd.setCursor(8,1);
  1002.       lcd.print(String("LVL: ") + String(random_wave_output) + String("  "));
  1003.  
  1004.       analogWrite(vibrator_pin, random_wave_output);
  1005.     }
  1006.     while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
  1007.     if(random_wave_button == BUTTON_SELECT){
  1008.       random_cancel = true;
  1009.     }
  1010.     analogWrite(vibrator_pin, 0);
  1011.  
  1012.   }
  1013.   else if(random_wave_type == 2){
  1014.  
  1015.     //sawtooth
  1016.     lcd.setCursor(0,0);
  1017.     lcd.print(String("    SAW: ") + String(random_wave_minimum) + String("-") + String(random_wave_maximum));
  1018.     lcd.setCursor(0,1);
  1019.     lcd.print(String("P: ") + String(random_wave_frequency));
  1020.     do{
  1021.       time_left = (random_wait + random_start_time - time) / 1000;
  1022.       random_wave_button = ReadButtons();
  1023.       time = millis();
  1024.  
  1025.       random_wave_output = random_wave_minimum + (random_wave_maximum - random_wave_minimum) * ( time % (random_wave_frequency * 1000) ) / ( random_wave_frequency * 1000 );
  1026.  
  1027.       lcd.setCursor(0,0);
  1028.       lcd.print(String(time_left) + String(" "));
  1029.  
  1030.       lcd.setCursor(8,1);
  1031.       lcd.print(String("LVL: ") + String(random_wave_output) + String("  "));
  1032.  
  1033.       analogWrite(vibrator_pin, random_wave_output);
  1034.     }
  1035.     while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
  1036.     if(random_wave_button == BUTTON_SELECT){
  1037.       random_cancel = true;
  1038.     }
  1039.     analogWrite(vibrator_pin, 0);
  1040.  
  1041.   }
  1042.   else if(random_wave_type == 3){
  1043.  
  1044.     //triangle
  1045.     lcd.setCursor(0,0);
  1046.     lcd.print(String("    TRI: ") + String(random_wave_minimum) + String("-") + String(random_wave_maximum));
  1047.     lcd.setCursor(0,1);
  1048.     lcd.print(String("P: ") + String(random_wave_frequency));
  1049.     do{
  1050.       time_left = (random_wait + random_start_time - time) / 1000;
  1051.       random_wave_button = ReadButtons();
  1052.       time = millis();
  1053.  
  1054.       random_wave_temp = ( random_wave_maximum - random_wave_minimum ) - 2 * ( random_wave_maximum - random_wave_minimum ) * ( time % (random_wave_frequency * 1000) ) / ( random_wave_frequency * 1000 );
  1055.       random_wave_output = random_wave_maximum - abs( random_wave_temp );
  1056.  
  1057.       lcd.setCursor(0,0);
  1058.       lcd.print(String(time_left) + String(" "));
  1059.  
  1060.       lcd.setCursor(8,1);
  1061.       lcd.print(String("LVL: ") + String(random_wave_output) + String("  "));
  1062.  
  1063.       analogWrite(vibrator_pin, random_wave_output);
  1064.     }
  1065.     while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
  1066.     if(random_wave_button == BUTTON_SELECT){
  1067.       random_cancel = true;
  1068.     }
  1069.     analogWrite(vibrator_pin, 0);
  1070.  
  1071.   }
  1072.   else if(random_wave_type == 4){
  1073.  
  1074.     //square
  1075.     lcd.setCursor(0,0);
  1076.     lcd.print(String("    SQR: ") + String(random_wave_minimum) + String("-") + String(random_wave_maximum));
  1077.     lcd.setCursor(0,1);
  1078.     lcd.print(String("P: ") + String(random_wave_frequency));
  1079.     do{
  1080.       time_left = (random_wait + random_start_time - time) / 1000;
  1081.       random_wave_button = ReadButtons();
  1082.       time = millis();
  1083.  
  1084.       if(time % ( random_wave_frequency * 1000 ) < random_wave_frequency * 500 ){
  1085.         random_wave_output = random_wave_maximum;
  1086.       }
  1087.       else{
  1088.         random_wave_output = random_wave_minimum;
  1089.       }
  1090.  
  1091.       lcd.setCursor(0,0);
  1092.       lcd.print(String(time_left) + String(" "));
  1093.  
  1094.       lcd.setCursor(8,1);
  1095.       lcd.print(String("LVL: ") + String(random_wave_output) + String("  "));
  1096.  
  1097.       analogWrite(vibrator_pin, random_wave_output);
  1098.     }
  1099.     while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
  1100.     if(random_wave_button == BUTTON_SELECT){
  1101.       random_cancel = true;
  1102.     }
  1103.     analogWrite(vibrator_pin, 0);
  1104.  
  1105.   }
  1106.   else if(random_wave_type == 5){
  1107.  
  1108.     //off
  1109.     time_left = (random_wait + random_start_time - time) / 1000;
  1110.     lcd.setCursor(0,0);
  1111.     lcd.print(String("OFF FOR: ") + String( time_left ) );
  1112.  
  1113.     analogWrite(vibrator_pin, 0);
  1114.  
  1115.     do{
  1116.       time_left = (random_wait + random_start_time - time) / 1000;
  1117.       lcd.setCursor(9,0);
  1118.       lcd.print( String( time_left ) + String(" ") );
  1119.       random_wave_button = ReadButtons();
  1120.       time = millis();
  1121.     }
  1122.     while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
  1123.     if(random_wave_button == BUTTON_SELECT){
  1124.       random_cancel = true;
  1125.     }
  1126.   }
  1127.   else if(random_wave_type == 6){
  1128.  
  1129.     //constant
  1130.     lcd.setCursor(0,0);
  1131.     lcd.print(String("CONSTANT @ ") + String(random_wave_maximum));
  1132.     lcd.setCursor(0,1);
  1133.     lcd.print(String("FOR: "));
  1134.  
  1135.     analogWrite(vibrator_pin, random_wave_maximum);
  1136.  
  1137.     do{
  1138.       time_left = (random_wait + random_start_time - time) / 1000;
  1139.       lcd.setCursor(5,1);
  1140.       lcd.print( String( time_left ) + String(" ") );
  1141.       random_wave_button = ReadButtons();
  1142.       time = millis();
  1143.     }
  1144.     while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
  1145.     if(random_wave_button == BUTTON_SELECT){
  1146.       random_cancel = true;
  1147.     }
  1148.     analogWrite(vibrator_pin, 0);
  1149.   }
  1150.   return(random_cancel);
  1151. }
  1152.  
  1153. void setup()
  1154. {
  1155.   lcd.createChar(1, char_arrow_left);
  1156.   lcd.createChar(2, char_arrow_right);
  1157.  
  1158.   pinMode( BUTTON_ADC_PIN, INPUT );         //ensure A0 is an input
  1159.   digitalWrite( BUTTON_ADC_PIN, LOW );      //ensure pullup is off on A0
  1160.   //lcd backlight control
  1161.   digitalWrite( LCD_BACKLIGHT_PIN, HIGH );  //backlight control pin D3 is high (on)
  1162.   pinMode( LCD_BACKLIGHT_PIN, OUTPUT );     //D3 is an output
  1163.   //set up the LCD number of columns and rows:
  1164.   lcd.begin( 16, 2 );
  1165. }
  1166.  
  1167. void loop()
  1168. {
  1169.   byte main_button;
  1170.   //DISPLAY MAIN MENU
  1171.   if(header_main != prev_header_main
  1172.     || prev_flash != flash()
  1173.     ){
  1174.     for(int x = 0; x < 2; x++){
  1175.       if(x == 0 && flash() == 0){
  1176.         lcd.setCursor(0, 0);
  1177.         lcd.print(String("                "));
  1178.       }
  1179.       else if(header_main + x >= count_items_main){
  1180.         lcd.setCursor(0, x);
  1181.         lcd.print(String("                "));
  1182.       }
  1183.       else{
  1184.         lcd.setCursor(0, x);
  1185.         lcd.print(String("                "));
  1186.         lcd.setCursor(0, x);
  1187.         lcd.print(String(menu_main[header_main + x]));
  1188.       }
  1189.     }
  1190.     prev_header_main = header_main;
  1191.     prev_flash = flash();
  1192.   }
  1193.  
  1194.   main_button = ReadButtons();
  1195.  
  1196.   switch( main_button )
  1197.   {
  1198.   case BUTTON_NONE:
  1199.     {
  1200.       break;
  1201.     }
  1202.   case BUTTON_RIGHT:
  1203.     {
  1204.       break;
  1205.     }
  1206.   case BUTTON_UP:
  1207.     {
  1208.       if(header_main-1 >= 0){
  1209.         header_main -= 1;
  1210.       }
  1211.       delay(button_wait_time_long);
  1212.       break;
  1213.     }
  1214.   case BUTTON_DOWN:
  1215.     {
  1216.       if(header_main+1 < count_items_main){
  1217.         header_main += 1;
  1218.       }
  1219.       delay(button_wait_time_long);
  1220.       break;
  1221.     }
  1222.   case BUTTON_LEFT:
  1223.     {
  1224.       break;
  1225.     }
  1226.   case BUTTON_SELECT:
  1227.     {  
  1228.       switch(header_main){
  1229.       case 0:
  1230.         display_menu_constant();
  1231.         break;
  1232.       case 1:
  1233.         display_menu_wave();
  1234.         break;
  1235.       case 2:
  1236.         display_menu_wave_waves();
  1237.         break;
  1238.       case 3:
  1239.         display_menu_random();
  1240.         break;
  1241.       }
  1242.       delay(button_wait_time_long);
  1243.       break;
  1244.     }
  1245.   default:
  1246.     {
  1247.       break;
  1248.     }
  1249.   }
  1250.  
  1251.   if( buttonJustPressed )
  1252.     buttonJustPressed = false;
  1253.   if( buttonJustReleased )
  1254.     buttonJustReleased = false;
  1255. }
  1256.  
  1257. byte ReadButtons()
  1258. {
  1259.   unsigned int buttonVoltage;
  1260.   byte button = BUTTON_NONE;
  1261.  
  1262.   buttonVoltage = analogRead( BUTTON_ADC_PIN );
  1263.  
  1264.   if( buttonVoltage < ( RIGHT_10BIT_ADC + BUTTONHYSTERESIS ) )
  1265.   {
  1266.     button = BUTTON_RIGHT;
  1267.   }
  1268.   else if(   buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
  1269.     && buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
  1270.   {
  1271.     button = BUTTON_UP;
  1272.   }
  1273.   else if(   buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
  1274.     && buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
  1275.   {
  1276.     button = BUTTON_DOWN;
  1277.   }
  1278.   else if(   buttonVoltage >= ( LEFT_10BIT_ADC - BUTTONHYSTERESIS )
  1279.     && buttonVoltage <= ( LEFT_10BIT_ADC + BUTTONHYSTERESIS ) )
  1280.   {
  1281.     button = BUTTON_LEFT;
  1282.   }
  1283.   else if(   buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
  1284.     && buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
  1285.   {
  1286.     button = BUTTON_SELECT;
  1287.   }
  1288.   if( ( buttonWas == BUTTON_NONE ) && ( button != BUTTON_NONE ) )
  1289.   {
  1290.     buttonJustPressed  = true;
  1291.     buttonJustReleased = false;
  1292.   }
  1293.   if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
  1294.   {
  1295.     buttonJustPressed  = false;
  1296.     buttonJustReleased = true;
  1297.   }
  1298.  
  1299.   buttonWas = button;
  1300.  
  1301.   return( button );
  1302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement