Hello all brother and sister. I was using arduino iot cloud and esp8266 connected with RFID reader and button switch. my code Under this problem. My destination is when I scan card for first time it will store in array and if I scan again it delete that item in array. make sur the process scanning is stop I push button switch and then array that store items when I scan it will show on dasboard message. but my problem is it do not show.
my code:
#include "thingProperties.h"
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN D4
#define RST_PIN D3
#define LED_red 01
#define LED_green 03
#define buzzerPin D8
#define buttonPin D0
MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x27, 16, 2);
struct Product {
String id;
String name;
double price;
};
const int MAX_PRODUCTS = 10;
Product products[MAX_PRODUCTS];
int numProducts = 0;
String scannedProducts[MAX_PRODUCTS];
int numScannedProducts = 0;
String tagID = "";
double Total = 0.00;
bool pressed = false;
//int size = 5;
String items[MAX_PRODUCTS];
int numScanneditem;
void displayMessage(const String& line1, const String& line2, int delayTime);
void setup() {
Serial.begin(9600);
//delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
addProduct("332EEE34", "Bottle", 1.00);
addProduct("F37EB915", "Can", 2.00);
addProduct("E3433814", "Basket", 3.00);
pinMode(buttonPin, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.clear();
//lcd.setCursor(4, 0);
lcd.print("Welcome!");
lcd.setCursor(0, 1);
lcd.print("Please scan me!!");
SPI.begin();
mfrc522.PCD_Init();
delay(4);
mfrc522.PCD_DumpVersionToSerial();
pinMode(LED_red, OUTPUT);
pinMode(LED_green, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// items[0] = "hello";
// items[1] = "my name";
//delete[] items;
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
bool curentState = digitalRead(buttonPin);
String scan;
for(int i = 0; i < numScannedProducts; i++){
scan += items[i] + ", ";
delay(1000);
if(items[i+1] == ""){
scan+="Amount: " + String(Total) + "$";
break;
}
}
//delay(2000);
scanning = scan;
//clearArray();
if (curentState == pressed) {
//delay(2000);
//clearArray();
delay(2000);
// for(int i = 0; i < MAX_PRODUCTS; i++){
// scan += items[i] + ", ";
// delay(1000);
// if(items[i+1] == ""){
// scan+="Amount: " + String(Total) + "$";
// break;
// }
// }
//scanning = scan;
resetScannedProducts();
Total = 0;
displayMessage("Thank You.", "Have a nice day!", 5000);
displayMessage("Welcome!", "Please scan me!!",0);
while(digitalRead(buttonPin) == pressed){
if (getID()) {
processRFID();
}
}
}
else if (getID()) {
processRFID();
}
}
void onIDChange() {
// Add your code here to act upon ID change
}
void onProductChange() {
// Add your code here to act upon Product change
}
void onPriceChange() {
// Add your code here to act upon Price change
}
void onScanningChange() {
// if(scanning == ""){
// displayMessage("Thank You.", "Have a nice day!", 5000);
// displayMessage("Welcome!", "Please scan me!!",0);
// }
// Add your code here to act upon Scanning change
}
//void onTotalChange() {
// Add your code here to act upon Total change
// if (total == "") {
// resetScannedProducts();
// Total = 0;
// displayMessage("Thank You.", "Have a nice day!", 5000);
// displayMessage("Welcome!", "Please scan me!!",0);
// }
//}
void onClickChange() {
// while(click == true){
// for(int i = 0; i < numProducts; i++){
// scanning = items[numProducts];
// }
// scanning = "Amount: " + String(Total) + "$";
// }
}
void clearStringArray() {
for (int i = 0; i < sizeof(items) / sizeof(items[0]); i++) {
items[i] = "";
}
}
void clearArray() {
for (int i = 0; i < 100 ; i++) {
items[i] = "";// Set each element to an empty string
//delay(500);
}
}
void displayMessage(const String& line1, const String& line2, int delayTime = 1000) {
lcd.clear();
if(line1 == "Welcome!")
lcd.setCursor(4, 0);
else if(line1 == "Thank You.")
lcd.setCursor(3, 0);
else
lcd.setCursor(0,0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
delay(delayTime);
}
boolean getID() {
if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
return false;
}
tagID = "";
for (uint8_t i = 0; i < 4; i++) {
tagID.concat(String(mfrc522.uid.uidByte[i], HEX));
}
tagID.toUpperCase();
mfrc522.PICC_HaltA();
return true;
}
void processRFID() {
for (int i = 0; i < numProducts; i++) {
if (tagID == products[i].id) {
if (!isProductScanned(products[i].id)) {
addProductAction(i, true);
} else {
addProductAction(i, false);
}
}
}
}
void addProductAction(int index, bool adding) {
//String item;
digitalWrite(adding ? LED_green : LED_red, HIGH);
tone(buzzerPin, adding ? 3250 : 2250);
delay(250);
digitalWrite(adding ? LED_green : LED_red, LOW);
noTone(buzzerPin);
displayMessage(adding ? "Adding:" : "Removing:",
adding ? products[index].name + " + " + String(products[index].price) + "$" : products[index].name + " - " + String(products[index].price) + "$");
//scanning = products[index].name +" "+ String(products[index].price) + "$";
if (adding) {
items[index] = products[index].name + " " + String(products[index].price) + "$";
//scanning = products[index].name + " + " + String(products[index].price) + "$";
Total += products[index].price;
addScannedProduct(products[index].id);
} else {
//size = removeItemFromArray(items, size, index);
//scanning = "\x1b";
Total -= products[index].price;
removeScannedProduct(products[index].id);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Item: " + String(numScannedProducts));
//scanning = products[index].name +" "+ String(products[index].price) + "$";
//scanning = "Item: " + String(numScannedProducts) + " : " + products[index].name;
lcd.setCursor(0, 1);
lcd.print("Amount: " + String(Total) + "$");
//delay(2000);
//scanning = "Amount: " + String(Total) + "$";
}
void addProduct(const String& id, const String& name, double price) {
if (numProducts < MAX_PRODUCTS) {
products[numProducts] = {id, name, price};
numProducts++;
}
}
boolean isProductScanned(const String& id) {
for (int i = 0; i < numScannedProducts; i++) {
if (scannedProducts[i] == id) {
return true;
}
}
return false;
}
void addScannedProduct(const String& id) {
if (numScannedProducts < MAX_PRODUCTS) {
scannedProducts[numScannedProducts] = id;
numScannedProducts++;
}
}
void removeScannedProduct(const String& id) {
for (int i = 0; i < numScannedProducts; i++) {
if (scannedProducts[i] == id) {
for (int j = i; j < numScannedProducts - 1; j++) {
scannedProducts[j] = scannedProducts[j + 1];
}
numScannedProducts--;
break;
}
}
}
void resetScannedProducts() {
//scanning = scan;
numScannedProducts = 0;
}
please tell me the problem. Thank you.