Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- // select the pins used on the LCD panel
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
- // define some values used by the panel and buttons
- int lcd_key = 0;
- int adc_key_in = 0;
- #define btnRIGHT 0
- #define btnUP 1
- #define btnDOWN 2
- #define btnLEFT 3
- #define btnSELECT 4
- #define btnNONE 5
- #define SelctionDelay 250
- #define MAX_PWM 10000
- #define LedPin 17
- #define ButtonDelay 50 //ms
- int current_pwm = 0;
- int a1read = 0;
- // read the buttons
- int read_LCD_buttons()
- {
- adc_key_in = analogRead(0); // read the value from the sensor
- // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
- // we add approx 50 to those values and check to see if we are close
- if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 250) return btnUP;
- if (adc_key_in < 450) return btnDOWN;
- if (adc_key_in < 650) return btnLEFT;
- if (adc_key_in < 850) return btnSELECT;
- return btnNONE; // when all others fail, return this...
- }
- void setup()
- {
- lcd.begin(16, 2);
- pinMode(A3, OUTPUT);
- Up();
- }
- int ledstat = LOW;
- long beforemills = 0;
- void loop()
- {
- lcd_key = read_LCD_buttons();
- switch (lcd_key)
- {
- case btnDOWN: Down(); break;
- case btnLEFT: Left(); break;
- case btnNONE: None(); break;
- case btnRIGHT: Right(); break;
- case btnSELECT: Select(); break;
- case btnUP: Up(); break;
- }
- doStuff();
- }
- long prevMils = 0;
- void doStuff()
- {
- long crnt_mills = millis();
- if (crnt_mills - prevMils >= ((1000 / current_pwm) / 2))
- {
- prevMils = crnt_mills;
- if (ledstat == LOW)
- {
- ledstat = HIGH;
- }
- else
- {
- ledstat = LOW;
- }
- digitalWrite(LedPin, ledstat);
- }
- }
- void Up()
- {
- if (current_pwm < MAX_PWM)
- {
- current_pwm += 1;
- delay(ButtonDelay);
- Update();
- }
- }
- void Down()
- {
- if (current_pwm > 1)
- {
- current_pwm = current_pwm - 1;
- delay(ButtonDelay);
- Update();
- }
- }
- void Left()
- {
- if (current_pwm > 10)
- {
- current_pwm = current_pwm - 10;
- delay(ButtonDelay);
- Update();
- }
- }
- void Right()
- {
- if (current_pwm < MAX_PWM)
- {
- current_pwm += 10;
- delay(ButtonDelay);
- Update();
- }
- }
- void None()
- {
- }
- void Select()
- {
- }
- void Update()
- {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Delay: " + String((1000 / current_pwm) / 2) + "ms");
- lcd.setCursor(0, 1);
- lcd.print("Freq : " + String(current_pwm) + "hz");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement