The problem is my if statement is not activating for some reason and I don't know how to make it activate.
The first if statement in the loop section is the one not working.
So I have a geiger counter and a timer. Every minute, I want the amount of counts to reset, and an updated minute. I don't count about seeing the amount of millis() that have passed. For instance I want the output to be:
Minute: 1
Counts: 6
Minute 2
Counts: 2
//definitions
int Geiger= 0;
int count = 0;
int minute =1;
unsigned long steptime;
const long interval = 60000;
unsigned long PastTime=0;
unsigned long CurrentTime=millis();
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (CurrentTime >= 60000) {
CurrentTime=PastTime;
count=0;
minute=minute+1;
Serial.print("Minute:");
Serial.println(minute);
}
unsigned long CurrentTime = millis();
Serial.print("Time: ");
Serial.println(CurrentTime);
if(Serial.available()>0) {
Geiger=Serial.read();
Serial.println(Geiger);
Geiger=(Geiger-48);
if (Geiger>0) {
count+=1;
}
}
Serial.print("Counts: ");
Serial.println(count);
}
Lose the "unsigned long" on line 22 (but not the rest of the line). That creates a new local variable and you already have that variable defined at the top of the program.
It looks like you have your Geiger counter connected to Serial as well as trying to use Serial for the Arduino Serial Monitor. That is only going to cause confusion. Assuming you are using an Uno use SoftwareSerial to make a separate serial for the Geiger counter.
Have a look at how to use millis() in several things at a time. The way you use it millis() will only be 60000 once every 49 days.
Please help me about this. I want to reset the counter for every 5 seconds and i just modified this code.
// digital pin 2 has a ZapperInput attached to it. Give it a name:
int ZapperInput = 2;
int ZapperState = 0;
int ZapperStateCounter = 0;
int seconds = 5;
unsigned long steptime;
const long interval = 5000;
unsigned long PastTime=0;
unsigned long CurrentTime=millis();
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the ZapperInput's pin an input:
pinMode(ZapperInput, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int ZapperState = digitalRead(ZapperInput);
// delay in between reads for stability
if (CurrentTime >= 5000) {
CurrentTime=PastTime;
ZapperStateCounter=0;
seconds=seconds+5;
Serial.print("Seconds:");
Serial.println(seconds);
}
if (ZapperState == HIGH){
ZapperStateCounter++;
delay(300);
Serial.println(ZapperStateCounter);
}}
When using millis() for timing you should save the millis() value when a start action occurs. Then each time through loop() check whether the required period has elapsed by subtracting the start time from the current millis() value and act appropriately if it has.
When using millis() for timing you should save the millis() value when a start action occurs. Then each time through loop() check whether the required period has elapsed by subtracting the start time from the current millis() value and act appropriately if it has.