Hello, everybody!
So I am making project about arduino weighting scale with attendance system in it. In the process i stumbled upon a little problem:
When i start the start the system my lcd is working fine (it's showing everything correctly) until the part where it has to show me the weight of the items I am measuring and the pieces.
I am posting the code in a hope that somebody will point me in the right direction.
P.S: It's probably possible that the problem may be in my schematic, but I am not sure.
Thank you for your help!
#include "HX711.h"
#include <LiquidCrystal.h>
#include <SPI.h>
#include <MFRC522.h>
#include <SD.h> // for the SD card
#include <RTClib.h> // for the RTC
#define SS_PIN 10
#define RST_PIN 9
#define CS_SD 3
// Create a file to store the data
File myFile;
// Instance of the class for RTC
RTC_DS1307 rtc;
// Define check in time
const int checkInHour = 9;
const int checkInMinute = 5;
//Variable to hold user check in
int userCheckInHour;
int userCheckInMinute;
// Variable to hold the tag's UID
String uidString;
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 = 2;
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
SD.begin(CS_SD);
lcd.begin(16,2);
pinMode(BackLight, OUTPUT);
digitalWrite(BackLight, HIGH);
LCDLine1="Welcome";
LCDLine2="Scan your ID";
updateLCD();
while(true){
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
continue;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
tone(buzzer, 2000);
noTone(buzzer);
continue;
}
//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();
// Setup for the SD card
Serial.print("Initializing SD card...");
if(!SD.begin(CS_SD)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// Setup for the RTC
if(!rtc.begin()) {
Serial.println("Couldn't find RTC");
while(1);
}
else {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
if(!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
// 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 loop() {
Scale();
updateLCD();
}
void logCard() {
// Enables SD card chip select pin
digitalWrite(CS_SD,LOW);
// Open file
myFile=SD.open("DATA.txt", FILE_WRITE);
// If the file opened ok, write to it
if (myFile) {
Serial.println("File opened ok");
myFile.print(uidString);
myFile.print(", ");
// Save time on SD card
DateTime now = rtc.now();
myFile.print(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(',');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.println(now.minute(), DEC);
myFile.close();
// Print time on Serial monitor
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.println(now.minute(), DEC);
Serial.println("sucessfully written on SD card");
myFile.close();
userCheckInHour = now.hour();
userCheckInMinute = now.minute();
digitalWrite(CS_SD,HIGH);
}
}
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();
}