Counting Problem

Hi,

I am working on a project to monitor hand hygiene compliance. I need to count the amount of compliance and noncompliance instances of an individual healthcare worker.

While I was working on the scenario on Bed A -> handwash -> Bed A -> Leave -> No handwash
The first round, the compliance count was correct at the end of the code.
I went ahead to do a second round of testing. However, the compliance count returned an extremely large value after I copied tagString to storeString in void loop(). (There was no such problem in the first round of testing)

I am not sure why there's such a large jump of about 14000 in compliance count. The count in the 3rd test is the same as the 2nd.

Thanks
Bob

#include <VirtualWire.h>
const int receive_pin = 2; //pin number to change accordingly
int RFIDResetPin = 13;
int buttonState = 0;
int buttonPin = 8;
int ledPin = 11;
int handWash = 0; //Status of Handwash
int bed = 0; //Status of Current Bed
int newBed = 0; //Status of a change in Bed
int currentBed = 0; //Status of current Bed (only after handwash)
int readByte;
char tagString[13]={0}; //A string to assign tags’s serial numbers
char storeString[13] = {0}; //to store the previous tagString value of bed A/B
long lastMillis = 0; //variable to store last time LED was updated
unsigned long currentMillis;
int compliance = 0;
int noncompliance = 0;
int store = 0;
int changeCount = 0;
///////Receiver Declaration/////////
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
////////////////////////////////////

//Register your RFID tags here
char bedTagA1[13] = “6F005C6280D1”; //bed A tag
char bedTagA2[13] = “6F005C74783F”; //bed A tag
char bedTagB1[13] = “6F005C6DF1AF”; //bed B tag
char bedTagB2[13] = “6F005CB57EF8”; //bed B tag
char dispTag[13] = “6F005C70ACEF”; //dispenser tag

void setup()
{
Serial.begin(9600);

pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);

pinMode(ledPin, OUTPUT);
pinMode(buttonPin,INPUT);
///////////////////Receiver Code///////////////////////
vw_set_rx_pin(receive_pin);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
//////////////////////////////////////////////////////
}

void loop()
{
currentMillis = millis();
buttonState = digitalRead(buttonPin);
if(strlen(tagString) == 0 && strlen(storeString) == 0) //no bed and previous bed
{
readTag(); //read for RFID tag
checkLED();
checkLast();
}
if(compareTag(tagString,dispTag) && (buttonState == HIGH)/vw_get_message(buf, &buflen)/ && handWash == 0)
{
Serial.println(“Bed: Handwashed (1)”);
offAlarm();
handWash = 1;
newBed = 0;
lastMillis = currentMillis;
}
if((compareTag(tagString, bedTagA1) || compareTag(tagString,bedTagA2))) //bed A
{
bed = 1; //indicates bed A
if(strlen(storeString) == 0)
{
for(int i = 0; i < strlen(tagString) ; i++)
{
storeString = tagString*; //make a copy of tagString into storeString*
* }*
* }*
* lastMillis = currentMillis; //the time of approach*
* }*
* else if((compareTag(tagString, bedTagB1) || compareTag(tagString,bedTagB2))) //bed B*
* {*
* bed = 2; //indicates bed b*
* if(strlen(storeString) == 0)*
* {*
* for(int i = 0; i < strlen(tagString) ; i++)*
* {*
storeString = tagString*; //make a copy of tagString into storeString*
* }*
* }*
* lastMillis = currentMillis; //the time of approach*
* }*
* while(strlen(tagString) != 0)*
* {*
* if(compareTag(tagString,dispTag))*
* {*
* clearTag(tagString);*
* break;*
* }*
* else*
* {*
* gotBed(bed);*
* }*
* }*
* while(strlen(tagString) == 0 && strlen(storeString) != 0) //no bed but have previous bed*
* {*
* bed = -1;*
* noBed();*
* }*
* resetReader(); //reset the RFID reader*
}
void gotBed(int bed)
{
* currentMillis = millis();*
* checkHandwash();*
* if(newBed != bed && handWash == 0) //Transitioning into bed A without handwash*
* {*
* Serial.println(“Bed: No handwash (1)”);*
* newBed = bed; //indicates a new bed is detected*
* lastMillis = currentMillis;*
* onAlarm();*
* }*
* if(newBed == bed && handWash == 0) //when a change in bed is detected*
* {*
* if(currentMillis - lastMillis > 10000UL) //needs to handwash within 10 seconds*
* {*
* Serial.println(“Bed: No handwash (2)”);*
* noncompliance++;*
* lastMillis = currentMillis;*
* newBed = 0;*
* offAlarm();*
* clearTag(tagString);*
* clearTag(storeString);*
* }*
* }*
* if(newBed != bed && handWash == 1) //Transitioning into bed A with handwash*
* {*
* if(currentBed != bed && currentBed == 0) //First Bed*
* {*
* Serial.println(“Bed: Handwashed (2)”);*
* currentBed = bed; //indicates hand is washed for first Bed*
* changeCount = 0;*
* changeCount++; //increase number of bedstate changes*
* lastMillis = currentMillis;*
* clearTag(tagString);*
* }*
* else if(currentBed != bed && currentBed > 0 && changeCount == 1) //After dispenser, HCW can approach either bed (allows bed change)*
* {*
* Serial.println(“Bed: First Bed”);*
* if(changeCount < 2)*
* {*
* compliance++;*
* changeCount++; //increase change count to disallow next bed change*
* Serial.println(compliance);*
* }*
* currentBed = bed; //handwash identified for currentBed*
* lastMillis = currentMillis;*
* clearTag(tagString);*
* }*
* else if(currentBed != bed && currentBed > 0 && changeCount == 2) //2nd change*
* {*
* Serial.println(“Bed: Change in Bed”);*
* changeCount = 0; //resets change counter*
* handWash = 0; //resets handwash*
* currentBed = 0; //resets currentbed*
* lastMillis = currentMillis;*
* }*
* else if(currentBed == bed) //same bed after handwash //////////////////////////else if/////////////////////////////////*
* {*
* Serial.println(“Bed: Safe to Proceed”);*
* if(changeCount < 2)*
* {*
* compliance++;*
* changeCount++;*
* Serial.println(compliance);*
* }*
* lastMillis = currentMillis;*
* clearTag(tagString);*
* }*
* }*
}
void noBed()
{
* currentMillis = millis();*
* readTag();*
* resetReader();*
* if(strlen(tagString) != 0)*
* {*
* lastMillis = currentMillis;*
* }*
* else if(strlen(tagString) == 0) //disable “leave” after dispenser*
* {*
* if(currentMillis - lastMillis > 6000UL && changeCount != 1) //more than 6 seconds after leaving bed*
* {*
* if(newBed != bed && currentBed == 1) //Transitioning from bed A to no bed*
* {*
* Serial.println(“Bed A: Leave”);*
* newBed = -1;*
* handWash = 0; //resets the timer*
* }*
* else if(newBed != bed && currentBed == 2) //Tranistioning from bed B to no bed*
* {*
* Serial.println(“Bed B: Leave”);*
* newBed = -1;*
* handWash = 0; //resets the timer*
* }*
* onAlarm(); //hand wash is needed when HCW leave bed for more than 6 seconds*
* changeCount = 0;*
* newBed = 0;*
* currentBed = 0; //indicates no bed*
* lastMillis = currentMillis;*
* clearTag(storeString);//clear existing memory of previous bed*
* }*
* else if(currentMillis - lastMillis > 6000UL && changeCount == 1)*
* {*
* Serial.println(“Dispenser: Leave”);*
* changeCount = 0;*
* handWash = 0;*
* bed = 0;*
* currentBed = 0;*
* newBed = 0;*
* lastMillis = currentMillis;*
* clearTag(tagString);*
* clearTag(storeString);*
* }*
* }*
}
void checkHandwash()
{
* currentMillis = millis();*
* buttonState = digitalRead(buttonPin); //read the state of the button pin *
if(buttonState == HIGH/vw_get_message(buf,&buflen)/) //when button is pressed
* {*
* clearTag(tagString); //clear previous Bed Tag*
* readTag(); //read dispenser tag*
* resetReader();*
* if(compareTag(tagString,dispTag))*
* {*
* offAlarm(); //off alarm*
* handWash = 1; //indicate hand is washed*
* newBed = 0; //newBed = 0 as current bed is Bed A, so no new bed*
* lastMillis = currentMillis; //store time when dispenser was last used*
* clearTag(tagString); //clear dispenser tag*
* if(strlen(storeString) != 0)*
* {*
* for(int i = 0; i < strlen(storeString) ; i++)*
* {*
tagString = storeString*; //copy stored string to tagString*
* }*
* }*
* }*
* }*
}
void checkLED()
{
* currentMillis = millis();*
* while(digitalRead(ledPin) == HIGH) //after leaving bed, alarm ons*
* {*
* checkHandwash();*
* if(currentMillis - lastMillis < 10000UL && handWash == 1) //if hand wash within 10 seconds after alarm is switched on*
* {*
* Serial.println(“Bed: Leave Compliance”);*
* compliance++; //increase compliance count*
* handWash = 0;*
* lastMillis = currentMillis;*
* offAlarm();*
* clearTag(tagString);*
* }*
* else if(currentMillis - lastMillis > 10000UL && handWash == 0) //if alarm on for 10 seconds, record non-compliance*
* {*
* Serial.println(“Bed: Leave Noncompliance”);*
* noncompliance++;*
* lastMillis = currentMillis;*
* offAlarm();*
* } *
* }*
}

void checkLast()
{
* currentMillis = millis();*
* if(currentMillis - lastMillis > 6000UL)//check for hand wash within the last 6 seconds*
* {*
* if(handWash == 1) //if hand wash is done more than 6 seconds ago, hand wash needs to be done again*
* {*
* Serial.println(“Reset Handwash”);*
* handWash = 0;*
* }*
* lastMillis = currentMillis; //resets the time interval*
* } *
}

The code is too long for me to check everything.

Can you modify your post and use [code][/code] tags around your sketch ?
At this moment, I can not read the “storeString [ i ]”.

When using millis(), please use unsigned long for the variables, “lastMillis” is a long.