Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. /********************************************************
  2. * 16x8 LED Matrix Message for Ham Radio *
  3. *********************************************************
  4. **********************************************************/
  5.  
  6. //Libraries
  7. #include <Wire.h>
  8. #include "Adafruit_LEDBackpack.h"
  9. #include "Adafruit_GFX.h"
  10.  
  11. //Library initialization
  12. Adafruit_8x16matrix matrix = Adafruit_8x16matrix();
  13.  
  14. //Constants
  15. #define APPLAUSE_ITER 1 //This is the number of times the "applause" message will scroll
  16. #define DELAY_TIME1 45 //Amount of milliseconds for the delay (on air message)
  17. #define DELAY_TIME2 60 //Amount of milliseconds for the delay (applause and stand by messages)
  18.  
  19. //Pin definitions
  20. #define buttonPin 2 //The pin used for the button
  21.  
  22. //Variables
  23. int buttonState = 0; //Variable for saving the state of the button, If the button is pressed, the value is 1, if not, it is 0 (button needs a 10kOhm pull down resistor)
  24. int lastButtonState = 0; //Variable for saving the previous state of the button
  25. boolean stdByState = false; //Boolean variable for the "stand by" message (can be either true or false, initializes as false)
  26.  
  27. void setup() {
  28. Serial.begin(9600);
  29. Serial.println("16x8 LED Matrix Test");
  30. matrix.begin(0x70); // pass in the address
  31. pinMode(buttonPin, INPUT); //Stablish the pin as an input
  32.  
  33. //Moved this part of the code to the setup because if it's always going to be the same, it just needs to be executed one time
  34. matrix.setBrightness(1);
  35. matrix.setTextSize(1);
  36. matrix.setTextWrap(false); // we don't want text to wrap so it scrolls nicely
  37. matrix.setTextColor(LED_ON);
  38. matrix.setRotation(1);
  39. }
  40.  
  41. void loop() {
  42. buttonState = digitalRead(buttonPin); //Read the button value
  43. if (!buttonState && lastButtonState){ //If button is currently not pressed but the last time it was
  44. applauseMessage();
  45. }
  46. lastButtonState = buttonState; //Save last button value
  47. if (buttonState) { //if button is pressed, call the on air message function
  48. onAirMessage();
  49. }
  50. else if (stdByState && !buttonState) { //if button has already been pressed but isn't currently
  51. standByMessage();
  52. }
  53. else {
  54. matrix.clear();
  55. matrix.writeDisplay();
  56. }
  57. }
  58.  
  59. void onAirMessage() {
  60. stdByState = true; //when the button is pressed "stand by" state becomes true
  61. matrix.blinkRate(0);
  62. for (int8_t x=16; x>=-56; x--) {
  63. buttonState = digitalRead(buttonPin); //Read the button value in each iteration of the for loop
  64. if (buttonState) { //if when inside the for loop the button is still pressed stay on the loop and print on the matrix
  65. matrix.clear();
  66. matrix.setCursor(x,0);
  67. matrix.print("-ON AIR!-");
  68. matrix.writeDisplay();
  69. delay(DELAY_TIME1);
  70. }
  71. else { //else, if the button is no longer pressed, clear the matrix, exit the loop and call applauseMessage function
  72. matrix.clear();
  73. matrix.writeDisplay();
  74. //applauseMessage();
  75. break;
  76. }
  77. }
  78. }
  79.  
  80. void applauseMessage() {
  81. matrix.blinkRate(1);
  82. for (int x=0; x<APPLAUSE_ITER; x++) {
  83. for (int8_t x=16; x>=-56; x--) {
  84. buttonState = digitalRead(buttonPin);
  85. if (!buttonState) {
  86. matrix.clear();
  87. matrix.setBrightness(2);
  88. matrix.setCursor(x,0);
  89. matrix.print("Applause");
  90. matrix.writeDisplay();
  91. delay(DELAY_TIME2);
  92. }
  93. else {
  94. matrix.clear();
  95. matrix.writeDisplay();
  96. break;
  97. }
  98. }
  99. if (buttonState) break;
  100. }
  101. }
  102.  
  103. void standByMessage() {
  104. matrix.blinkRate(0);
  105. for (int8_t x=16; x>=-56; x--) {
  106. buttonState = digitalRead(buttonPin);
  107. if (!buttonState) { //if button is not pressed stay on loop
  108. matrix.clear();
  109. matrix.setBrightness(1);
  110. matrix.setCursor(x,0);
  111. matrix.print("-STANDBY-");
  112. matrix.writeDisplay();
  113. delay(DELAY_TIME2);
  114. }
  115. else { //else, if the button is pressed, clear the matrix and exit the loop
  116. matrix.clear();
  117. matrix.writeDisplay();
  118. break;
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement