hey there i am working on this project initiated by an NGO here to supply drinking water to poor people and avoid wastage of water. the theory is when an registered RFID is detected arduino asks for pressing green button, it opens the valve and keeps it open till red button is pressed. but the problem is after the red button has been pressed the code should restart but my code just stuck there...and manually reset is required which i don't want please let me know if there is a bug in the code. the code is attached with the post.
a12.ino (3.5 KB)
system
August 18, 2014, 10:53am
2
No need to attach such a short sketch, just post it.
I auto-formatted it first.
#include <SoftwareSerial.h>
SoftwareSerial id20(3,9);
////////////////////////////////////////
String customer1 = "66006BF25AA5";
String customer2 = "66006BFE51A2";
String customer3 = "66006C0CDFD9";
String customer4 = "66006BF5FB03";
String customer5 = "66006BF96793";
/////////////////////////////////////////
String testCard;
char testtag[12];
int indexnumber = 0;
char tagChar;
/////////////////////////////////////////
int counter = 0;
int hallsensor = 9;
volatile int NbTopsFan = 0;
////////////////////////////////////////
boolean redButton = 6;
boolean greenLed = 7;
boolean greenButton = 8;
boolean redLed = 5;
boolean valve = 4;
////////////////////////////////////////
void setup()
{
Serial.begin(9600);
id20.begin(9600);
attachInterrupt(0, rpm, RISING);
// inputs
pinMode(hallsensor,INPUT);
pinMode(redButton,INPUT);
pinMode(greenButton,INPUT);
// outputs
pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(valve,OUTPUT);
// initialization
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(valve, LOW);
}
////////////////////////////////////////
void approved()
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
Serial.println("Card approved");
Serial.println("your card number is ");
Serial.println(testCard);
delay(1000);
digitalWrite(greenLed, LOW);
Serial.println("press the green button");
stopUntill(); //wait for green signal
digitalWrite(valve, HIGH);
digitalWrite(greenLed, HIGH);
isr();
stopAll();
}
//////////////////////////////////////////////////
void stopAll()
{
digitalWrite(valve, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
delay(400);
digitalWrite(greenLed, LOW);
NbTopsFan=0;
}
//////////////////////////////////////////////////
void stopUntill()
{
while(digitalRead(greenButton) == LOW)
{
delay(300);
}
}
/////////////////////////////////////////////
void(* resetFunc) (void) = 0; //declare reset function @ address 0
//////////////////////////////////////////////////
void isr()
{
delay(50);
while(digitalRead(redButton)==LOW)
{
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
interrupts(); //Enables interrupts
delay (1000); //Wait 1 second
Serial.print (NbTopsFan);
Serial.println(" NbTopsFan");
noInterrupts(); //Disable interrupts
Serial.print (NbTopsFan);
Serial.println(" NbTopsFan");
counter=counter+NbTopsFan;
Serial.print(counter);
Serial.println (" ticks");
}
resetFunc(); //call reset
}
/////////////////////////////////////////////
void notApproved()
{
digitalWrite(redLed, LOW);
Serial.println("Card not Approved");
digitalWrite(valve, LOW);
delay(500);
digitalWrite(redLed, HIGH);
delay(1000);
digitalWrite(redLed, LOW);
}
/////////////////////////////////////////////////
void rpm ()
{
NbTopsFan++;
}
////////////////////////////////////////////////
void readTag()
{
tagChar = id20.read();
if (indexnumber != 0) // never a zero in tag number
{
testtag[indexnumber - 1] = tagChar;
}
indexnumber++;
if (indexnumber == 13 ) // end of tag number
{
indexnumber = 0;
testCard = String(testtag);
if (testCard.equals(customer1))
{
approved();
}
if (testCard.equals(customer2))
{
approved();
}
if (testCard.equals(customer3))
{
approved();
}
if (testCard.equals(customer4))
{
approved();
}
if (testCard.equals(customer5))
{
approved();
}
else
notApproved();
}
}
/////////////////////////////////////////////////
void loop()
{
readTag();
}
the "reset function at 0" certainly starts the code back at the very beginning, but it doesn't reset any of the things that are reset by a power-on...
http://forum.arduino.cc/index.php/topic,12874.0.html
And if you read the rest of that thread you'd see why jumping to address 0 is not really a reset and not recommended.
All that does is start the code over, it doesn't reset the chip - an important distinction.