Untitled
Untitled
Untitled
h>
#include <SPI.h>
#define L1_MOTOR 30
#define L2_MOTOR 31
#define L_ENA 6
#define R1_MOTOR 32
#define R2_MOTOR 33
#define R_ENA 7
#define TRIG_PIN 34
#define ECHO_PIN 35
#define L_IR 36
#define R_IR 37
#define RED_LED 38
#define GREEN_LED 39
#define BLUE_LED 40
#define RST_PIN 5
#define SS_PIN 53
MFRC522 mfrc522 (SS_PIN, RST_PIN);
int distance;
int counter;
long duration;
String content;
String command;
bool flag = true;
void setup ()
{
Serial.begin (9600);
SPI.begin ();
mfrc522.PCD_Init ();
Serial.println ("STARTING");
delay (500);
Serial.println ("PLEASE WAIT . . . ");
delay (2500);
//----------------------------------------
//LEDs
void RED ()
{
digitalWrite (RED_LED, HIGH);
digitalWrite (GREEN_LED, LOW);
digitalWrite (BLUE_LED, LOW);
}
void GREEN ()
{
digitalWrite (RED_LED, LOW);
digitalWrite (GREEN_LED, HIGH);
digitalWrite (BLUE_LED, LOW);
}
void BLUE ()
{
digitalWrite (RED_LED, LOW);
digitalWrite (GREEN_LED, LOW);
digitalWrite (BLUE_LED, HIGH);
}
void RGB ()
{
digitalWrite (RED_LED, HIGH);
digitalWrite (GREEN_LED, LOW);
digitalWrite (BLUE_LED, LOW);
delay (500);
void OFF ()
{
digitalWrite (RED_LED, LOW);
digitalWrite (GREEN_LED, LOW);
digitalWrite (BLUE_LED, LOW);
}
//----------------------------------------
//MOTORs
void FORWARD ()
{
Serial.println ("FORWARD");
GREEN ();
digitalWrite (L1_MOTOR, HIGH);
digitalWrite (L2_MOTOR, LOW);
analogWrite (L_ENA, 60);
void STOP ()
{
Serial.println ("STOP");
RED ();
void LEFT ()
{
Serial.println ("LEFT");
BLUE ();
void RIGHT ()
{
Serial.println ("RIGHT");
BLUE ();
//----------------------------------------
//RETURN
void RETURN ()
{
while (flag)
{
Serial.println ("DONE!");
RGB ();
delay (1500);
OFF ();
if (flag == true)
{
flag = false;
counter = 0;
command = "";
return ;
}
}
}
//----------------------------------------
//RETURNING FROM STATION
void RETURN_ONE ()
{
while (flag)
{
Serial.println ("RETURNING");
RGB ();
delay (1500);
OFF ();
if (flag == true)
{
flag = false;
counter = 1;
command = "";
return ;
}
}
}
void RETURN_TWO ()
{
while (flag)
{
Serial.println ("RETURNING");
RGB ();
delay (1500);
OFF ();
if (flag == true)
{
flag = false;
counter = 2;
command = "";
return ;
}
}
}
void RETURN_THREE ()
{
while (flag)
{
Serial.println ("RETURNING");
RGB ();
delay (1500);
OFF ();
if (flag == true)
{
flag = false;
counter = 3;
command = "";
return ;
}
}
}
void RETURN_FOUR ()
{
while (flag)
{
Serial.println ("RETURNING");
RGB ();
delay (1500);
OFF ();
if (flag == true)
{
flag = false;
counter = 4;
command = "";
return ;
}
}
}
//----------------------------------------
//COMMANDS
//CASHIER
void CASHIER ()
{
digitalWrite (TRIG_PIN, LOW);
delayMicroseconds (2);
digitalWrite (TRIG_PIN, HIGH);
delayMicroseconds (10);
digitalWrite (TRIG_PIN, LOW);
if (!mfrc522.PICC_IsNewCardPresent ())
{
return ;
}
if (!mfrc522.PICC_ReadCardSerial ())
{
return ;
}
content = "";
byte letter;
RETURN_ONE ();
}
}
void CASHIER_ELEVATOR ()
{
digitalWrite (TRIG_PIN, LOW);
delayMicroseconds (2);
digitalWrite (TRIG_PIN, HIGH);
delayMicroseconds (10);
digitalWrite (TRIG_PIN, LOW);
if (!mfrc522.PICC_IsNewCardPresent ())
{
return ;
}
if (!mfrc522.PICC_ReadCardSerial ())
{
return ;
}
content = "";
byte letter;
RETURN ();
}
}
//BUSINESS OFFICE
void BUSINESS ()
{
digitalWrite (TRIG_PIN, LOW);
delayMicroseconds (2);
digitalWrite (TRIG_PIN, HIGH);
delayMicroseconds (10);
digitalWrite (TRIG_PIN, LOW);
if (!mfrc522.PICC_IsNewCardPresent ())
{
return ;
}
if (!mfrc522.PICC_ReadCardSerial ())
{
return ;
}
content = "";
byte letter;
RETURN_TWO ();
}
}
void BUSINESS_ELEVATOR ()
{
digitalWrite (TRIG_PIN, LOW);
delayMicroseconds (2);
digitalWrite (TRIG_PIN, HIGH);
delayMicroseconds (10);
digitalWrite (TRIG_PIN, LOW);
if (!mfrc522.PICC_IsNewCardPresent ())
{
return ;
}
if (!mfrc522.PICC_ReadCardSerial ())
{
return ;
}
content = "";
byte letter;
RETURN ();
}
}
//TOOL_ROOM
void TOOL ()
{
digitalWrite (TRIG_PIN, LOW);
delayMicroseconds (2);
digitalWrite (TRIG_PIN, HIGH);
delayMicroseconds (10);
digitalWrite (TRIG_PIN, LOW);
if (!mfrc522.PICC_IsNewCardPresent ())
{
return ;
}
if (!mfrc522.PICC_ReadCardSerial ())
{
return ;
}
content = "";
byte letter;
RETURN_THREE ();
}
}
void TOOL_ELEVATOR ()
{
digitalWrite (TRIG_PIN, LOW);
delayMicroseconds (2);
digitalWrite (TRIG_PIN, HIGH);
delayMicroseconds (10);
digitalWrite (TRIG_PIN, LOW);
if (!mfrc522.PICC_IsNewCardPresent ())
{
return ;
}
if (!mfrc522.PICC_ReadCardSerial ())
{
return ;
}
content = "";
byte letter;
RETURN ();
}
}
//REGISTRAR
void REGISTRAR ()
{
digitalWrite (TRIG_PIN, LOW);
delayMicroseconds (2);
digitalWrite (TRIG_PIN, HIGH);
delayMicroseconds (10);
digitalWrite (TRIG_PIN, LOW);
if (!mfrc522.PICC_IsNewCardPresent ())
{
return ;
}
if (!mfrc522.PICC_ReadCardSerial ())
{
return ;
}
content = "";
byte letter;
RETURN_FOUR ();
}
}
void REGISTRAR_ELEVATOR ()
{
digitalWrite (TRIG_PIN, LOW);
delayMicroseconds (2);
digitalWrite (TRIG_PIN, HIGH);
delayMicroseconds (10);
digitalWrite (TRIG_PIN, LOW);
if (!mfrc522.PICC_IsNewCardPresent ())
{
return ;
}
if (!mfrc522.PICC_ReadCardSerial ())
{
return ;
}
content = "";
byte letter;
RETURN ();
}
}
void loop ()
{
flag = true ;
//FORT
if (command == "*cashier#" || command == "*Cashier#")
{
CASHIER ();
}
//BACK
if (counter == 1)
{
CASHIER_ELEVATOR ();
}
if (counter == 2)
{
BUSINESS_ELEVATOR ();
}
if (counter == 3)
{
TOOL_ELEVATOR ();
}
if (counter == 4)
{
REGISTRAR_ELEVATOR ();
}
}