SHARE
TWEET

Arduino led pulse gun thingy

bobmarley12345 Jun 12th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2.  
  3. // select the pins used on the LCD panel
  4. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  5.  
  6. // define some values used by the panel and buttons
  7. int lcd_key     =     0;
  8. int adc_key_in  =     0;
  9. #define btnRIGHT      0
  10. #define btnUP         1
  11. #define btnDOWN       2
  12. #define btnLEFT       3
  13. #define btnSELECT     4
  14. #define btnNONE       5
  15. #define SelctionDelay 250
  16. #define MAX_PWM       10000
  17. #define LedPin        17
  18. #define ButtonDelay   50 //ms
  19.  
  20. int current_pwm = 0;
  21. int a1read = 0;
  22.  
  23. // read the buttons
  24. int read_LCD_buttons()
  25. {
  26.   adc_key_in = analogRead(0);      // read the value from the sensor
  27.   // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  28.   // we add approx 50 to those values and check to see if we are close
  29.   if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  30.   if (adc_key_in < 50)   return btnRIGHT;
  31.   if (adc_key_in < 250)  return btnUP;
  32.   if (adc_key_in < 450)  return btnDOWN;
  33.   if (adc_key_in < 650)  return btnLEFT;
  34.   if (adc_key_in < 850)  return btnSELECT;
  35.   return btnNONE;  // when all others fail, return this...
  36. }
  37.  
  38. void setup()
  39. {
  40.   lcd.begin(16, 2);
  41.   pinMode(A3, OUTPUT);
  42.   Up();
  43. }
  44. int ledstat = LOW;
  45. long beforemills = 0;
  46. void loop()
  47. {
  48.   lcd_key = read_LCD_buttons();
  49.   switch (lcd_key)
  50.   {
  51.     case btnDOWN:   Down();   break;
  52.     case btnLEFT:   Left();   break;
  53.     case btnNONE:   None();   break;
  54.     case btnRIGHT:  Right();  break;
  55.     case btnSELECT: Select(); break;
  56.     case btnUP:     Up();     break;
  57.   }
  58.   doStuff();
  59. }
  60. long prevMils = 0;
  61. void doStuff()
  62. {
  63.   long crnt_mills = millis();
  64.   if (crnt_mills - prevMils >= ((1000 / current_pwm) / 2))
  65.   {
  66.     prevMils = crnt_mills;
  67.     if (ledstat == LOW)
  68.     {
  69.       ledstat = HIGH;
  70.     }
  71.     else
  72.     {
  73.        ledstat = LOW;
  74.     }
  75.  
  76.     digitalWrite(LedPin, ledstat);
  77.   }
  78. }
  79. void Up()
  80. {
  81.   if (current_pwm < MAX_PWM)
  82.   {
  83.     current_pwm += 1;
  84.     delay(ButtonDelay);
  85.     Update();
  86.   }
  87. }
  88. void Down()
  89. {
  90.   if (current_pwm > 1)
  91.   {
  92.     current_pwm = current_pwm - 1;
  93.     delay(ButtonDelay);
  94.     Update();
  95.   }
  96. }
  97. void Left()
  98. {
  99.   if (current_pwm > 10)
  100.   {
  101.     current_pwm = current_pwm - 10;
  102.     delay(ButtonDelay);
  103.     Update();
  104.   }
  105. }
  106. void Right()
  107. {
  108.   if (current_pwm < MAX_PWM)
  109.   {
  110.     current_pwm += 10;
  111.     delay(ButtonDelay);
  112.     Update();
  113.   }
  114. }
  115. void None()
  116. {
  117.  
  118. }
  119. void Select()
  120. {
  121.  
  122. }
  123. void Update()
  124. {
  125.   lcd.clear();
  126.   lcd.setCursor(0, 0);
  127.   lcd.print("Delay: " + String((1000 / current_pwm) / 2) + "ms");
  128.   lcd.setCursor(0, 1);
  129.   lcd.print("Freq : " + String(current_pwm) + "hz");
  130. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top