#include
int dly = 2000;
int motorStatus;
int val;
int rotdelay = 2000;
int buttonStatus;
int dl = 500;
///////////////
int m1 = 5;
int m2 = 6;
int m3 = 7;
int m4 = 8;
int m5 = 9;
int m6 = 10;
//////////////
void setup() {
pinMode(A0, INPUT);
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(m4, OUTPUT);
pinMode(m5, OUTPUT);
pinMode(m6, OUTPUT);
Serial.begin(112500);
while (!Serial) continue;
}
void loop() {
/////////////button selecting///////////////
int val = analogRead(A0);
if (val >= 580 && val<= 620){
buttonStatus = 1;
serialpush();
Serial.println("button1 selected");
delay(dl);
}
if (val >= 650 && val<= 680){
buttonStatus = 2;
serialpush();
Serial.println("button2 selected");
delay(dl);
}
if (val >= 700 && val<= 715){
buttonStatus = 3;
serialpush();
Serial.println("button3 selected");
delay(dl);
}
if (val >= 760 && val<= 775){
buttonStatus = 4;
serialpush();
Serial.println("button4 selected");
delay(dl);
}
if (val >= 830 && val<= 850){
buttonStatus = 5;
serialpush();
Serial.println("button5 selected");
delay(dl);
}
if (val >= 915 && val<= 935){
buttonStatus = 6;
serialpush();
Serial.println("button6 selected");
delay(dl);
}
///////////////////////selecting and switching motors/////////////////////////////////////////
if (Serial.available()){
serialpull();
if (motorStatus== 1) {
void motor1();
}
}
if (Serial.available()){
if (motorStatus== 2) {
void motor2();
}
}
if (Serial.available()){
if (motorStatus== 3) {
void motor3();
}
}
if (Serial.available()){
if (motorStatus== 4) {
void motor4();
}
}
if (Serial.available()){
if (motorStatus== 5) {
void motor5();
}
}
if (Serial.available()){
if (motorStatus== 6) {
void motor6();
}
}
////////////////////////////////////////////////////////////////
}
void serialpush(){
StaticJsonDocument<200> doc;
doc["buttonStatus"] = buttonStatus;
// Send the JSON document over the "link" serial port
serializeJson(doc, Serial);
// Wait
delay(2000);
}
void serialpull(){
StaticJsonDocument<200> doc;
doc["motorStatus"] = motorStatus;
deserializeJson(doc, Serial);
delay(2000);
}
//////////////////////////////////////////
void motor1(){
digitalWrite(m1, HIGH);
delay(rotdelay);
digitalWrite(m1, LOW);
}
void motor2(){
digitalWrite(m2, HIGH);
delay(rotdelay);
digitalWrite(m2, LOW);
}
void motor3(){
digitalWrite(m3, HIGH);
delay(rotdelay);
digitalWrite(m3, LOW);
}
void motor4(){
digitalWrite(m4, HIGH);
delay(rotdelay);
digitalWrite(m4, LOW);
}
void motor5(){
digitalWrite(m5, HIGH);
delay(rotdelay);
digitalWrite(m5, LOW);
}
void motor6(){
digitalWrite(m6, HIGH);
delay(rotdelay);
digitalWrite(m6, LOW);
}
the following codes are for esp32
#include
#include
#include
LCD_I2C lcd(0x27, 20, 4);
#define RST_PIN 4 // Configurable, see typical pin layout above
#define SS_PIN 5 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
#define RXp2 16
#define TXp2 17
uint32_t CardBalance = 0;
uint32_t total_cost=0;
String Card="";
uint32_t Card_Balance=0;
int progress=0, stage=0;
const unsigned long period = 1000;
float volume=0.0;
uint32_t unit_cost[]={200,300,400,500,600,700};
String item;
String prod1 = "choco";
String prod2 = "teabag";
String prod3 = "coffee";
String prod4 = "snack";
String prod5 = "candy";
String prod6 = "nut";
const char* ssid = "SOV36";
const char* password = "123456780";
String machine = "1A2B3C";
int cost;
String product;
int servstatus;
int buttonStatus;
int motorStatus;
int serdl = 60;
int32_t value;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXp2, TXp2);
WiFi.begin(ssid, password);
SPI.begin();
lcd.begin();
lcd.backlight();
mfrc522.PCD_Init(); // Init MFRC522 card
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
// Serial.println(F("tap your card"));
}
void loop() {
tap();
while (Serial2.available()){
serialpull();
while(buttonStatus!=0 || buttonStatus!=NULL){
stage=1;
Serial.println(buttonStatus);
total_cost= unit_cost[buttonStatus-1];
Serial.println("item cost= "+String(total_cost));
while(stage==1){
checkcardinfo();
}
if(CardBalance>=total_cost){
Serial.print("nice");}
else{DenielScreen();
}
buttonStatus=NULL;
motorStatus=NULL;
CardBalance=NULL;
total_cost=NULL;
}
}
}
void DenielScreen(){
Serial.println("insufficient balance");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("insufficient bal");
}
void serialpush(){
Serial2.println(motorStatus);
// Wait
//delay(2000);
Serial.println("serial push stage end");
}
void serialpull(){
if (Serial2.available()){
StaticJsonDocument<300> doc;
DeserializationError err = deserializeJson(doc, Serial2);
if (err == DeserializationError::Ok)
{
buttonStatus = doc["buttonStatus"].as();
Serial.println(buttonStatus);
delay(2000);
}
}
}
void serialpull1(){
if (Serial2.available()){
Serial.println(Serial2.read());
}
}
//void cardRequest(){
//Serial.print("tap a card");
//
//
// lcd.clear();
// lcd.setCursor(2, 1);
// lcd.print("TAP CARD TO PAY");
// lcd.setCursor(5, 2);
// lcd.print(product);
//}
////////////////////////////////////////////////////////////////////////////////////
/* checking new card information and storing them */
///////////////////////////////////////////////////////////////////////////////////////
void checkcardinfo(){
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
Card="";
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Card+=String(mfrc522.uid.uidByte[i],HEX);
Card.toUpperCase();
}
Serial.println ("new card = "+String(Card));
// screen();
// In this sample we use the second sector,
// that is: sector #1, covering block #4 up to and including block #7
byte sector = 1;
byte valueBlockB = 6;
byte trailerBlock = 7;
MFRC522::StatusCode status;
byte buffer[18];
byte size = sizeof(buffer);
// Authenticate using key A
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
byte trailerBuffer[] = {
255, 255, 255, 255, 255, 255, // Keep default key A
0, 0, 0,
0,
255, 255, 255, 255, 255, 255}; // Keep default key B
mfrc522.MIFARE_SetAccessBits(&trailerBuffer[6], 0, 6, 6, 3);
status = mfrc522.MIFARE_Read(trailerBlock, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Check if it matches the desired access pattern already;
// because if it does, we don't need to write it again...
if ( buffer[6] != trailerBuffer[6]
|| buffer[7] != trailerBuffer[7]
|| buffer[8] != trailerBuffer[8]) {
// They don't match (yet), so write it to the PICC
Serial.println(F("Writing new sector trailer..."));
status = mfrc522.MIFARE_Write(trailerBlock, trailerBuffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
}
// Authenticate using key B
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Show the new value of valueBlockB
status = mfrc522.MIFARE_GetValue(valueBlockB, &value);
CardBalance=value;
if (status != MFRC522::STATUS_OK) {
Serial.print(F("mifare_GetValue() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
Serial.println("card balance = "+String(CardBalance));
Serial.println();
if(CardBalance>=total_cost){
/////////////////////////////////////////////////
status = mfrc522.MIFARE_Decrement(valueBlockB, total_cost);//rest cost
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Decrement() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Transfer(valueBlockB);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Transfer() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Show the new value of valueBlockB
status = mfrc522.MIFARE_GetValue(valueBlockB, &value);
Serial.println("new balance= "+String(value));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("mifare_GetValue() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
////////////////////////////////
motorStatus=buttonStatus;
product = ("item"+String(buttonStatus));
Serial.println("motor "+String(motorStatus));
serialpush();
serialpull1();
// sendToserver();
balanceScreen();
delay(serdl);
}else{
DenielScreen();
}
mfrc522.PCD_StopCrypto1();
stage=0;
}
void tap(){
lcd.clear();
if (product == "1"){item = prod1;}
else if(product == "2"){item = prod2;}
else if(product == "3"){item = prod3;}
else if(product == "4"){item = prod4;}
else if(product == "5"){item = prod5;}
else if(product == "6"){item = prod6;}
// else {item = NULL;}
lcd.setCursor(4,1);
lcd.print("tap card");
lcd.setCursor(3,2);
lcd.print("pick a product");
lcd.setCursor(3,3);
lcd.print(item);
}
void balanceScreen(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("balance "+String(CardBalance));
lcd.setCursor(0,1);
lcd.print("new_bal "+String(value));
lcd.setCursor(0,2);
lcd.print("card "+String(Card));
lcd.setCursor(0,3);
lcd.print("price "+String(total_cost));
delay(3000);
}
problem rising while trying to upload my codes, shows an error over and over while trying upload to the atmega328p