#include <AddicoreRFID.h>
// this constant won't change:
const int buttonPin = 2; // pin for the main switch connected to the machine
const int resetPin = 4; // pin for the reset button
const int addOnePin = 6; // pin for the +1 button
const int subOnePin = 13; // pin for the -1 button
const int ledRedPin = 22; // LED pin for Reset Button
const int ledGreenPin = 23; // LED pin for +1 button
const int ledYellowPin = 24; // LED pin for -1 button
const int ledRunPin = 25; //LED pin for running green LED
const int ledOffPin = 26; //LED pin for off red LED
const int ledMaintPin = 27; //LED pin for maintenance yellow LED
const int ledITPin = 28; //LED pin for IT blue LED
// include the library code:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
//RFID setup
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[3];
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int resetState = 0; // default reset state
int addOneState = 0; // default add one state
int subOneState = 0; // default subtract one state
String machineState = "off"; // default machine state
// LCD setup: initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
// initialize the button pins as an input and LEDs as an output
pinMode(buttonPin, INPUT);
pinMode(resetPin, INPUT);
pinMode(addOnePin, INPUT);
pinMode(subOnePin, INPUT);
pinMode (ledRedPin, OUTPUT);
pinMode (ledGreenPin, OUTPUT);
pinMode (ledYellowPin, OUTPUT);
pinMode (ledRunPin, OUTPUT);
pinMode (ledOffPin, OUTPUT);
pinMode (ledMaintPin, OUTPUT);
pinMode (ledITPin, OUTPUT);
// turn on button leds
digitalWrite(ledRedPin, HIGH);
digitalWrite(ledGreenPin, HIGH);
digitalWrite(ledYellowPin, HIGH);
digitalWrite(ledRunPin, LOW);
digitalWrite(ledOffPin, LOW);
digitalWrite(ledMaintPin, LOW);
digitalWrite(ledITPin, LOW);
// initialize serial communication:
Serial.begin(9600);
// configure the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Cycle Count");
//RFID void setup
Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Serial.println(F("This code scan the MIFARE Classic NUID."));
Serial.print(F("Using the following key:"));
printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}
void loop() {
// read the pushbutton input pins:
buttonState = digitalRead(buttonPin);
resetState = digitalRead(resetPin);
addOneState = digitalRead(addOnePin);
subOneState = digitalRead(subOnePin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println(buttonPushCounter);
machineState = "run";
} else {
// if the current state is LOW then the button went from on to off:
}
}
//reset button is pushed
if (resetState == HIGH) {
digitalWrite(ledRedPin, LOW);
buttonPushCounter = 0;
lcd.clear();
lcd.begin(16, 2);
lcd.print("Cycle Count");
lcd.setCursor(0, 1);
lcd.print(buttonPushCounter);
delay(250);
digitalWrite(ledRedPin, HIGH);
machineState = "off";
}
//add one button is pushed
if (addOneState == HIGH) {
digitalWrite(ledGreenPin, LOW);
buttonPushCounter++;
lcd.clear();
lcd.begin(16, 2);
lcd.print("Cycle Count");
lcd.setCursor(0, 1);
lcd.print(buttonPushCounter);
Serial.println(buttonPushCounter);
delay(250);
digitalWrite(ledGreenPin, HIGH);
}
//subtract one button is pushed
if (subOneState == HIGH) {
digitalWrite(ledYellowPin, LOW);
buttonPushCounter--;
lcd.clear();
lcd.begin(16, 2);
lcd.print("Cycle Count");
lcd.setCursor(0, 1);
lcd.print(buttonPushCounter);
Serial.println(buttonPushCounter);
delay(250);
digitalWrite(ledYellowPin, HIGH);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// set LEDs to togglen on/off depending on the state of the machine
if (machineState == "run") {
digitalWrite(ledRunPin, HIGH);
digitalWrite(ledOffPin, LOW);
digitalWrite(ledMaintPin, LOW);
digitalWrite(ledITPin, LOW);
}
if (machineState == "off") {
digitalWrite(ledRunPin, LOW);
digitalWrite(ledOffPin, HIGH);
digitalWrite(ledMaintPin, LOW);
digitalWrite(ledITPin, LOW);
}
if (machineState == "maint") {
digitalWrite(ledRunPin, LOW);
digitalWrite(ledOffPin, LOW);
digitalWrite(ledMaintPin, HIGH);
digitalWrite(ledITPin, LOW);
}
if (machineState == "IT") {
digitalWrite(ledRunPin, LOW);
digitalWrite(ledOffPin, LOW);
digitalWrite(ledMaintPin, LOW);
digitalWrite(ledITPin, HIGH);
}
// LCD screen: set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(buttonPushCounter);
// Delay a little bit to avoid bouncing (this number will depend on the speed of the machine)
delay(300);
//RFID loop
// Look for new cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been read
if ( ! rfid.PICC_ReadCardSerial())
return;
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
if (rfid.uid.uidByte[0] != nuidPICC[0] ||
rfid.uid.uidByte[1] != nuidPICC[1] ||
rfid.uid.uidByte[2] != nuidPICC[2] ||
rfid.uid.uidByte[3] != nuidPICC[3] ) {
Serial.println(F("A new card has been detected."));
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
Serial.println(F("The NUID tag is:"));
Serial.print(F("In hex: "));
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
Serial.print(F("In dec: "));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
}
else Serial.println(F("Card read previously."));
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
Not sure what the problem is but you are overflowing nuidPICC by putting 4 bytes in it, only 3 are allocated.
You only need to call lcd.begin() once, so leave the call in setup() and remove it elsewhere.
Consider using constant bytes for the machine states instead of Strings, 0=on, 1=off, for example.