Hello everybody,
I'm doing a project about weighting scale and I'm stuck at the data saving part.
My goal is to save a users card UID and all the items he weighted for a day to his account.
Example:
User UID: 95 7E 60 50
Checked In: 9:00 AM 03.12.19
Measured PCS: 3000
Checked Out: 17:00 PM
It will be even better if i would be able to save how much PCS he weighted at what our, but in the end i need the sum of all items for the day.
I achieved saving the items to a txt file with for cycle.
The problem with the card UID is that it saves multiple times with the items and i only need it once as i mentioned earlier.
If you can point me to solution I'll be very thankful.
ALSO I'm not using an RTC module because i have only DS1302 which i figured out is really frustrating, so i removed it completely.
Here is the code:
#include "HX711.h"
#include <LiquidCrystal.h>
#include <SPI.h>
#include <MFRC522.h>
#include <SD.h> // for the SD card
#define SS_PIN 10
#define RST_PIN 9
#define CS_SD 3
// Create a file to store the data
File myFile;
// Variable to hold the tag's UID
MFRC522 mfrc522(SS_PIN, RST_PIN);
HX711 scale;
float elements = 2.5;
LiquidCrystal lcd(A2,A1,5,4,A0,A5);
const int BackLight = 8;
String LCDLine1,LCDLine2;
int n;
const int buzzer = 7;
float rounded;
void updateLCD () {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(LCDLine1);
lcd.setCursor(0,1);
lcd.print(LCDLine2);
}
void setup() {
Serial.begin(9600);
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
lcd.begin(16,2);
pinMode(BackLight, OUTPUT);
digitalWrite(BackLight, HIGH);
LCDLine1="Welcome";
LCDLine2="Scan your ID";
updateLCD();
if (SD.begin(CS_SD))
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
while(true){
// Look for new cards02`11
if ( ! mfrc522.PICC_IsNewCardPresent())
{
continue;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
digitalWrite(CS_SD,LOW); // enables the cs pin
// If the file opened ok, write to it
if (myFile) {
Serial.println("Read:");
// Reading the whole file
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
}
else {
Serial.println("error opening test.txt");
}
digitalWrite(CS_SD,HIGH);
}
break;
}
while(true){
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "9E 7E 85 89") //change here the UID of the card/cards that you want to give access
{
Serial.println("Authorized access");
Serial.println();
delay(3000);
break;
}
else {
Serial.println(" Access denied");
delay(3000);
}
}
Serial.println("HX711 Demo");
LCDLine1="Initializing ...";
LCDLine2="";
updateLCD();
// parameter "gain" is ommited; the default value 128 is used by the library
// HX711.DOUT - pin #A1
// HX711.PD_SCK - pin #A0
scale.begin(A4, A3);
scale.set_scale(1900.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(10), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
}
void Scale()
{
float average = scale.get_units(10);
if (average < 0.3)
{
average = 0;
}
float number = scale.get_units()/elements;//divide the average of 20 readings with the elements value to get the number of elements
n = round(number);
//n = (int)number; // convert float into int
Serial.print("\t| elements:\t");
Serial.print(n); //serial monitor
// lcd.clear();
//set cursor first row
LCDLine1=String(average,1)+" g"; //print on lcd average value from 10 raw readings 1 time
LCDLine2=String(n)+" pcs";
Serial.print("\t| average:\t");
Serial.println(average); // serial monitor -||-
//print PCS
scale.power_down(); // put the ADC in sleep mode
delay(1000);
scale.power_up();
updateLCD();
}
void myfile()
{
String content= "";
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
for (int n=0; n==0; n++)
{
myFile.print(n, DEC);
myFile.print("/");
}
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
myFile.print("/");
myFile.print(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println("sucessfully written on SD card");
myFile.close(); // close the file
}
}
void loop() {
Scale();
updateLCD();
myfile();
}