#include <Servo.h>
#include <IRremote.h>
#include <LiquidCrystal.h>
int pin_reciever = 4;
//servo library
Servo servo;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
IRrecv receiver(pin_reciever);
int trigPin = 1;
int echoPin = 0;
int servoPin = 13;
int led = 10;
int x;
String y;
int number = 6528;
long duration, dist, average;
long aver[3]; //array for average
String bando = "";
String strinput = "";
boolean run = true;
char input[4];
int pressedKey = "";
decode_results results;
void setup() {
lcd.begin(16, 2);
// you can now interact with the LCD, e.g.:
Serial.begin(9600);
servo.attach(servoPin);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.write(0); //close cap on power on
delay(100);
servo.detach();
receiver.enableIRIn();
}
void loop() {
lcd.print("Press 0 to start");
waitForKey();
if (pressedKey == "0"){
strinput = "";
lcd.clear();
print(" Welcome to the Maths Lab");
print(" This is a ATM machine");
lcd.print("Insert card");
while(run){
getDistance();
Serial.print(dist);
if (dist < 5 && dist != 0){
run = false;
}
}
lcd.clear();
pin:
lcd.print("Enter the pin");
for(int i = 0; i < 4; i++){
waitForKey();
strinput = strinput + pressedKey;
lcd.clear();
lcd.print(strinput);
}
if (strinput == "6528"){
servo.write(0);
print(" Well Done. You found the number");
print(" Here's your prize");
servo.attach(servoPin);
for (int pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
servo.detach();
}
if(strinput != "6528"){
lcd.clear();
lcd.print("Try again");
delay(1000);
lcd.clear();
delay(1000);
strinput = "";
goto pin;
}
}
return 0;
}
void print(String text) {
lcd.setCursor(0,0);
lcd.print(text);
for (int scrollCounter = 0; scrollCounter < text.length(); scrollCounter++)
{
lcd.scrollDisplayLeft();
delay(200);
}
lcd.clear();
}
void pront(String text1, String text2){
lcd.home();
lcd.print(text1);
lcd.setCursor(0,1);
lcd.print(text2);
}
void measure() {
digitalWrite(10,HIGH);
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
dist = (duration/2) / 29.1; //obtain distance
}
void getDistance(){
for (int i=0;i<=2;i++) { //average distance
measure();
aver[i]=dist;
delay(10); //delay between measurements
}
dist=(aver[0]+aver[1]+aver[2]);
}
void waitForKey(){
pressedKey = "";
while (run) {
translateIR();
if (pressedKey != ""){
run = false;
}
}
}
void translateIR()
{
// Takes command based on IR code received
switch (receiver.decodedIRData.command) {
case 162:
pressedKey = "POWER";
break;
case 226:
pressedKey = "MENU";
break;
case 34:
pressedKey = "TEST";
break;
case 2:
pressedKey = "PLUS";
break;
case 194:
pressedKey = "BACK";
break;
case 224:
pressedKey = "PREV.";
break;
case 168:
pressedKey = "PLAY";
break;
case 144:
pressedKey = "NEXT";
break;
case 104:
pressedKey = "0";
break;
case 152:
pressedKey = "MINUS";
break;
case 176:
pressedKey = "C";
break;
case 48:
pressedKey = "1";
break;
case 24:
pressedKey = "2";
break;
case 122:
pressedKey = "3";
break;
case 16:
pressedKey = "4";
break;
case 56:
pressedKey = "5";
break;
case 90:
pressedKey = "6";
break;
case 66:
pressedKey = "7";
break;
case 74:
pressedKey = "8";
break;
case 82:
pressedKey = "9";
break;
default:
pressedKey = "Other button";
}
}