Menu
Index
Engineering
Entertainment
Literature
Miscellaneous
Contact
Search
tiktok.com/@pkvi.xyz
buymeacoffee.com/pkvi
scored.co/u/pkvi_halfkdeltaxsq
Miter
Why Ayh?
Miter
@pkvi
"...may not meet professional standards."
371 miters
122 tenons
Subscribe
0.00171
Arduino Coin Jar


Arduino Nano
Elegoo PIR Motion Sensor Module
Elegoo Keypad
Elegoo Piezo Module
Elegoo RGB LED Module
Elegoo Relay Module
MG995 Servo
Transformer
Florescent Ballast
Neoprene Gasket Sheet



  1. // Arduino Coin Jar
  2. // pkvi
  3. #include <Keypad.h>
  4. #include <Password.h>
  5. #include <VarSpeedServo.h>
  6. VarSpeedServo jar;
  7. // Pins
  8. int piezo = A2;
  9. int red = A3;
  10. int green = A4;
  11. int blue = A5;
  12. int relay = 2;
  13. int jar_pin = 3;
  14. int pir = 4;
  15. // Keypad
  16. const byte ROWS = 4;
  17. const byte COLS = 4;
  18. char keys[ROWS][COLS] = {
  19. {'1', '2', '3', 'A'},
  20. {'4', '5', '6', 'B'},
  21. {'7', '8', '9', 'C'},
  22. {'*', '0', '#', 'D'}
  23. };
  24. byte rowPins[ROWS] = {5, 6, 7, 8};
  25. byte colPins[COLS] = {9, 10, 11, 12};
  26. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  27. // Set Passcode
  28. Password passcode = Password( "947" );
  29. // States
  30. int closed = 88;
  31. int opened = 30;
  32. int open_state = 0;
  33. int pir_state = 0;
  34. int motion;
  35. void setup() {
  36. Serial.begin(9600);
  37. // Pin Placements
  38. jar.attach(jar_pin);
  39. pinMode(piezo, OUTPUT);
  40. pinMode(red, OUTPUT);
  41. pinMode(green, OUTPUT);
  42. pinMode(blue, OUTPUT);
  43. pinMode(relay, OUTPUT);
  44. pinMode(pir, INPUT);
  45. // Keypad Listener
  46. keypad.addEventListener(keypadEvent);
  47. // Close Jar
  48. jar.write(closed, 200, true);
  49. // No Buzz
  50. noTone(piezo);
  51. // No Zap
  52. digitalWrite(relay, LOW);
  53. // Activate
  54. // Form of Coin Jar
  55. while (millis() < 2000)
  56. {
  57. digitalWrite(blue, HIGH);
  58. delay(50);
  59. digitalWrite(blue, LOW);
  60. delay(50);
  61. }
  62. digitalWrite(green, LOW);
  63. digitalWrite(red, HIGH);
  64. pir_state = 1;
  65. }
  66. void loop() {
  67. keypad.getKey();
  68. motion_sensor();
  69. }
  70. void keypadEvent(KeypadEvent eKey) {
  71. switch (keypad.getState()) {
  72. case PRESSED:
  73. Serial.println(eKey);
  74. button();
  75. switch (eKey) {
  76. case '#': check_passcode(); break;
  77. case '*': passcode.reset(); break;
  78. default: passcode.append(eKey);
  79. }
  80. }
  81. }
  82. void check_passcode() {
  83. if (passcode.evaluate()) {
  84. if (open_state == 0) {
  85. open_coin_jar();
  86. passcode.reset();
  87. } else if (open_state == 1) {
  88. close_coin_jar();
  89. passcode.reset();
  90. }
  91. } else {
  92. digitalWrite(blue, LOW);
  93. digitalWrite(green, LOW);
  94. while (millis() < 2000) {
  95. digitalWrite(red, HIGH);
  96. delay(50);
  97. digitalWrite(red, LOW);
  98. delay(50);
  99. }
  100. }
  101. }
  102. void open_coin_jar() {
  103. pir_state = 0;
  104. digitalWrite(green, HIGH);
  105. digitalWrite(red, LOW);
  106. digitalWrite(relay, LOW);
  107. jar.write(opened, 200, true);
  108. open_state = 1;
  109. }
  110. void close_coin_jar() {
  111. jar.write(closed, 200, true);
  112. open_state = 0;
  113. pir_state = 1;
  114. }
  115. void motion_sensor() {
  116. if (pir_state == 1) {
  117. digitalWrite(red, HIGH);
  118. digitalWrite(green, LOW);
  119. motion = digitalRead(pir);
  120. if (motion == HIGH) {
  121. digitalWrite(relay, HIGH);
  122. delay(1000);
  123. digitalWrite(relay, LOW);
  124. }
  125. } else if (pir_state == 0) {
  126. digitalWrite(red, LOW);
  127. digitalWrite(green, HIGH);
  128. digitalWrite(relay, LOW);
  129. }
  130. }
  131. void button() {
  132. digitalWrite(red, LOW);
  133. digitalWrite(green, LOW);
  134. digitalWrite(blue, HIGH);
  135. tone(piezo, 1000);
  136. delay(250);
  137. digitalWrite(blue, LOW);
  138. noTone(piezo);
  139. }