Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <stdint.h>
- #include <IRremote.h>
- #include <LiquidCrystal.h>
- #define TIMEOUT 120
- LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
- IRrecv irrecv(2);
- decode_results irData;
- //WDT timer
- volatile uint8_t frontWDT; //Fron-end wdt. This will reset current transcation, system WDT will reset the entire program.
- //Input buffer
- volatile uint32_t inputBuffer; //User input buffer
- volatile uint8_t lastInput;
- volatile uint8_t lastValidInput; //In case of IR error
- volatile boolean bufferWriteComplete;
- void resetBuffer() {
- inputBuffer = 0;
- lastInput = 0x00;
- bufferWriteComplete = false;
- }
- //User db
- volatile uint32_t userBalance[32];
- volatile uint32_t userPass[32];
- void setup(){
- //Init begin
- cli();
- //Debug interface
- Serial.begin(9600);
- //Init IR
- irrecv.enableIRIn();
- irrecv.blink13(true);
- //LCD = 1602
- lcd.begin(16, 2);
- //Init timer: CTC, 1s
- TCCR1A = 0;
- TCCR1B = 0;
- OCR1A = 15624; //Compare = 1s
- TCCR1B |= (1 << WGM12);
- TCCR1B |= (1 << CS12)|(1 << CS10); //Scale = 1024
- TIMSK1 |= (1 << OCIE1A);
- //Create IR interrupt
- attachInterrupt(digitalPinToInterrupt(2),ISR_IRRxc,CHANGE);
- //Load user info
- for (uint8_t i = 0; i < 32; i++) {
- userPass[i] = i;
- userBalance[i] = 100;
- }
- //Init down, enable interrupt
- sei();
- }
- ISR(TIMER1_COMPA_vect){ //Front-end WDT ++
- frontWDT++;
- // Serial.println(frontWDT);
- }
- void ISR_IRRxc() {
- if(irrecv.decode(&irData)) {
- switch (irData.value) {
- case 0xFFA25D: lastInput = 0x0A; break; //Press POWER
- case 0xFF6897: lastInput = 0x00; break; //Press digit 0 - 9
- case 0xFF30CF: lastInput = 0x01; break;
- case 0xFF18E7: lastInput = 0x02; break;
- case 0xFF7A85: lastInput = 0x03; break;
- case 0xFF10EF: lastInput = 0x04; break;
- case 0xFF38C7: lastInput = 0x05; break;
- case 0xFF5AA5: lastInput = 0x06; break;
- case 0xFF42BD: lastInput = 0x07; break;
- case 0xFF4AB5: lastInput = 0x08; break;
- case 0xFF52AD: lastInput = 0x09; break;
- default: lastInput = 0xFF; break; //Unknown key
- }
- if (lastInput < 0x0A) { //Input digit, concat to buffer
- inputBuffer = inputBuffer * 10 + lastInput;
- lastValidInput = lastInput;
- }
- else if (lastInput == 0x0A) //Input POWER (ENTER), fire signal
- bufferWriteComplete = true;
- Serial.println("IR Rxc interrupt: buffer = "+String(inputBuffer)+" New = "+String(lastInput));
- delay(250);
- irrecv.resume();
- }
- }
- void writeLCD(String line0, String line1) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(line0);
- lcd.setCursor(0, 1);
- lcd.print(line1);
- }
- void loop() {
- /* S_Reset */
- Serial.println("[State] RESET");
- writeLCD("RESET...","");
- delay(500);
- /* S_Hello */
- Serial.println("[State] HELLO");
- writeLCD("HELLO","PRESS POWER");
- frontWDT = 0;
- resetBuffer();
- while(frontWDT < TIMEOUT && !bufferWriteComplete) ; //Wait until user input finish or timeout
- if (frontWDT >= TIMEOUT) //Timeout, end current transcation
- return;
- /* S_Acc */
- Serial.println("[State] Account");
- writeLCD("Account:","");
- frontWDT = 0;
- resetBuffer();
- while(frontWDT < TIMEOUT && !bufferWriteComplete) {
- writeLCD("Account:",String(inputBuffer));
- delay(100);
- }
- if (frontWDT >= TIMEOUT) //Timeout, end current transcation
- return;
- /* S_CheckAcc */
- Serial.println("[State] Check account");
- if (inputBuffer >= 32) { //There is only 32 users
- Serial.println("Invalid user account");
- writeLCD("Invalid account","");
- delay(4000);
- return;
- }
- uint8_t userid = 0x1F & inputBuffer; //Using 0001 1111 to mask 6-bit userid space
- char username = userid + 0x41; //id 0 = A, 1 = B, and so on...
- Serial.println("User id: "+String(userid));
- Serial.println("User name: "+String(username));
- /* S_Pass */
- for (uint8_t i = 0; i < 255; i++) {
- Serial.println("[State] Password");
- Serial.println("Try = "+String(i));
- writeLCD("Password:","");
- frontWDT = 0;
- resetBuffer();
- while(frontWDT < TIMEOUT && !bufferWriteComplete) {
- writeLCD("Password:",String(inputBuffer));
- delay(100);
- }
- if (frontWDT >= TIMEOUT) //Timeout, end current transcation
- return;
- /* S_CheckPass */
- Serial.println("[State] Check password");
- if (inputBuffer == userPass[userid]) { //Compare pass with user input
- Serial.println("Password OK");
- writeLCD("Password OK","");
- delay(2000);
- break; //Break the loop
- }
- else {
- Serial.println("Bad Password");
- writeLCD("Bad Password","");
- delay(2000);
- if (i >= 3) return; //Failed for more than 3 times, end current transcation
- }
- }
- /* S_Mode */
- Serial.println("[State] Mode");
- writeLCD("$ "+String(userBalance[userid]),"1.Dep 2.Wtd");
- boolean dw;
- frontWDT = 0;
- resetBuffer();
- while(frontWDT < TIMEOUT && lastInput != 0x01 && lastInput != 0x02 ) { //Wait until timeout or press 1 or 2
- }
- if (frontWDT >= TIMEOUT) //Timeout, end current transcation
- return;
- dw = (lastValidInput == 0x01);
- /* S_Amount */
- Serial.println("[State] Amount");
- String dwWord = dw ? "Deposit" : "Withdrawal";
- Serial.println("Mode: "+dwWord);
- writeLCD(dwWord+":","$ ");
- frontWDT = 0;
- resetBuffer();
- while(frontWDT < TIMEOUT && !bufferWriteComplete) {
- writeLCD(dwWord+":","$ "+String(inputBuffer));
- delay(100);
- }
- if (frontWDT >= TIMEOUT) //Timeout, end current transcation
- return;
- /* S_CheckAmount */
- Serial.println("[State] Check amount");
- Serial.println("Before transcation: "+String(userBalance[userid]));
- Serial.println("Transcation amount: "+String(inputBuffer));
- if (dw) {
- userBalance[userid] += inputBuffer; //User should not have that much money which could cause bank overflow, uint32_t = 2^32 = $4G
- }
- else {
- if (userBalance[userid] < inputBuffer) {
- Serial.println("Insufficient funds");
- writeLCD("Insufficient"," funds :(");
- delay(2000);
- }
- else {
- userBalance[userid] -= inputBuffer;
- }
- }
- /* S_Display */
- Serial.println("[State] Display");
- Serial.println("After transcation: "+String(userBalance[userid]));
- writeLCD(String(username),"$ "+String(userBalance[userid]));
- delay(6000);
- writeLCD("Thank you","Merci");
- delay(3000);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement