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 );} }
- #define LCD_BACKLIGHT_LEVEL(level) { if(level <= 255 && level >= 0){analogWrite( LCD_BACKLIGHT_PIN, level );}else{digitalWrite(LCD_BACKLIGHT_PIN, HIGH );}}
- /*--------------------------------------------------------------------------------------
- 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] = {
- B00001,
- B00011,
- B00111,
- B01111,
- B00111,
- B00011,
- B00001,
- B00000};
- byte char_arrow_right[8] = {
- B10000,
- B11000,
- B11100,
- B11110,
- B11100,
- B11000,
- B10000,
- B00000};
- const int vibrator_pin = 10;
- const String start = "-----START!-----";
- const String blank_start = "----- -----";
- const float pi = 3.14159;
- const int output_max = 255;
- unsigned long time;
- int prev_time, flasher, prev_flash;
- boolean update = true;
- 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;
- char* menu_main[] = {
- "Constant", "Wave", "Wave Of Waves", "Random", "Settings"};
- int count_items_main = 5;
- /*---------------------------*/
- /*-------Constant Menu-------*/
- int constant_level = 255;
- int header_constant = 0;
- char* menu_constant[] = {
- "Level:"};
- int count_items_constant = 1;
- /*---------------------------*/
- /*---------Wave Menu---------*/
- int header_wave = 0;
- long wave_values[] = {
- 0, /* wave type */
- 0, /* minimum level */
- 255, /* maximum level */
- 4 /* wave period */
- };
- long wave_values_max[] = {
- 3, /* wave type */
- 255, /* minimum level */
- 255, /* maximum level */
- 9999 /* wave period */
- };
- char* menu_wave[] = {
- "Type:", "MinLvl:", "MaxLvl:", "Period:"};
- int count_items_wave = 4;
- char* type_wave[] = {
- "Cosine", "Sawtooth", "Triangle", "Square"};
- int count_items_type_wave = 4;
- /*---------------------------*/
- /*-------Wave of Waves-------*/
- int header_wave_waves = 0;
- long wave_waves_values[] = {
- 140, /* min level */
- 255, /* max level */
- 10, /* multiplier */
- 180 /* wave period */
- };
- long wave_waves_values_max[] = {
- 255, /* min level */
- 255, /* max level */
- 99, /* multiplier */
- 9999 /* wave period */
- };
- char* menu_wave_waves[] = {
- "MinLvl:", "MaxLvl:", "Multix:", "Period:"};
- int count_items_wave_waves = 4;
- /*---------------------------*/
- /*---------Rand Menu---------*/
- int header_random = 0;
- long random_values[] = {
- 0, /* min level */
- 255, /* max level */
- 1, /* min period */
- 20, /* max period */
- 1, /* min time */
- 120 /* max time */
- };
- long random_values_max[] = {
- 255, /* min level */
- 255, /* max level */
- 9999, /* min period */
- 9999, /* max period */
- 9999, /* min time */
- 9999, /* max time */
- };
- char* menu_random[] = {
- "MinLvl:", "MaxLvl:", "MinTime:", "MaxTime:", "MinPeriod:", "MaxPeriod:"};
- int count_items_random = 6;
- /*---------------------------*/
- /*-------Settings Menu-------*/
- int header_settings = 0;
- long start_delay = 0;
- int screen_brightness = 255;
- int button_wait_time_short = 60;
- int button_wait_time_long = 250;
- const int button_wait_time_short_min = 10;
- const int button_wait_time_long_min = 10;
- const long start_delay_max = 9999;
- const int screen_brightness_max = 255;
- const int button_wait_time_short_max = 1000;
- const int button_wait_time_long_max = 1000;
- char* menu_settings[] = {
- "StartDlay:", "ScrnLvl:", "BtnDlyShrt:", "BtnDlayLng:"
- };
- int count_items_settings = 4;
- /*---------------------------*/
- 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;
- }
- boolean start_delay_countdown(){
- long random_wave_output;
- long random_wave_temp;
- long time_left;
- boolean cancel_delay = false;
- unsigned long random_start_time = millis();
- if(start_delay != 0){
- lcd.setCursor(0,0);
- lcd.print(String(" "));
- lcd.setCursor(0,1);
- lcd.print(String(" "));
- lcd.setCursor(0,0);
- lcd.print(String("START IN: "));
- analogWrite(vibrator_pin, 0);
- do{
- time = millis();
- time_left = (start_delay * 1000 + random_start_time - time) / 1000;
- lcd.setCursor(10,0);
- lcd.print( String( time_left ) + String(" ") );
- button = ReadButtons();
- }
- while(button != BUTTON_SELECT && start_delay * 1000 + random_start_time > time);
- if(button == BUTTON_SELECT){
- cancel_delay = true;
- }
- }
- return( cancel_delay );
- }
- void display(int display_header, char* display_items[], long display_values[], int display_count, boolean display_type){
- if(update || flash() != prev_flash){
- lcd.setCursor(0,0);
- lcd.print(String(" "));
- lcd.setCursor(0,1);
- lcd.print(String(" "));
- //top row
- lcd.setCursor(0,0);
- if(display_header == 0 && display_type){
- arrow_print(display_items[0], type_wave[wave_values[0]]);
- }
- else if(display_header == display_count){
- start_print();
- }
- else{
- arrow_print(display_items[display_header], String(display_values[display_header]));
- }
- //bottom row
- lcd.setCursor(0,1);
- if(display_header + 1 > display_count){
- //do nothing
- }
- else if(display_header + 1 == display_count){
- lcd.print(String(start));
- }
- else{
- lcd.print(String(display_items[display_header + 1] + String(" ") + String(display_values[display_header + 1])));
- };
- prev_flash = flash();
- update = false;
- }
- }
- void display_menu_constant(){
- delay(button_wait_time_long);
- do{
- 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( button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- //Increment Selection
- if(header_constant == 0 && constant_level + 1 <= output_max){
- constant_level++;
- }
- delay(button_wait_time_short);
- break;
- }
- case BUTTON_UP:
- {
- if(header_constant-1 >= 0){
- header_constant -= 1;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_DOWN:
- {
- if(header_constant+1 <= count_items_constant){
- header_constant++;
- }
- 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);
- break;
- }
- case BUTTON_SELECT:
- {
- if(header_constant == count_items_constant){
- delay(button_wait_time_long);
- vibrator_constant(constant_level);
- }
- delay(button_wait_time_long);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- while(!(button == BUTTON_SELECT && header_constant != count_items_constant));
- return;
- }
- void vibrator_constant(int constant_level_output){
- if(start_delay_countdown()){
- return;
- }
- 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);
- do{
- button = ReadButtons();
- display(header_wave, menu_wave, wave_values, count_items_wave, true);
- switch( button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- //Increment Selection
- if(header_wave == 0 && wave_values[0] + 1 <= wave_values_max[0]){
- wave_values[0]++;
- }
- else if(header_wave == 1 && wave_values[1] + 1 < wave_values[2]){
- wave_values[1]++;
- }
- else if(header_wave == 2 && wave_values[2] + 1 <= wave_values_max[2]){
- wave_values[2]++;
- }
- else if(header_wave == 3 && wave_values[3] + 1 <= wave_values_max[3]){
- wave_values[3]++;
- }
- delay(button_wait_time_short);
- break;
- }
- case BUTTON_UP:
- {
- if(header_wave - 1 >= 0){
- header_wave -= 1;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_DOWN:
- {
- if(header_wave + 1 <= count_items_wave){
- header_wave++;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_LEFT:
- {
- //Decrement Selection
- if(header_wave == 0 && wave_values[0] - 1 >= 0){
- wave_values[0] -= 1;
- }
- else if(header_wave == 1 && wave_values[1] - 1 >= 0){
- wave_values[1] -= 1;
- }
- else if(header_wave == 2 && wave_values[2] - 1 > wave_values[1]){
- wave_values[2] -= 1;
- }
- else if(header_wave == 3 && wave_values[3] - 1 >= 1){
- wave_values[3] -= 1;
- }
- delay(button_wait_time_short);
- break;
- }
- case BUTTON_SELECT:
- {
- if(header_wave == count_items_wave){
- delay(button_wait_time_long);
- vibrator_wave(wave_values[0], wave_values[1], wave_values[2], wave_values[3]);
- }
- delay(button_wait_time_long);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- while(!(button == BUTTON_SELECT && header_wave != count_items_wave));
- return;
- }
- void vibrator_wave(long vibrator_wave_type, long vibrator_wave_minimum, long vibrator_wave_maximum, long vibrator_wave_frequency){
- if(start_delay_countdown()){
- return;
- }
- 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{
- 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(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{
- 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(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{
- 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(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{
- 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(button != BUTTON_SELECT);
- analogWrite(vibrator_pin, 0);
- }
- return;
- }
- void display_menu_wave_waves(){
- delay(button_wait_time_long);
- do{
- button = ReadButtons();
- display(header_wave_waves, menu_wave_waves, wave_waves_values, count_items_wave_waves, false);
- switch( button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- if(header_wave_waves == 0 && wave_waves_values[0] + 1 < wave_waves_values[1]){
- wave_waves_values[0]++;
- }
- else if(header_wave_waves == 1 && wave_waves_values[1] + 1 <= output_max){
- wave_waves_values[1]++;
- }
- else if(header_wave_waves == 2 && wave_waves_values[2] + 1 <= wave_waves_values_max[2]){
- wave_waves_values[2]++;
- }
- else if(header_wave_waves == 3 && wave_waves_values[3] + 1 <= wave_waves_values_max[2]){
- wave_waves_values[3]++;
- }
- delay(button_wait_time_short);
- break;
- }
- case BUTTON_UP:
- {
- if(header_wave_waves - 1 >= 0){
- header_wave_waves -= 1;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_DOWN:
- {
- if(header_wave_waves + 1 <= count_items_wave_waves){
- header_wave_waves += 1;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_LEFT:
- {
- if(header_wave_waves == 0 && wave_waves_values[0] - 1 >= 0){
- wave_waves_values[0] -= 1;
- }
- else if(header_wave_waves == 1 && wave_waves_values[1] - 1 > wave_waves_values[0]){
- wave_waves_values[1] -= 1;
- }
- else if(header_wave_waves == 2 && wave_waves_values[2] - 1 > 0){
- wave_waves_values[2] -= 1;
- }
- else if(header_wave_waves == 3 && wave_waves_values[3] - 1 > 0){
- wave_waves_values[3] -= 1;
- }
- delay(button_wait_time_short);
- break;
- }
- case BUTTON_SELECT:
- {
- if(header_wave_waves == count_items_wave_waves){
- delay(button_wait_time_long);
- vibrate_wave_waves(wave_waves_values[0], wave_waves_values[1], wave_waves_values[2], wave_waves_values[3]);
- }
- delay(button_wait_time_long);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- while(!(button == BUTTON_SELECT && header_wave_waves != count_items_wave_waves));
- 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){
- if(start_delay_countdown()){
- return;
- }
- 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(10,0);
- lcd.print(String("MP:") + String(vibrate_wave_waves_multiplier));
- lcd.setCursor(0,1);
- lcd.print(String("P:") + String(vibrate_wave_waves_wavelength));
- lcd.setCursor(11,1);
- lcd.print(String("V:"));
- do{
- time = millis();
- 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,1);
- lcd.print(String(vibrate_wave_waves_output) + String(" "));
- analogWrite(vibrator_pin, vibrate_wave_waves_output);
- }
- while(button != BUTTON_SELECT);
- analogWrite(vibrator_pin, 0);
- return;
- }
- void display_menu_random(){
- delay(button_wait_time_long);
- do{
- button = ReadButtons();
- display(header_random, menu_random, random_values, count_items_random, false);
- switch( button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- if(header_random == 0 && random_values[0] + 1 < random_values[1]){
- random_values[0]++;
- }
- else if(header_random == 1 && random_values[1] + 1 <= output_max){
- random_values[1]++;
- }
- else if(header_random == 2 && random_values[2] + 1 <= random_values[3]){
- random_values[2]++;
- }
- else if(header_random == 3 && random_values[3] + 1 <= random_values_max[3]){
- random_values[3]++;
- }
- else if(header_random == 4 && random_values[4] + 1 < random_values_max[4]){
- random_values[4]++;
- }
- else if(header_random == 5 && random_values[5] + 1 <= random_values_max[5]){
- random_values[5]++;
- }
- delay(button_wait_time_short);
- break;
- }
- case BUTTON_UP:
- {
- if(header_random - 1 >= 0){
- header_random -= 1;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_DOWN:
- {
- if(header_random + 1 <= count_items_random){
- header_random += 1;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_LEFT:
- {
- if(header_random == 0 && random_values[0] - 1 >= 0){
- random_values[0] -= 1;
- }
- else if(header_random == 1 && random_values[1] - 1 > random_values[0]){
- random_values[1] -= 1;
- }
- else if(header_random == 2 && random_values[2] - 1 >= 1){
- random_values[2] -= 1;
- }
- else if(header_random == 3 && random_values[3] - 1 > random_values[2]){
- random_values[3] -= 1;
- }
- else if(header_random == 4 && random_values[4] - 1 >= 1){
- random_values[4] -= 1;
- }
- else if(header_random == 5 && random_values[5] - 1 > random_values[4]){
- random_values[5] -= 1;
- }
- delay(button_wait_time_short);
- break;
- }
- case BUTTON_SELECT:
- {
- if(header_random == count_items_random){
- delay(button_wait_time_long);
- sub_random(random_values[0], random_values[1], random_values[2], random_values[3], random_values[4], random_values[5]);
- }
- delay(button_wait_time_long);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- while(!(button == BUTTON_SELECT && header_random != count_items_random));
- 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){
- if(start_delay_countdown()){
- return;
- }
- 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;
- long sub_random_multiplier;
- 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_multiplier = random(3, 30);
- sub_random_mode = random(1, 8);
- 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);
- }
- 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, long random_multiplier){
- 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 = millis();
- time_left = (random_wait + random_start_time - time) / 1000;
- button = ReadButtons();
- 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(button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(button == BUTTON_SELECT){
- random_cancel = true;
- }
- }
- 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 = millis();
- time_left = (random_wait + random_start_time - time) / 1000;
- button = ReadButtons();
- 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(button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(button == BUTTON_SELECT){
- random_cancel = true;
- }
- }
- 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 = millis();
- time_left = (random_wait + random_start_time - time) / 1000;
- button = ReadButtons();
- 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(button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(button == BUTTON_SELECT){
- random_cancel = true;
- }
- }
- 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));
- random_cancel = false;
- do{
- time = millis();
- time_left = (random_wait + random_start_time - time) / 1000;
- button = ReadButtons();
- 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(button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(button == BUTTON_SELECT){
- random_cancel = true;
- }
- }
- else if(random_wave_type == 5){
- //off
- lcd.setCursor(0,0);
- lcd.print(String("OFF FOR: "));
- analogWrite(vibrator_pin, 0);
- do{
- time = millis();
- time_left = (random_wait + random_start_time - time) / 1000;
- lcd.setCursor(9,0);
- lcd.print( String( time_left ) + String(" ") );
- button = ReadButtons();
- }
- while(button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(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 = millis();
- time_left = (random_wait + random_start_time - time) / 1000;
- lcd.setCursor(5,1);
- lcd.print( String( time_left ) + String(" ") );
- button = ReadButtons();
- }
- while(button != BUTTON_SELECT && random_wait + random_start_time > time);
- if(button == BUTTON_SELECT){
- random_cancel = true;
- }
- }
- else if(random_wave_type == 7){
- //wave of waves
- lcd.setCursor(0,0);
- lcd.print(String(" WOW: ") + String(random_wave_minimum) + String("-") + String(random_wave_maximum));
- lcd.setCursor(0,1);
- lcd.print(String("P:") + String(random_wave_frequency) + String("M:") + String(random_multiplier));
- do{
- button = ReadButtons();
- time = millis();
- time_left = (random_wait + random_start_time - time) / 1000;
- lcd.setCursor(0,0);
- lcd.print(String(time_left));
- 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 );
- analogWrite(vibrator_pin, random_wave_output);
- lcd.setCursor(8,1);
- lcd.print(String(" LVL:") + String(random_wave_output) + String(" "));
- }
- while(button != BUTTON_SELECT && random_wait + random_start_time > time);
- }
- analogWrite(vibrator_pin, 0);
- return(random_cancel);
- }
- void display_menu_settings(){
- delay(button_wait_time_long);
- do{
- button = ReadButtons();
- if(update || flash() != prev_flash){
- for(int x = 0; x < 2; x++){
- if(header_settings + x > count_items_settings){
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- }
- else{
- lcd.setCursor(0, x);
- lcd.print(String(" "));
- lcd.setCursor(0, x);
- if(header_settings + x == 0){
- if(header_settings == 0){
- arrow_print(menu_settings[0], String(start_delay));
- }
- else{
- lcd.print(String(menu_settings[0]) + String(" ") + String(start_delay));
- }
- }
- else if(header_settings + x == 1){
- if(header_settings == 1){
- arrow_print(menu_settings[1], String(screen_brightness));
- }
- else{
- lcd.print(String(menu_settings[1]) + String(" ") + String(screen_brightness));
- }
- }
- else if(header_settings + x == 2){
- if(header_settings == 2){
- arrow_print(menu_settings[2], String(button_wait_time_short));
- }
- else{
- lcd.print(String(menu_settings[2]) + String(" ") + String(button_wait_time_short));
- }
- }
- else if(header_settings + x == 3){
- if(header_settings == 3){
- arrow_print(menu_settings[3], String(button_wait_time_long));
- }
- else{
- lcd.print(String(menu_settings[3]) + String(" ") + String(button_wait_time_long));
- }
- }
- }
- }
- prev_flash = flash();
- update = false;
- }
- switch( button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- //Increment Selection
- if(header_settings == 0 && start_delay + 1 <= start_delay_max){
- start_delay++;
- }
- else if(header_settings == 1 && screen_brightness + 1 <= screen_brightness_max){
- screen_brightness++;
- LCD_BACKLIGHT_LEVEL(screen_brightness);
- }
- else if(header_settings == 2 && button_wait_time_short + 1 <= button_wait_time_short_max){
- button_wait_time_short++;
- }
- else if(header_settings == 3 && button_wait_time_long + 1 <= button_wait_time_long_max){
- button_wait_time_long++;
- }
- delay(button_wait_time_short);
- break;
- }
- case BUTTON_UP:
- {
- if(header_settings - 1 >= 0){
- header_settings -= 1;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_DOWN:
- {
- if(header_settings + 1 < count_items_settings){
- header_settings++;
- }
- delay(button_wait_time_long);
- break;
- }
- case BUTTON_LEFT:
- {
- //Decrement Selection
- if(header_settings == 0 && start_delay - 1 >= 0){
- start_delay -= 1;
- }
- else if(header_settings == 1 && screen_brightness - 1 >= 0){
- screen_brightness -= 1;
- LCD_BACKLIGHT_LEVEL(screen_brightness);
- }
- else if(header_settings == 2 && button_wait_time_short - 1 >= button_wait_time_short_min){
- button_wait_time_short -= 1;
- }
- else if(header_settings == 3 && button_wait_time_long - 1 >= button_wait_time_long_min){
- button_wait_time_long -= 1;
- }
- delay(button_wait_time_short);
- break;
- }
- case BUTTON_SELECT:
- {
- //No Action
- delay(button_wait_time_long);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- while(!(button == BUTTON_SELECT && header_constant != count_items_constant));
- return;
- }
- 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 );
- lcd.setCursor(0,0);
- lcd.print(String("Wave Gen V6"));
- lcd.setCursor(0,1);
- lcd.print(String("By Clarkey"));
- delay(1000);
- }
- void loop()
- {
- //DISPLAY MAIN MENU
- if(update || 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]));
- }
- }
- update = false;
- prev_flash = flash();
- }
- button = ReadButtons();
- switch( 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;
- case 4:
- display_menu_settings();
- break;
- }
- delay(button_wait_time_long);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- 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;
- if(button != BUTTON_NONE){
- update = true;
- }
- return( button );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement