daily pastebin goal
41%
SHARE
TWEET

Arduino Wave Generator - V6

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