# Calculating and displaying detection time

I’m trying to use a modern devices to calculate the time that is accumulating when a light is on. The sensor sends a voltage to the Arduino (I’m using a Leonardo).

the first step is for a quiescent voltage to be calculated during the time that the sensor is turned on. Then, when the light is turned on, the sensor sends a higher voltage, which is read by the Arduino for a few seconds to determine the highest voltage when the light is off, and the sensor is sending a quiescent voltage. Then, it monitors the voltage from the sensor, and when the voltage is higher than the highest quiescent voltage, it starts a timer, and stops the timer when the voltage drops to within 10% of the lowest quiescent voltage.

It is not working properly.

1. I want it to report the maximum quiescent voltage just one time after 14 seconds. it doesn’t do that. it keeps printing the line “Please wait while calibration is printed, thanks” for 14 seconds, then reports the max and min quiescent voltage.

2. then it keeps printing “The current voltage is X.XXXX”

3. the it alternates printing the lines “The current voltage is X.XXXX” and “the time the current was on is XXXX”

4. occasionally, it will print "The current increase started at: “XXXX”

Her is what I want it to do:

1. Print the “Please wait while calibration is printed, thanks” just one time.

2. print the elapse time that the light was on after the light is turned of.

this seems like it should be a simple thing for me to program, and I am sure there must be a simple solution, but I can’t find it.

can anyone help?

Thanks

Justin

``````/* this records the length of time a current is on, and calculates the total time it is on.*/

long startTime  ;
long stopTime  ;
long elapseTime  ;
float qvolts;
float hiqvolts = 0;
float loqvolts = 1;
float monvolt = 0;
void setup() {
Serial.begin(9600);
}
// put your setup code here, to run once:

void loop() {
if (millis()>=1000 && millis() <=1100) {
Serial.println("Please wait while calibration is completed, thanks");
}
int rawmonvolts;
int rawvolts;

if (millis()<= 10000 && millis() <=14000){
qvolts = rawvolts / (3.3 * 1023);
hiqvolts = max(hiqvolts, qvolts);
loqvolts = min(loqvolts, qvolts);
Serial.print ("The maximum quiescent voltage is ");
Serial.println(hiqvolts, 4);
Serial.print("The minimum quiescent voltage is ");
Serial.println(loqvolts, 4);
}
if (millis()>140000){;
float monvolt = rawmonvolts / (3.3 * 1023);
float himonvolt = max(monvolt, hiqvolts);
Serial.print ("The current voltage is ");
Serial.println (monvolt, 4);
if (himonvolt > hiqvolts) {
startTime = millis();
Serial.print("The current increase started at: " );
Serial.println(startTime);
float lomonvolt = min(monvolt, lomonvolt);
if (lomonvolt <= loqvolts * 1.1)
{
stopTime = millis();
elapseTime = (stopTime - startTime);
Serial.print ("The current was on ");
Serial.println (elapseTime);
}
}
}

delay(2000);
}

// put your main code here, to run repeatedly:

code]
``````
``````if (millis()>=1000 && millis() <=1100)
``````

How long will that be true? How many times can loop repeat in that amount of time?

If you only want that happening once at startup then why not put it in setup?

The demo Several Things at a Time illustrates the use of millis() to manage timing. The important thing is to use the difference between two values of millis() rather than any absolute value.

…R

Delta_G:

``````if (millis()>=1000 && millis() <=1100)
``````

How long will that be true? How many times can loop repeat in that amount of time?

If you only want that happening once at startup then why not put it in setup?

Tried putting the initial quiescent voltage determination in setup, and it did not work. also tried simple "Serial.print(“hello”) and that did not work.

Again, I am trying to establish a Quiescent voltage, just once. and the latest attempt is to capture it in a very early stage. the method I tried was to capture it when it has ran for only a second, and I found the 100 millisecond time span to be not to long or short so that the quiescent voltage is calculated only once.

once the quiescent voltage is established, the sketch will monitor the voltage and when it is above the maximum quiescent voltage, it will assume the power is on to the appliance that the sensor is attached to. once the voltage drops back to near the minimum quiescent voltage, the sketch will assume the appliance is off, and report how long the appliance was on.

Thanks

Justin

Tried putting the initial quiescent voltage determination in setup, and it did not work.

There's that uselessly lame phrase "it didn't work" again. The code you didn't post DID work. It just didn't do what you expect, because your expectations are wrong. How they are wrong we can't tell you, because you didn't post the code, you didn't explain what it actually does, and you didn't explain what you expected it to do.

PaulS:
There’s that uselessly lame phrase “it didn’t work” again. The code you didn’t post DID work. It just didn’t do what you expect, because your expectations are wrong. How they are wrong we can’t tell you, because you didn’t post the code, you didn’t explain what it actually does, and you didn’t explain what you expected it to do.

Please see my initial post for the code that does not work. I am not interested in semantics here, If the code in a sketch this simple does not produce the intended result, it is not working. I also explained what I want the code to do, and what it is producing.

for this last post, I did this simple sketch, which produced nothing. there was no line printed in the serial monitor.

I did this in setup:

void setup(9600){
Serial.begn(9600);
Serial.Println(“Hi, This is testing the startup”) ;

}

the first step

STARTUP_MODE

is for a quiescent voltage to be calculated during the time that the sensor is turned on. Then,

POWER_ON_MODE

when the light is turned on, the sensor sends a higher voltage, which is read by the Arduino for a few seconds to determine the highest voltage when the light is off, and the sensor is sending a quiescent voltage. Then,

MONITOR_MODE

it monitors the voltage from the sensor, and when the voltage is higher than the highest quiescent voltage, it starts a timer, and stops the timer when the voltage drops to within 10% of the lowest quiescent voltage.

you want to do something like this, but all of the readings you are doing, I didn't look at...

``````enum DeviceMode {
STARTUP_MODE,
POWER_ON_MODE,
MONITOR_MODE,
UNKNOWN_MODE
} mode = STARTUP_MODE;

DeviceMode lastMode = UNKNOWN_MODE;

uint32_t startMillis;

int rawvolts;
double qvolts;
double hiqvolts = 0;
double loqvolts = 1;

void setup()
{
Serial.begin(9600);
startMillis = millis();
}

void loop()
{
if (lastMode != mode)
{
switch (mode)
{
case STARTUP_MODE:
Serial.println("Startup Mode...");
break;
case POWER_ON_MODE:
// do what happens when you exit Startup mode
Serial.println("Power On Mode...");
break;
case MONITOR_MODE:
// do what happens when you exit Monitor mode
qvolts = rawvolts / (3.3 * 1023);
hiqvolts = max(hiqvolts, qvolts);
loqvolts = min(loqvolts, qvolts);
Serial.print ("The maximum quiescent voltage is ");
Serial.println(hiqvolts, 4);
Serial.print("The minimum quiescent voltage is ");
Serial.println(loqvolts, 4);
Serial.println("Monitor Mode...");
break;
}
lastMode = mode;
}
switch (mode)
{
case STARTUP_MODE:
if (millis() - startMillis > 15000) //timeout
{
startMillis = millis();
mode = POWER_ON_MODE;
}

break;
case POWER_ON_MODE:
if (millis() - startMillis > 15000) //timeout
{
startMillis = millis();
mode = MONITOR_MODE;
}
break;
case MONITOR_MODE:
//
// put your state change detection code here
//
break;
}
}
``````

I did this simple sketch, which produced nothing. there was no line printed in the serial monitor.

Which Arduino are you using? What baud rate is the serial monitor app set at?

We can make progress when you post information like this. We can not when you say "it doesn't work".

Griiker: I did this in setup:

void setup(9600){ Serial.begn(9600); Serial.Println("Hi, This is testing the startup") ;

}

No, you didn't do that. That wouldn't have compiled. You seem to have stuck something extra in the parenthesis on the first line. How about giving us the real code that caused the problem.

Look, if you don't like the fact that we want to see the actual code that produced the actual problem and a real description of what the problem was then you don't have to come here for help. I mean really, when you're getting free help you don't try to dictate the terms. Ever hear the expression, "You don't look a gift horse in the mouth." Do you know what that expression means?

We ask for a complete code that demonstrates the problem. Not a snippet. Not part of it. Post the whole thing. If you know where the problem is then leave us alone and fix it. If you don't know where the problem is then don't pretend that you know which part of the code we'd need to see in order to fix it. Sometimes we want to be able to copy and paste it into the IDE and try it for ourselves to see if we get the same issue. If we have to rewrite part of it then chances are we'll just skip you over and help someone else. Or what if we do rewrite it but we do it right and the problem goes away. So we just post back that your snippet is fine and you get no answer. Would that be helpful?

I like to turn up warnings which will point out mistakes like this:

``````/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.18/cores/arduino/main.cpp: In function 'main':
/Users/john/Documents/Arduino/sketch_jun03a/sketch_jun03a.ino:45:25: warning: 'lomonvolt' may be used uninitialized in this function [-Wmaybe-uninitialized]
float lomonvolt = min(monvolt, lomonvolt);
^
/Users/john/Documents/Arduino/sketch_jun03a/sketch_jun03a.ino:45:13: note: 'lomonvolt' was declared here
float lomonvolt = min(monvolt, lomonvolt);
^
Sketch uses 4352 bytes (13%) of program storage space. Maximum is 32256 bytes.
Global variables use 418 bytes (20%) of dynamic memory, leaving 1630 bytes for local variables. Maximum is 2048 bytes.
``````

You can't initialize a local variable using based on its own value. It has no value. This indicates an error in logic that might be responsible for your unexpected results.

Delta G:

I included the code for my original problem in my initial post on this topic. my original problem.

When I am replying to a post, I can't place code in it, not sure how to do that. so I retyped it in, I apologize for this, however, in my haste (I have other tasks to perform besides this programming) I did not type it in correctly. Still, the code was simple enough, I thought I could do it without so much going wrong with the transcribing.

here is what the basic problem is that I am having with putting Serial.print("...."); in setup:

void setup(){

Serial.begin(9600); Serial.print("hello"); }

when I try placing the above with proper syntax, spelling etc., it will compile, however, it will not print the 'hello' line in the monitor. it does not show up in the monitor. yet it still compiles.

Look, if you don't like the fact that we want to see the actual code that produced the actual problem and a real description of what the problem was then you don't have to come here for help. I mean really, when you're getting free help you don't try to dictate the terms. Ever hear the expression, "You don't look a gift horse in the mouth." Do you know what that expression means?

I am not sure where I appeared to be dictating terms. perhaps you could help me out and show me where, and I can explain why I used what language I did, and please understand, that I am not trying to dictate terms with my initial request for help.

We ask for a complete code that demonstrates the problem. Not a snippet. Not part of it. Post the whole thing.

While I can appreciate that I made the typing mistake in the reply, I did so thinking that such a simple bit of code would have been easy enough to transcribe, again, I apologize for the typographical errors, I'm using 2 computers, one of which is not internet connected to do coding with (its a hard thing to explain) so I have to do some gymnastics to get code into my postings. I felt this code was enough, mostly because this is all the code I have entered into the setup section, the void loop() part has nothing but opening and closing brackets.

If you know where the problem is then leave us alone and fix it.

I do not know where the code is, which is why I am asking for help. I have tried many things to get my main sketch to work, and I have not been successful.

If you don't know where the problem is then don't pretend that you know which part of the code we'd need to see in order to fix it. Sometimes we want to be able to copy and paste it into the IDE and try it for ourselves to see if we get the same issue. If we have to rewrite part of it then chances are we'll just skip you over and help someone else. Or what if we do rewrite it but we do it right and the problem goes away. So we just post back that your snippet is fine and you get no answer. Would that be helpful?

As I have stated, I placed the entirety of my code in my intial post, if need be, I can do it again under a new topic. I had assumed that copy and placing the code into your IDE was the intent, so I placed the entirety of it in my initial post where it shows to do so.

I certainly do not expect an rewrite, but some pointers on what to do. as I have said, I have tried several different iterations, if statements, switches, and so on, and I was getting now where.

Finally, Delta_G, I can understand why you may think I am out of line, unfortunately, this often happens with emails, posts, replies etc. they do not convey the true spirit nor the emotion, nor the efficiency in communication that a face to face or a phone conversation. this inevitably leads to the type of miscommunication we have here, and while I am alarmed by your response, I have tried to imagine your own frustration with me and I have tried to clarify where I am coming from, and understand where you are coming from as well.

SO I do thank you for your effort.

Justin

[/quote]

Thanks Bulldoglowell, I will try your approach, I really do appreciate it.

Justin

I'm using a Leonardo)

however, it will not print the 'hello' line in the monitor

Could those statements be connected in some way?

if you want something to be done just one time, make a boolean like this:

``````static bool oneStep = false;
``````

// put it outside any function so to be a global variable, or use the static keyword if you declare it inside a function and want to retain the value between calls.

and then do something like this in your main code:

``````if ( !oneStep && Time_is_ok ){ oneStep = true; DO other stuff here you like to get executed just once like: Serial.print("Hello"); }
if( oneStep && Time_is_not_ok ) oneStep = false;
``````

Griiker:
When I am replying to a post, I can’t place code in it, not sure how to do that.

No offense, but if copy-paste is beyond your ability then you’ve got a lot of learning to do before you attempt programming.

Delta_G, I have explained in an earlier reply that I am working with two separate machines, the one I'm using for sketching is not connected to the internet, this makes cutting and pasting impossible.

Furthermore, the tone of your responses are belittling, and insulting, while I'm letting you have the last word, as I will not dignify it with a response should it be antagonistic in nature.

Justin

Griiker: Furthermore, the tone of your responses are belittling, and insulting, while I'm letting you have the last word, as I will not dignify it with a response should it be antagonistic in nature.

karma++

this is should be a happy learning place

Griiker: I am working with two separate machines, the one I'm using for sketching is not connected to the internet, this makes cutting and pasting impossible.

USB memory stick? Just sayin'

Griiker:
the one I’m using for sketching is not connected to the internet, this makes cutting and pasting impossible.

Why not do the programming on the machine that is connected to the internet until you get your problem sorted out?

Alternatively, do some VERY careful proof reading before posting a Reply. The compiler won’t accept silly typos and I don’t see why you should expect the humans who are trying to help you to do so.

…R

Robin2: Why not do the programming on the machine that is connected to the internet until you get your problem sorted out?

...R

+1