Advertisement
redm0n

Piggy Bank code

Aug 15th, 2015
1,453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. //Piggy Bank code by mizzuddinkamal
  2.  
  3. // include the library code:
  4. #include <LiquidCrystal.h>
  5. #include <Wire.h>
  6.  
  7. // initialize the library with the numbers of the interface pins
  8. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  9.  
  10. // include the library code:
  11. #include <LiquidCrystal.h>
  12.  
  13. // initialize the library with the numbers of the interface pins
  14. LiquidCrystal lcd(7,8,9,10,11,12);
  15.  
  16. volatile byte CoinPulseCount = 0;
  17. byte NewCoinInserted;
  18. byte Command = 0;
  19. int OpticalCountPin = 3;
  20. volatile unsigned long PulseTime;
  21. float CurrentCredit;
  22. float NewCurrentCredit;
  23.  
  24. String OnePulse = "50 Sen";
  25. String TwoPulses = "50 Sen";
  26. String ThreePulses = "20 Sen";
  27. String FourPulses = "20 Sen";
  28. String FivePulses = "£1";
  29. String SixPulses = "£2";
  30.  
  31. void showCredit(){
  32. NewCurrentCredit = (CurrentCredit/100) + NewCurrentCredit;
  33. Serial.print("Total Credit: RM ");
  34. Serial.println(NewCurrentCredit);
  35. lcd.begin(16,2);
  36. lcd.clear();
  37. lcd.setCursor(0,0);
  38. lcd.print("Total Credit: RM");
  39. lcd.setCursor(0,1);
  40. lcd.print(NewCurrentCredit);
  41. }
  42.  
  43. void setup(){
  44. Serial.begin(9600);
  45. Serial.println("Waiting...");
  46. Serial.println();
  47. pinMode(OpticalCountPin, INPUT);
  48. attachInterrupt(1, CoinPulse, RISING);
  49. lcd.setCursor(0,0);
  50. lcd.print("Waiting. . .");
  51. }
  52.  
  53. void loop(){
  54. if(CoinPulseCount > 0 && millis() - PulseTime > 200){
  55. NewCoinInserted = CoinPulseCount;
  56. CoinPulseCount = 0;
  57. }
  58. switch(NewCoinInserted){
  59. case 1:
  60. Serial.println(OnePulse + " inserted");
  61. CurrentCredit = 50;
  62. showCredit();
  63. NewCoinInserted = 0;
  64. break;
  65. case 2:
  66. Serial.println(TwoPulses + " inserted");
  67. CurrentCredit = 50;
  68. showCredit();
  69. NewCoinInserted = 0;
  70. break;
  71. case 3:
  72. Serial.println(ThreePulses + " inserted");
  73. CurrentCredit = 20;
  74. showCredit();
  75. NewCoinInserted = 0;
  76. break;
  77. case 4:
  78. Serial.println(FourPulses + " inserted");
  79. CurrentCredit = 20;
  80. showCredit();
  81. NewCoinInserted = 0;
  82. break;
  83. case 5:
  84. Serial.println(FivePulses + " inserted");
  85. NewCoinInserted = 0;
  86. break;
  87. case 6:
  88. Serial.println(SixPulses + " inserted");
  89. NewCoinInserted = 0;
  90. break;
  91. }
  92. }
  93.  
  94. void CoinPulse(){
  95. CoinPulseCount ++;
  96. PulseTime = millis();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement