Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h> // include LCD library
- /*--------------------------------------------------------------------------------------
- Defines
- --------------------------------------------------------------------------------------*/
- // Pins in use
- #define BUTTON_ADC_PIN A0 // A0 is the button ADC input
- #define LCD_BACKLIGHT_PIN 3 // D3 controls LCD backlight
- // ADC readings expected for the 5 buttons on the ADC input
- #define RIGHT_10BIT_ADC 0 // right
- #define UP_10BIT_ADC 145 // up
- #define DOWN_10BIT_ADC 329 // down
- #define LEFT_10BIT_ADC 505 // left
- #define SELECT_10BIT_ADC 741 // right
- #define BUTTONHYSTERESIS 10 // hysteresis for valid button sensing window
- //return values for ReadButtons()
- #define BUTTON_NONE 0 //
- #define BUTTON_RIGHT 1 //
- #define BUTTON_UP 2 //
- #define BUTTON_DOWN 3 //
- #define BUTTON_LEFT 4 //
- #define BUTTON_SELECT 5 //
- //some example macros with friendly labels for LCD backlight/pin control, tested and can be swapped into the example code as you like
- #define LCD_BACKLIGHT_OFF() digitalWrite( LCD_BACKLIGHT_PIN, LOW )
- #define LCD_BACKLIGHT_ON() digitalWrite( LCD_BACKLIGHT_PIN, HIGH )
- #define LCD_BACKLIGHT(state) { if( state ){digitalWrite( LCD_BACKLIGHT_PIN, HIGH );}else{digitalWrite( LCD_BACKLIGHT_PIN, LOW );} }
- /*--------------------------------------------------------------------------------------
- Variables
- --------------------------------------------------------------------------------------*/
- byte buttonJustPressed = false; //this will be true after a ReadButtons() call if triggered
- byte buttonJustReleased = false; //this will be true after a ReadButtons() call if triggered
- byte buttonWas = BUTTON_NONE; //used by ReadButtons() for detection of button events
- byte button;
- byte char_arrow_left[8] = {
- B00011,
- B00111,
- B01111,
- B11111,
- B11111,
- B01111,
- B00111,
- B00011};
- byte char_arrow_right[8] = {
- B11000,
- B11100,
- B11110,
- B11111,
- B11111,
- B11110,
- B11100,
- B11000};
- int vibrator_pin = 10;
- int button_wait_time_short = 60;
- int button_wait_time_long = 250;
- String start = "-----START!-----";
- String blank_start = "----- -----";
- const float pi = 3.14159;
- unsigned long time;
- int prev_time, flasher, prev_flash;
- 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 )
- /*---------Main Menu---------*/
- int header_main = 0;
- int prev_header_main;
- char* menu_main[] = {
- "Constant", "Wave", "Wave Of Waves", "Random"};
- int count_items_main = 4;
- /*---------------------------*/
- /*-------Constant Menu-------*/
- int constant_level = 255;
- int header_constant = 0;
- char* menu_constant[] = {
- "Level:"};
- int count_items_constant = 2;
- /*---------------------------*/
- /*---------Wave Menu---------*/
- int header_wave = 0;
- int wave_type = 0;
- long wave_minimum = 0;
- long wave_maximum = 255;
- long wave_frequency = 4; //4 sec, just a preference
- const long wave_max_frequency = 9999; //9999 sec = 170mins
- char* menu_wave[] = {
- "Type:", "MinLvl:", "MaxLvl:", "Period:"};
- int count_items_wave = 5;
- char* type_wave[] = {
- "Cosine", "Sawtooth", "Triangle", "Square"};
- int count_items_type_wave = 4;
- /*---------------------------*/
- /*-------Wave of Waves-------*/
- int header_wave_waves = 0;
- long wave_waves_min = 140;
- long wave_waves_max = 255;
- long wave_waves_multiplier = 10; //relative multiplier on the amount of cycles (there will always be an infinite amount, this just changes the rate.)
- const long wave_waves_multiplier_max = 999;
- long wave_waves_wavelength = 180; //seconds
- const long wave_waves_wavelength_max = 9999; //17 mins
- char* menu_wave_waves[] = {
- "MinLvl:", "MaxLvl:", "Multix:", "Period:"};
- int count_items_wave_waves = 5;
- /*---------------------------*/
- /*---------Rand Menu---------*/
- int header_random = 0;
- long random_minimum = 0;
- long random_maximum = 255;
- long random_min_freq = 1; //1 second
- long random_max_freq = 20; //20 seconds
- const long random_max_freq_max = 9999; //1000 seconds. Roughly 17mins, 1000 so it doesn't overflow the screen
- long random_time_min = 1;
- long random_time_max = 120; //120 seconds = 2min
- const long random_time_max_max = 9999; //1000sec
- unsigned long prev_random_time_max;
- char* menu_random[] = {
- "MinLvl:", "MaxLvl:", "MinTime:", "MaxTime:", "MinPeriod:", "MaxPeriod:"};
- int count_items_random = 7;
- /*---------------------------*/
- void arrow_print(char* string_one, String string_two){
- if(flash()){
- lcd.print(String(string_one));
- lcd.write(1);
- lcd.print(String(string_two));
- lcd.write(2);
- }else{
- lcd.print(String(string_one) + String(" ") + String(string_two));
- }
- }
- void start_print(){
- if(flash()){
- lcd.print(String(start));
- }
- else{
- lcd.print(String(blank_start));
- }
- }
- int flash(){
- int timestamp;
- timestamp = ( (millis() / 500) % 100 );
- if(timestamp != prev_time){
- if(flasher == 1){
- flasher = 0;
- }
- else{
- flasher = 1;
- }
- }
- prev_time = timestamp;
- return flasher;
- }
- void display_menu_constant(){
- delay(button_wait_time_long);
- boolean update = true;
- byte constant_button;
- do{
- constant_button = ReadButtons();
- if(update || flash() != prev_flash){
- for(int x = 0; x < 2; x++){
- if(header_constant + x >= count_items_constant){
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- }
- else{
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- lcd.setCursor(0, x);
- if(header_constant + x == 0){
- if(header_constant == 0){
- arrow_print(menu_constant[0], String(constant_level));
- }else{
- lcd.print(String(menu_constant[0]) + String(" ") + String(constant_level));
- }
- }
- else if(header_constant + x == 1){
- if(header_constant == 1){
- start_print();
- }
- else{
- lcd.print(String(start));
- }
- }
- }
- }
- prev_flash = flash();
- update = false;
- }
- switch( constant_button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- //Increment Selection
- if(header_constant == 0 && constant_level + 1 <= 255){
- constant_level++;
- delay(button_wait_time_short);
- update = true;
- }
- break;
- }
- case BUTTON_UP:
- {
- if(header_constant-1 >= 0){
- header_constant -= 1;
- update = true;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_DOWN:
- {
- if(header_constant+1 < count_items_constant){
- header_constant++;
- update = true;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_LEFT:
- {
- //Decrement Selection
- if(header_constant == 0 && constant_level - 1 >= 0){
- constant_level -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- break;
- }
- case BUTTON_SELECT:
- {
- if(header_constant == count_items_constant - 1){
- delay(250);
- vibrator_constant(constant_level);
- update = true;
- delay(button_wait_time_long);
- }
- break;
- }
- default:
- {
- break;
- }
- }
- }
- while(!(constant_button == BUTTON_SELECT && header_constant != count_items_constant - 1));
- return;
- }
- void vibrator_constant(int constant_level_output){
- byte vibrator_constant_button;
- lcd.setCursor(0,0);
- lcd.print(String(" "));
- analogWrite(vibrator_pin, constant_level_output);
- do{
- vibrator_constant_button = ReadButtons();
- lcd.setCursor(0,0);
- lcd.print(String("CONSTANT @ ") + String(constant_level_output));
- }
- while(vibrator_constant_button != BUTTON_SELECT);
- analogWrite(vibrator_pin, 0);
- return;
- }
- void display_menu_wave(){
- delay(button_wait_time_long);
- byte wave_button;
- boolean update = true;
- do{
- wave_button = ReadButtons();
- if(update || flash() != prev_flash){
- for(int x = 0; x < 2; x++){
- if(header_wave + x >= count_items_wave){
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- }
- else{
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- lcd.setCursor(0, x);
- if(header_wave + x == 0){
- if(header_wave == 0){
- arrow_print(menu_wave[0], type_wave[wave_type]);
- }else{
- lcd.print(String(menu_wave[0]) + String(" ") + String(type_wave[wave_type]));
- }
- }
- else if(header_wave + x == 1){
- if(header_wave == 1){
- arrow_print(menu_wave[1], String(wave_minimum));
- }else{
- lcd.print(String(menu_wave[1]) + String(" ") + String(wave_minimum));
- }
- }
- else if(header_wave + x == 2){
- if(header_wave == 2){
- arrow_print(menu_wave[2], String(wave_maximum));
- }else{
- lcd.print(String(menu_wave[2]) + String(" ") + String(wave_maximum));
- }
- }
- else if(header_wave + x == 3){
- if(header_wave == 3){
- arrow_print(menu_wave[3], String(wave_frequency));
- }else{
- lcd.print(String(menu_wave[3]) + String(" ") + String(wave_frequency));
- }
- }
- else if(header_wave + x == 4){
- if(header_wave == 4){
- start_print();
- }
- else{
- lcd.print(String(start));
- }
- }
- }
- }
- prev_flash = flash();
- update = false;
- }
- switch( wave_button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- //Increment Selection
- if(header_wave == 0 && wave_type + 1 <= 3){
- wave_type++;
- delay(button_wait_time_long);
- update = true;
- }
- else if(header_wave == 1 && wave_minimum + 1 < 256 && wave_minimum < wave_maximum){
- wave_minimum++;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_wave == 2 && wave_maximum + 1 < 256){
- wave_maximum++;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_wave == 3 && wave_frequency + 1 <= wave_max_frequency){
- wave_frequency++;
- delay(button_wait_time_short);
- update = true;
- }
- break;
- }
- case BUTTON_UP:
- {
- if(header_wave - 1 >= 0){
- header_wave -= 1;
- update = true;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_DOWN:
- {
- if(header_wave + 1 < count_items_wave){
- header_wave++;
- update = true;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_LEFT:
- {
- //Decrement Selection
- if(header_wave == 0 && wave_type > 0){
- wave_type -= 1;
- delay(button_wait_time_long);
- update = true;
- }
- else if(header_wave == 1 && wave_minimum > 0){
- wave_minimum -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_wave == 2 && wave_maximum > 0 && wave_maximum > wave_minimum){
- wave_maximum -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_wave == 3 && wave_frequency > 1){
- wave_frequency -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- break;
- }
- case BUTTON_SELECT:
- {
- if(header_wave == count_items_wave - 1){
- delay(button_wait_time_long);
- vibrator_wave(wave_type, wave_minimum, wave_maximum, wave_frequency);
- update = true;
- delay(button_wait_time_long);
- }
- break;
- }
- default:
- {
- break;
- }
- }
- }
- while(!(wave_button == BUTTON_SELECT && header_wave != count_items_wave - 1));
- return;
- }
- void vibrator_wave(int vibrator_wave_type, long vibrator_wave_minimum, long vibrator_wave_maximum, long vibrator_wave_frequency){
- byte vibrator_wave_button;
- long vibrator_wave_output;
- long vibrator_wave_temp;
- String vibrator_display_output;
- lcd.setCursor(0,0);
- lcd.print(String(" "));
- lcd.setCursor(0,1);
- lcd.print(String(" "));
- if(vibrator_wave_type == 0){
- //sine
- lcd.setCursor(0,0);
- lcd.print(String("COSINE: ") + String(vibrator_wave_minimum) + String("-") + String(vibrator_wave_maximum));
- lcd.setCursor(0,1);
- lcd.print(String("P: ") + String(vibrator_wave_frequency));
- do{
- vibrator_wave_button = ReadButtons();
- time = millis();
- 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);
- lcd.setCursor(8,1);
- lcd.print(String("LVL: ") + String(vibrator_wave_output) + String(" "));
- analogWrite(vibrator_pin, vibrator_wave_output);
- }
- while(vibrator_wave_button != BUTTON_SELECT);
- analogWrite(vibrator_pin, 0);
- }
- else if(vibrator_wave_type == 1){
- //sawtooth
- lcd.setCursor(0,0);
- lcd.print(String("SAWTOOTH:") + String(vibrator_wave_minimum) + String("-") + String(vibrator_wave_maximum));
- lcd.setCursor(0,1);
- lcd.print(String("P: ") + String(vibrator_wave_frequency));
- do{
- vibrator_wave_button = ReadButtons();
- time = millis();
- vibrator_wave_output = vibrator_wave_minimum + (vibrator_wave_maximum - vibrator_wave_minimum) * ( time % (vibrator_wave_frequency * 1000) ) / ( vibrator_wave_frequency * 1000 );
- lcd.setCursor(8,1);
- lcd.print(String("LVL: ") + String(vibrator_wave_output) + String(" "));
- analogWrite(vibrator_pin, vibrator_wave_output);
- }
- while(vibrator_wave_button != BUTTON_SELECT);
- analogWrite(vibrator_pin, 0);
- }
- else if(vibrator_wave_type == 2){
- //triangle
- lcd.setCursor(0,0);
- lcd.print(String("TRIANGLE:") + String(vibrator_wave_minimum) + String("-") + String(vibrator_wave_maximum));
- lcd.setCursor(0,1);
- lcd.print(String("P: ") + String(vibrator_wave_frequency));
- do{
- vibrator_wave_button = ReadButtons();
- time = millis();
- 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 );
- vibrator_wave_output = vibrator_wave_maximum - abs( vibrator_wave_temp );
- lcd.setCursor(8,1);
- lcd.print(String("LVL: ") + String(vibrator_wave_output) + String(" "));
- analogWrite(vibrator_pin, vibrator_wave_output);
- }
- while(vibrator_wave_button != BUTTON_SELECT);
- analogWrite(vibrator_pin, 0);
- }
- else if(vibrator_wave_type == 3){
- //square
- lcd.setCursor(0,0);
- lcd.print(String("SQUARE: ") + String(vibrator_wave_minimum) + String("-") + String(vibrator_wave_maximum));
- lcd.setCursor(0,1);
- lcd.print(String("P: ") + String(vibrator_wave_frequency));
- do{
- vibrator_wave_button = ReadButtons();
- time = millis();
- if(time % ( vibrator_wave_frequency * 1000 ) < vibrator_wave_frequency * 500 ){
- vibrator_wave_output = vibrator_wave_maximum;
- }
- else{
- vibrator_wave_output = vibrator_wave_minimum;
- }
- lcd.setCursor(8,1);
- lcd.print(String("LVL: ") + String(vibrator_wave_output) + String(" "));
- analogWrite(vibrator_pin, vibrator_wave_output);
- }
- while(vibrator_wave_button != BUTTON_SELECT);
- analogWrite(vibrator_pin, 0);
- }
- return;
- }
- void display_menu_wave_waves(){
- delay(250);
- byte wave_waves_button;
- boolean update = true;
- do{
- wave_waves_button = ReadButtons();
- if(update || flash() != prev_flash
- ){
- for(int x = 0; x < 2; x++){
- if(header_wave_waves + x >= count_items_wave_waves){
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- }
- else{
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- lcd.setCursor(0, x);
- if(header_wave_waves + x == 0){
- if(header_wave_waves == 0){
- arrow_print(menu_wave_waves[0], String(wave_waves_min));
- }else{
- lcd.print(String(menu_wave_waves[0]) + String(" ") + String(wave_waves_min));
- }
- }
- else if(header_wave_waves + x == 1){
- if(header_wave_waves == 1){
- arrow_print(menu_wave_waves[1], String(wave_waves_max));
- }else{
- lcd.print(String(menu_wave_waves[1]) + String(" ") + String(wave_waves_max));
- }
- }
- else if(header_wave_waves + x == 2){
- if(header_wave_waves == 2){
- arrow_print(menu_wave_waves[2], String(wave_waves_multiplier));
- }else{
- lcd.print(String(menu_wave_waves[2]) + String(" ") + String(wave_waves_multiplier));
- }
- }
- else if(header_wave_waves + x == 3){
- if(header_wave_waves == 3){
- arrow_print(menu_wave_waves[3], String(wave_waves_wavelength));
- }else{
- lcd.print(String(menu_wave_waves[3]) + String(" ") + String(wave_waves_wavelength));
- }
- }
- else if(header_wave_waves + x == 4){
- if(header_wave_waves == 4){
- start_print();
- }
- else{
- lcd.print(String(start));
- }
- }
- }
- }
- prev_flash = flash();
- update = false;
- }
- switch( wave_waves_button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- if(header_wave_waves == 0 && wave_waves_min + 1 < wave_waves_max && wave_waves_min + 1 <= 255){
- wave_waves_min++;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_wave_waves == 1 && wave_waves_max + 1 <= 255){
- wave_waves_max++;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_wave_waves == 2 && wave_waves_multiplier + 1 <= wave_waves_multiplier_max){
- wave_waves_multiplier++;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_wave_waves == 3 && wave_waves_wavelength + 1 <= wave_waves_wavelength_max){
- wave_waves_wavelength++;
- delay(button_wait_time_short);
- update = true;
- }
- break;
- }
- case BUTTON_UP:
- {
- if(header_wave_waves - 1 >= 0){
- header_wave_waves -= 1;
- update = true;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_DOWN:
- {
- if(header_wave_waves + 1 < count_items_wave_waves){
- header_wave_waves += 1;
- update = true;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_LEFT:
- {
- if(header_wave_waves == 0 && wave_waves_min - 1 >= 0){
- wave_waves_min -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_wave_waves == 1 && wave_waves_max - 1 > 0 && wave_waves_max - 1 > wave_waves_min){
- wave_waves_max -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_wave_waves == 2 && wave_waves_multiplier - 1 > 0){
- wave_waves_multiplier -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_wave_waves == 3 && wave_waves_wavelength - 1 > 0){
- wave_waves_wavelength -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- break;
- }
- case BUTTON_SELECT:
- {
- if(header_wave_waves == 4){
- delay(button_wait_time_long);
- vibrate_wave_waves(wave_waves_min, wave_waves_max, wave_waves_multiplier, wave_waves_wavelength);
- update = true;
- delay(button_wait_time_long);
- }
- break;
- }
- default:
- {
- break;
- }
- }
- }while(!(wave_waves_button == BUTTON_SELECT && header_wave_waves != count_items_wave_waves - 1));
- return;
- }
- void vibrate_wave_waves(long vibrate_wave_waves_min, long vibrate_wave_waves_max, long vibrate_wave_waves_multiplier, long vibrate_wave_waves_wavelength){
- byte vibrator_wave_waves_button;
- long vibrate_wave_waves_output;
- lcd.setCursor(0,0);
- lcd.print(String(" "));
- lcd.setCursor(0,1);
- lcd.print(String(" "));
- lcd.setCursor(0,0);
- lcd.print(String("WW:") + String(vibrate_wave_waves_min) + String("-") + String(vibrate_wave_waves_max));
- lcd.setCursor(11,0);
- lcd.print(String("V:"));
- lcd.setCursor(0,1);
- lcd.print(String("P:") + String(vibrate_wave_waves_wavelength));
- lcd.setCursor(10,1);
- lcd.print(String("MP:") + String(vibrate_wave_waves_multiplier));
- do{
- time = millis();
- vibrator_wave_waves_button = ReadButtons();
- 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 );
- lcd.setCursor(13,0);
- lcd.print(String(vibrate_wave_waves_output) + String(" "));
- analogWrite(vibrator_pin, vibrate_wave_waves_output);
- }
- while(vibrator_wave_waves_button != BUTTON_SELECT);
- analogWrite(vibrator_pin, 0);
- return;
- }
- void display_menu_random(){
- delay(250);
- byte random_button;
- boolean update = true;
- do{
- random_button = ReadButtons();
- if(update || flash() != prev_flash){
- for(int x = 0; x < 2; x++){
- if(header_random + x >= count_items_random){
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- }
- else{
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- lcd.setCursor(0, x);
- if(header_random + x == 0){
- if(header_random == 0){
- arrow_print(menu_random[0], String(random_minimum));
- }else{
- lcd.print(String(menu_random[0]) + String(" ") + String(random_minimum));
- }
- }
- else if(header_random + x == 1){
- if(header_random == 1){
- arrow_print(menu_random[1], String(random_maximum));
- }else{
- lcd.print(String(menu_random[1]) + String(" ") + String(random_maximum));
- }
- }
- else if(header_random + x == 2){
- if(header_random == 2){
- arrow_print(menu_random[2], String(random_time_min));
- }else{
- lcd.print(String(menu_random[2]) + String(" ") + String(random_time_min));
- }
- }
- else if(header_random + x == 3){
- if(header_random == 3){
- arrow_print(menu_random[3], String(random_time_max));
- }else{
- lcd.print(String(menu_random[3]) + String(" ") + String(random_time_max));
- }
- }
- else if(header_random + x == 4){
- if(header_random == 4){
- arrow_print(menu_random[4], String(random_min_freq));
- }else{
- lcd.print(String(menu_random[4]) + String(" ") + String(random_min_freq));
- }
- }
- else if(header_random + x == 5){
- if(header_random == 5){
- arrow_print(menu_random[5], String(random_max_freq));
- }else{
- lcd.print(String(menu_random[5]) + String(" ") + String(random_max_freq));
- }
- }
- else if(header_random + x == 6){
- if(header_random == 6){
- start_print();
- }
- else{
- lcd.print(String(start));
- }
- }
- }
- }
- prev_flash = flash();
- update = false;
- }
- switch( random_button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- if(header_random == 0 && random_minimum + 1 < random_maximum && random_minimum + 1 <= 255){
- random_minimum++;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_random == 1 && random_maximum + 1 <= 255){
- random_maximum++;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_random == 2 && random_time_min + 1 < random_time_max && random_time_min + 1 <= random_time_max_max){
- random_time_min++;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_random == 3 && random_time_max + 1 <= random_time_max_max){
- random_time_max++;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_random == 4 && random_min_freq + 1 < random_max_freq && random_min_freq <= random_max_freq_max){
- random_min_freq++;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_random == 5 && random_max_freq + 1 <= random_max_freq_max){
- random_max_freq++;
- delay(button_wait_time_short);
- update = true;
- }
- break;
- }
- case BUTTON_UP:
- {
- if(header_random-1 >= 0){
- header_random -= 1;
- update = true;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_DOWN:
- {
- if(header_random+1 < count_items_random){
- header_random += 1;
- update = true;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_LEFT:
- {
- if(header_random == 0 && random_minimum - 1 >= 0){
- random_minimum -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_random == 1 && random_maximum - 1 > random_minimum && random_maximum - 1 >= 0){
- random_maximum -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_random == 2 && random_time_min - 1 >= 1){
- random_time_min -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_random == 3 && random_time_max - 1 > random_time_min && random_time_max - 1 >= 0){
- random_time_max -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_random == 4 && random_min_freq - 1 >= 1){
- random_min_freq -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- else if(header_random == 5 && random_max_freq - 1 > random_min_freq && random_max_freq >= 1){
- random_max_freq -= 1;
- delay(button_wait_time_short);
- update = true;
- }
- break;
- }
- case BUTTON_SELECT:
- {
- if(header_random == 6){
- delay(button_wait_time_long);
- sub_random(random_minimum, random_maximum, random_time_min, random_time_max, random_min_freq, random_max_freq);
- update = true;
- delay(button_wait_time_long);
- }
- break;
- }
- default:
- {
- break;
- }
- }
- }
- while(!(random_button == BUTTON_SELECT && header_random != count_items_random - 1));
- return;
- }
- 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){
- boolean sub_random_break = false;
- long sub_random_output_minimum, sub_random_output_maximum, sub_random_output_freq;
- long sub_random_temp_1, sub_random_temp_2, sub_random_output_time;
- int sub_random_mode;
- do{
- sub_random_temp_1 = random(sub_random_minimum, sub_random_maximum + 1);
- sub_random_temp_2 = random(sub_random_minimum, sub_random_maximum + 1);
- if(sub_random_temp_1 >= sub_random_temp_2){
- sub_random_output_minimum = sub_random_temp_2;
- sub_random_output_maximum = sub_random_temp_1;
- }
- else{
- sub_random_output_minimum = sub_random_temp_1;
- sub_random_output_maximum = sub_random_temp_2;
- }
- sub_random_output_freq = random( sub_random_min_freq, sub_random_max_freq + 1 );
- sub_random_output_time = random( sub_random_time_min * 1000, sub_random_time_max * 1000 + 1 );
- sub_random_mode = random(1, 7);
- 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);
- }
- while(sub_random_break == false);
- return;
- }
- boolean random_vibrator_wave(int random_wave_type, long random_wave_minimum, long random_wave_maximum, long random_wave_frequency, long random_wait){
- byte random_wave_button;
- long random_wave_output;
- long random_wave_temp;
- long time_left;
- unsigned long random_start_time = millis();
- boolean random_cancel = false;
- lcd.setCursor(0,0);
- lcd.print(String(" "));
- lcd.setCursor(0,1);
- lcd.print(String(" "));
- if(random_wave_type == 1){
- //cosine
- lcd.setCursor(0,0);
- lcd.print(String(" COS: ") + String(random_wave_minimum) + String("-") + String(random_wave_maximum));
- lcd.setCursor(0,1);
- lcd.print(String("P: ") + String(random_wave_frequency));
- do{
- time_left = (random_wait + random_start_time - time) / 1000;
- random_wave_button = ReadButtons();
- time = millis();
- 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);
- lcd.setCursor(0,0);
- lcd.print(String(time_left) + String(" "));
- lcd.setCursor(8,1);
- lcd.print(String("LVL: ") + String(random_wave_output) + String(" "));
- analogWrite(vibrator_pin, random_wave_output);
- }
- while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(random_wave_button == BUTTON_SELECT){
- random_cancel = true;
- }
- analogWrite(vibrator_pin, 0);
- }
- else if(random_wave_type == 2){
- //sawtooth
- lcd.setCursor(0,0);
- lcd.print(String(" SAW: ") + String(random_wave_minimum) + String("-") + String(random_wave_maximum));
- lcd.setCursor(0,1);
- lcd.print(String("P: ") + String(random_wave_frequency));
- do{
- time_left = (random_wait + random_start_time - time) / 1000;
- random_wave_button = ReadButtons();
- time = millis();
- random_wave_output = random_wave_minimum + (random_wave_maximum - random_wave_minimum) * ( time % (random_wave_frequency * 1000) ) / ( random_wave_frequency * 1000 );
- lcd.setCursor(0,0);
- lcd.print(String(time_left) + String(" "));
- lcd.setCursor(8,1);
- lcd.print(String("LVL: ") + String(random_wave_output) + String(" "));
- analogWrite(vibrator_pin, random_wave_output);
- }
- while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(random_wave_button == BUTTON_SELECT){
- random_cancel = true;
- }
- analogWrite(vibrator_pin, 0);
- }
- else if(random_wave_type == 3){
- //triangle
- lcd.setCursor(0,0);
- lcd.print(String(" TRI: ") + String(random_wave_minimum) + String("-") + String(random_wave_maximum));
- lcd.setCursor(0,1);
- lcd.print(String("P: ") + String(random_wave_frequency));
- do{
- time_left = (random_wait + random_start_time - time) / 1000;
- random_wave_button = ReadButtons();
- time = millis();
- 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 );
- random_wave_output = random_wave_maximum - abs( random_wave_temp );
- lcd.setCursor(0,0);
- lcd.print(String(time_left) + String(" "));
- lcd.setCursor(8,1);
- lcd.print(String("LVL: ") + String(random_wave_output) + String(" "));
- analogWrite(vibrator_pin, random_wave_output);
- }
- while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(random_wave_button == BUTTON_SELECT){
- random_cancel = true;
- }
- analogWrite(vibrator_pin, 0);
- }
- else if(random_wave_type == 4){
- //square
- lcd.setCursor(0,0);
- lcd.print(String(" SQR: ") + String(random_wave_minimum) + String("-") + String(random_wave_maximum));
- lcd.setCursor(0,1);
- lcd.print(String("P: ") + String(random_wave_frequency));
- do{
- time_left = (random_wait + random_start_time - time) / 1000;
- random_wave_button = ReadButtons();
- time = millis();
- if(time % ( random_wave_frequency * 1000 ) < random_wave_frequency * 500 ){
- random_wave_output = random_wave_maximum;
- }
- else{
- random_wave_output = random_wave_minimum;
- }
- lcd.setCursor(0,0);
- lcd.print(String(time_left) + String(" "));
- lcd.setCursor(8,1);
- lcd.print(String("LVL: ") + String(random_wave_output) + String(" "));
- analogWrite(vibrator_pin, random_wave_output);
- }
- while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(random_wave_button == BUTTON_SELECT){
- random_cancel = true;
- }
- analogWrite(vibrator_pin, 0);
- }
- else if(random_wave_type == 5){
- //off
- time_left = (random_wait + random_start_time - time) / 1000;
- lcd.setCursor(0,0);
- lcd.print(String("OFF FOR: ") + String( time_left ) );
- analogWrite(vibrator_pin, 0);
- do{
- time_left = (random_wait + random_start_time - time) / 1000;
- lcd.setCursor(9,0);
- lcd.print( String( time_left ) + String(" ") );
- random_wave_button = ReadButtons();
- time = millis();
- }
- while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(random_wave_button == BUTTON_SELECT){
- random_cancel = true;
- }
- }
- else if(random_wave_type == 6){
- //constant
- lcd.setCursor(0,0);
- lcd.print(String("CONSTANT @ ") + String(random_wave_maximum));
- lcd.setCursor(0,1);
- lcd.print(String("FOR: "));
- analogWrite(vibrator_pin, random_wave_maximum);
- do{
- time_left = (random_wait + random_start_time - time) / 1000;
- lcd.setCursor(5,1);
- lcd.print( String( time_left ) + String(" ") );
- random_wave_button = ReadButtons();
- time = millis();
- }
- while(random_wave_button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(random_wave_button == BUTTON_SELECT){
- random_cancel = true;
- }
- analogWrite(vibrator_pin, 0);
- }
- return(random_cancel);
- }
- void setup()
- {
- lcd.createChar(1, char_arrow_left);
- lcd.createChar(2, char_arrow_right);
- pinMode( BUTTON_ADC_PIN, INPUT ); //ensure A0 is an input
- digitalWrite( BUTTON_ADC_PIN, LOW ); //ensure pullup is off on A0
- //lcd backlight control
- digitalWrite( LCD_BACKLIGHT_PIN, HIGH ); //backlight control pin D3 is high (on)
- pinMode( LCD_BACKLIGHT_PIN, OUTPUT ); //D3 is an output
- //set up the LCD number of columns and rows:
- lcd.begin( 16, 2 );
- }
- void loop()
- {
- byte main_button;
- //DISPLAY MAIN MENU
- if(header_main != prev_header_main
- || prev_flash != flash()
- ){
- for(int x = 0; x < 2; x++){
- if(x == 0 && flash() == 0){
- lcd.setCursor(0, 0);
- lcd.print(String(" "));
- }
- else if(header_main + x >= count_items_main){
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- }
- else{
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- lcd.setCursor(0, x);
- lcd.print(String(menu_main[header_main + x]));
- }
- }
- prev_header_main = header_main;
- prev_flash = flash();
- }
- main_button = ReadButtons();
- switch( main_button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- break;
- }
- case BUTTON_UP:
- {
- if(header_main-1 >= 0){
- header_main -= 1;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_DOWN:
- {
- if(header_main+1 < count_items_main){
- header_main += 1;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_LEFT:
- {
- break;
- }
- case BUTTON_SELECT:
- {
- switch(header_main){
- case 0:
- display_menu_constant();
- break;
- case 1:
- display_menu_wave();
- break;
- case 2:
- display_menu_wave_waves();
- break;
- case 3:
- display_menu_random();
- break;
- }
- delay(button_wait_time_long);
- break;
- }
- default:
- {
- break;
- }
- }
- if( buttonJustPressed )
- buttonJustPressed = false;
- if( buttonJustReleased )
- buttonJustReleased = false;
- }
- byte ReadButtons()
- {
- unsigned int buttonVoltage;
- byte button = BUTTON_NONE;
- buttonVoltage = analogRead( BUTTON_ADC_PIN );
- if( buttonVoltage < ( RIGHT_10BIT_ADC + BUTTONHYSTERESIS ) )
- {
- button = BUTTON_RIGHT;
- }
- else if( buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
- && buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
- {
- button = BUTTON_UP;
- }
- else if( buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
- && buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
- {
- button = BUTTON_DOWN;
- }
- else if( buttonVoltage >= ( LEFT_10BIT_ADC - BUTTONHYSTERESIS )
- && buttonVoltage <= ( LEFT_10BIT_ADC + BUTTONHYSTERESIS ) )
- {
- button = BUTTON_LEFT;
- }
- else if( buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
- && buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
- {
- button = BUTTON_SELECT;
- }
- if( ( buttonWas == BUTTON_NONE ) && ( button != BUTTON_NONE ) )
- {
- buttonJustPressed = true;
- buttonJustReleased = false;
- }
- if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
- {
- buttonJustPressed = false;
- buttonJustReleased = true;
- }
- buttonWas = button;
- return( button );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement