Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. // Program
  2. #include <LiquidCrystal.h> //Dołączenie bilbioteki
  3. LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Informacja o podłączeniu nowego wyświetlacza
  4.  
  5. #define BTN_PIN A0
  6.  
  7. #define BTN1 1023
  8. #define BTN2 911
  9. #define BTN3 797
  10. #define BTN4 683
  11. #define BTN5 572
  12. #define BTN6 461
  13. #define BTN7 333
  14. #define BTN8 222
  15.  
  16. int latchPin = 8;
  17. //Pin connected to SH_CP of 74HC595
  18. int clockPin = 12;
  19. ////Pin connected to DS of 74HC595
  20. int dataPin = 11;
  21.  
  22. //holder for infromation you're going to pass to shifting function
  23. byte data = 0;
  24.  
  25. bool isAnalogBtnPressed(int analogVal, int btnValue) {
  26. return abs(btnValue - analogVal) <= 20;
  27. }
  28.  
  29.  
  30. int odczytanaWartosc = 0;
  31.  
  32. void setup() {
  33. pinMode(BTN_PIN, INPUT);
  34. Serial.begin(9600);
  35. pinMode(latchPin, OUTPUT);
  36. pinMode(clockPin, OUTPUT);
  37. pinMode(dataPin, OUTPUT);
  38. lcd.begin(16, 2); //Deklaracja typu
  39.  
  40.  
  41. }
  42.  
  43. void loop() {
  44. int btnData = analogRead(BTN_PIN);
  45. if(isAnalogBtnPressed(btnData, BTN1)) {
  46. Serial.println("Wcisnieto przycisk 1");
  47. lcd.setCursor(0, 0); //Ustawienie kursora
  48. lcd.print("Wcisnieto 1"); //Wyświetlenie tekstu
  49. lightShiftPinA(0);
  50. delay(1000);
  51. } else if(isAnalogBtnPressed(btnData, BTN2)) {
  52. Serial.println("Wcisnieto przycisk 2");
  53. lcd.setCursor(0, 0); //Ustawienie kursora
  54. lcd.print("Wcisnieto 2"); //Wyświetlenie tekstu
  55. lightShiftPinA(1);
  56. delay(1000);
  57. } else if(isAnalogBtnPressed(btnData, BTN3)) {
  58. Serial.println("Wcisnieto przycisk 3");
  59. lcd.setCursor(0, 0); //Ustawienie kursora
  60. lcd.print("Wcisnieto 3"); //Wyświetlenie tekstu
  61. lightShiftPinA(2);
  62. delay(1000);
  63. } else if(isAnalogBtnPressed(btnData, BTN4)) {
  64. Serial.println("Wcisnieto przycisk 4");
  65. lcd.setCursor(0, 0); //Ustawienie kursora
  66. lcd.print("Wcisnieto 4"); //Wyświetlenie tekstu
  67. lightShiftPinA(3);
  68. delay(1000);
  69. }
  70. else if(isAnalogBtnPressed(btnData, BTN5)) {
  71. Serial.println("Wcisnieto przycisk 5");
  72. lcd.setCursor(0, 0); //Ustawienie kursora
  73. lcd.print("Wcisnieto 5"); //Wyświetlenie tekstu
  74. lightShiftPinA(4);
  75. delay(1000);
  76. } else if(isAnalogBtnPressed(btnData, BTN6)) {
  77. Serial.println("Wcisnieto przycisk 6");
  78. lcd.setCursor(0, 0); //Ustawienie kursora
  79. lcd.print("Wcisnieto 6"); //Wyświetlenie tekstu
  80. lightShiftPinA(5);
  81. delay(1000);
  82. } else if(isAnalogBtnPressed(btnData, BTN7)) {
  83. Serial.println("Wcisnieto przycisk 7");
  84. lcd.setCursor(0, 0); //Ustawienie kursora
  85. lcd.print("Wcisnieto 7"); //Wyświetlenie tekstu
  86. lightShiftPinA(6);
  87. delay(1000);
  88. }
  89. else if(isAnalogBtnPressed(btnData, BTN8)) {
  90. Serial.println("Wcisnieto przycisk 8");
  91. lcd.setCursor(0, 0); //Ustawienie kursora
  92. lcd.print("Wcisnieto 8"); //Wyświetlenie tekstu
  93. lightShiftPinA(7);
  94. delay(1000);
  95. }
  96. delay(100);
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. //This function uses bitwise math to move the pins up
  105. void lightShiftPinA(int p) {
  106. //defines a local variable
  107. int pin;
  108.  
  109. //this is line uses a bitwise operator
  110. //shifting a bit left using << is the same
  111. //as multiplying the decimal number by two.
  112. pin = 1<< p;
  113.  
  114. //ground latchPin and hold low for as long as you are transmitting
  115. digitalWrite(latchPin, LOW);
  116. //move 'em out
  117. shiftOut(dataPin, clockPin, MSBFIRST, pin);
  118. //return the latch pin high to signal chip that it
  119. //no longer needs to listen for information
  120. digitalWrite(latchPin, HIGH);
  121.  
  122. }
  123.  
  124. //This function uses that fact that each bit in a byte
  125. //is 2 times greater than the one before it to
  126. //shift the bits higher
  127. void lightShiftPinB(int p) {
  128. //defines a local variable
  129. int pin;
  130.  
  131. //start with the pin = 1 so that if 0 is passed to this
  132. //function pin 0 will light.
  133. pin = 1;
  134.  
  135. for (int x = 0; x < p; x++) {
  136. pin = pin * 2;
  137. }
  138.  
  139. //ground latchPin and hold low for as long as you are transmitting
  140. digitalWrite(latchPin, LOW);
  141. //move 'em out
  142. shiftOut(dataPin, clockPin, MSBFIRST, pin);
  143. //return the latch pin high to signal chip that it
  144. //no longer needs to listen for information
  145. digitalWrite(latchPin, HIGH);
  146.  
  147. }
  148.  
  149. //blinks the whole register based on the number of times you want to
  150. //blink "n" and the pause between them "d"
  151. //starts with a moment of darkness to make sure the first blink
  152. //has its full visual effect.
  153. void blinkAll(int n, int d) {
  154. digitalWrite(latchPin, LOW);
  155. shiftOut(dataPin, clockPin, MSBFIRST, 0);
  156. digitalWrite(latchPin, HIGH);
  157. delay(200);
  158. for (int x = 0; x < n; x++) {
  159. digitalWrite(latchPin, LOW);
  160. shiftOut(dataPin, clockPin, MSBFIRST, 255);
  161. digitalWrite(latchPin, HIGH);
  162. delay(d);
  163. digitalWrite(latchPin, LOW);
  164. shiftOut(dataPin, clockPin, MSBFIRST, 0);
  165. digitalWrite(latchPin, HIGH);
  166. delay(d);
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement