### Author Topic: Arduino Schematic HELP!  (Read 8311 times)

0 Members and 1 Guest are viewing this topic.

#### alsetalokin4017

• Super Contributor
• Posts: 1757
• Country:
##### Re: Arduino Schematic HELP!
« Reply #25 on: October 01, 2016, 12:50:27 pm »
how would i use millis in a sketch like this:

const int Button = A0;
const int Light = 8;

int timeVal = 0;
int buttonstate = 0;

void setup() {
pinMode(Light, OUTPUT);
pinMode(Button, INPUT);
}

void loop() {

if(timeVal > 0){
digitalWrite(Light, HIGH);
}
else{
digitalWrite(Light, LOW);
}
if(buttonstate == HIGH){
timeVal = 5;
}
}

OK... first let  me offer a suggestion. Change the Light pin to 13 for easy testing. Most Arduino boards already have a built-in LED and resistor on Pin 13. So it's easy to test this code on an Uno, all you need is a jumper to pretend to be a Button, connecting A0 to either 5V or GND.
Now... remember what I said about telling it what to do if the buttonstate is _not_ HIGH? You need to place an else statement after the last if statement. As it is, the Light pin stays high no matter what the buttonstate is.
So I've changed your code this slight amount:
Code: [Select]
const int Button = A0;const int Light = 13;int timeVal = 0;int buttonstate = 0;void setup() {  pinMode(Light, OUTPUT);  pinMode(Button, INPUT);}void loop() {  buttonstate = digitalRead(Button);  if(timeVal > 0)  digitalWrite(Light, HIGH);  else  digitalWrite(Light, LOW);  if(buttonstate == HIGH)  timeVal = 5;  else timeVal = 0;}(scroll the code window if you can't see the whole sketch)

Notice with this sketch the Light state follows the buttonstate. When the button is HIGH (at 5V) the Light (LED on pin 13) is ON and when the button is LOW (at ground) the Light is OFF.  See what happens if you comment out that last "else" statement.

Now look at the way Psi used the millis() statement in his example. Millis() returns the number of milliseconds since the sketch started. So it's a constantly increasing number until it overflows at about 50 days and restarts from zero. The only time your sketch would need to worry about overflow is if the overflow instant happened during the sketch's time delay. As long as the entire time delay is within the 50 day cycle you are fine. Probably. (I'm sure the purists will be all over me for sloppy programming; yes, you should deal with the overflow gracefully, but there is plenty of time for that after you get something working! In 50 days time, you'll know how to deal with millis() overflow.)
So look at how Psi used the millis() to set a "start" time, test if "now" is a certain time later than "start", do something if it is, then reset "start" to the new "now". Play around with that in your sketch and see what you get.
« Last Edit: October 01, 2016, 12:52:16 pm by alsetalokin4017 »
The easiest person to fool is yourself. -- Richard Feynman

#### zapta

• Super Contributor
• Posts: 6004
• Country:
##### Re: Arduino Schematic HELP!
« Reply #26 on: October 01, 2016, 12:51:22 pm »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++.
So what is the most similar programming language to Arduino?

It's not 'similar', the arduino programming language is C++, period.

When you program in C++ you also use libraries, standard C++ libraries,  your own libraries, and other people libraries. In the case of Arduino the language is C++ and it comes packaged not just with the C++ libraries but also with libraries that were written by the Arduino people.
Drain the swamp.

• Regular Contributor
• Posts: 149
• Country:
##### Re: Arduino Schematic HELP!
« Reply #27 on: October 01, 2016, 01:46:52 pm »
If you're using millis() you should read https://www.arduino.cc/en/Reference/Millis , especially:

Quote
Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.

So you need to add an overflow detection.

On the subject of overflow detection, here's a kind-of-tested code snippet I wrote a while back for Arduino that gracefully handles overflows during delay generation:

Code: [Select]
void readDelay(unsigned long start, unsigned long delay_us){     if (start == 0)    start = micros();  unsigned long target = start + delay_us;  //We might have overflowed, so do this carefully:  if (target > start) {    //no overflow    while (micros() < target)      continue;  } else {    //We overflowed: target is actual_target - 2^32, so current is larger than it    //We can treat this case by being clever with signs: if interpreted as signed    //  numbers, right now current < 0 and target > 0, with current increasing over    //  time. So things will work out fine for us if we use signed comparisons here.    //This hasn't been tested as I don't know how to get the Arduino into this     //  state without watching it for an hour.    Serial.print("Timer overflow!\n");    while ((signed long)(micros()) < (signed long)target)      continue;  }}
(I post this not because it's helpful to the original poster -- it's quite clearly not useful right now -- but because 1. I have an unhealthy fondness for cleverly efficient code that usually bites me in the rear when I have to debug my own creations and 2. I must admit to being disappointed, after clicking on this thread title, to see that it wasn't actually about the Arduino hardware schematics. There is a special place in my heart for schematics that randomly mix English and Italian, and no, it is not a nice, happy, fun place.)

#### skillz21

• Frequent Contributor
• Posts: 266
• Country:
##### Re: Arduino Schematic HELP!
« Reply #28 on: October 01, 2016, 05:48:56 pm »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++.
So what is the most similar programming language to Arduino?

It's not 'similar', the arduino programming language is C++, period.

When you program in C++ you also use libraries, standard C++ libraries,  your own libraries, and other people libraries. In the case of Arduino the language is C++ and it comes packaged not just with the C++ libraries but also with libraries that were written by the Arduino people.
Isn't C++ one of the hardest programming languages?
"It's only a stupid man, that believe stupid quotes they find on the internet" - Me, 2017

#### skillz21

• Frequent Contributor
• Posts: 266
• Country:
##### Re: Arduino Schematic HELP!
« Reply #29 on: October 01, 2016, 06:08:59 pm »
I did some more research into millis. Do I do it like, if any movement is found, the current millis value is recorded as a variable and the light turns on. then, once the millis value increases by about 30 seconds, the light is turned off. Can i do that?
"It's only a stupid man, that believe stupid quotes they find on the internet" - Me, 2017

#### IanB

• Super Contributor
• Posts: 9033
• Country:
##### Re: Arduino Schematic HELP!
« Reply #30 on: October 01, 2016, 06:18:24 pm »
So you need to add an overflow detection.

On the subject of overflow detection, here's a kind-of-tested code snippet I wrote a while back for Arduino that gracefully handles overflows during delay generation:

There is no need to check for overflow when using unsigned counters. If you subtract "previous time" from "current time" in unsigned arithmetic the result will always be the desired elapsed time even if the timer has wrapped around in the mean time. See here for example: http://www.microchip.com/forums/m723128.aspx
I'm not an EE--what am I doing here?

#### skillz21

• Frequent Contributor
• Posts: 266
• Country:
##### Re: Arduino Schematic HELP!
« Reply #31 on: October 01, 2016, 07:08:01 pm »
would this kind of thing work? (it doesn't work, it says that A0 was not declared in this scope.)

const int Button = A0;
const int Light = 8;

unsigned long time;

int timeVal = 0;
int buttonstate = 0;
int timeoffVal = timeVal + 30000

void setup(){
pinMode(Light, OUTPUT);
pinMode(Button, INPUT);
}
void loop(){

if(buttonstate == HIGH){
timeVal = time;
digitalWrite(Light, HIGH);
}
else{
}

if(timeoffVal = time) {
timeoffVal = 0;
digitalWrite(Light, LOW);
}
}
"It's only a stupid man, that believe stupid quotes they find on the internet" - Me, 2017

#### zapta

• Super Contributor
• Posts: 6004
• Country:
##### Re: Arduino Schematic HELP!
« Reply #32 on: October 01, 2016, 07:51:58 pm »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++.
So what is the most similar programming language to Arduino?

It's not 'similar', the arduino programming language is C++, period.

When you program in C++ you also use libraries, standard C++ libraries,  your own libraries, and other people libraries. In the case of Arduino the language is C++ and it comes packaged not just with the C++ libraries but also with libraries that were written by the Arduino people.
Isn't C++ one of the hardest programming languages?
It has many features, some are simple and some aren't. When you write, you choose what features to use.
Drain the swamp.

#### timb

• Super Contributor
• Posts: 2528
• Country:
• Pretentiously Posting Polysyllabic Prose
##### Arduino Schematic HELP!
« Reply #33 on: October 01, 2016, 08:16:22 pm »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++.
So what is the most similar programming language to Arduino?

It's not 'similar', the arduino programming language is C++, period.

When you program in C++ you also use libraries, standard C++ libraries,  your own libraries, and other people libraries. In the case of Arduino the language is C++ and it comes packaged not just with the C++ libraries but also with libraries that were written by the Arduino people.
Isn't C++ one of the hardest programming languages?

Not at all. See, you need to focus on learning the basic concepts of programming. Arduino can do that; there's plenty of tutorials both on the Arduino site and elsewhere. If you get stuck, remember, it's just C(++).

Once you know the fundamentals of programming, such as functions, loops, variables, registers, libraries, etc. than you can quickly pickup pretty much any language.

I got started programming in BASIC when I was about 10. I moved on to C when I was 12. This gave me a good set of fundamentals that have served me well the last 20 years.

Another thing, I think you're confused with this whole C/C++/C# thing:

First, there was C, which is a compiled procedural language. It's heavily used everywhere, both in the PC and embedded worlds.

Then there was C++, which is built on top of C. It adds object oriented functionality as well as other features to C. Most of the time you can mix C and C++ so long as you're using a CPP compiler.

C# is Microsoft's own unique take on C++. There is an embedded version of it, but I'd avoid it altogether.

Arduino is C++ plus a library that acts as a bit of an abstraction layer to make things easier. (For example, digitalWrite is just a function that's part of the Arduino library; it hides all the register manipulation you'd have to do otherwise.)

Finally, Arduino will teach you C/C++, but it won't teach you good habits, so keep that in mind.
« Last Edit: October 01, 2016, 08:19:42 pm by timb »
Any sufficiently advanced technology is indistinguishable from magic; e.g., Cheez Whiz, Hot Dogs and RF.

#### skillz21

• Frequent Contributor
• Posts: 266
• Country:
##### Re: Arduino Schematic HELP!
« Reply #34 on: October 01, 2016, 08:32:12 pm »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++.
So what is the most similar programming language to Arduino?

It's not 'similar', the arduino programming language is C++, period.

When you program in C++ you also use libraries, standard C++ libraries,  your own libraries, and other people libraries. In the case of Arduino the language is C++ and it comes packaged not just with the C++ libraries but also with libraries that were written by the Arduino people.
Isn't C++ one of the hardest programming languages?

Not at all. See, you need to focus on learning the basic concepts of programming. Arduino can do that; there's plenty of tutorials both on the Arduino site and elsewhere. If you get stuck, remember, it's just C(++).

Once you know the fundamentals of programming, such as functions, loops, variables, registers, libraries, etc. than you can quickly pickup pretty much any language.

I got started programming in BASIC when I was about 10. I moved on to C when I was 12. This gave me a good set of fundamentals that have served me well the last 20 years.

Another thing, I think you're confused with this whole C/C++/C# thing:

First, there was C, which is a compiled procedural language. It's heavily used everywhere, both in the PC and embedded worlds.

Then there was C++, which is built on top of C. It adds object oriented functionality as well as other features to C. Most of the time you can mix C and C++ so long as you're using a CPP compiler.

C# is Microsoft's own unique take on C++. There is an embedded version of it, but I'd avoid it altogether.

Arduino is C++ plus a library that acts as a bit of an abstraction layer to make things easier. (For example, digitalWrite is just a function that's part of the Arduino library; it hides all the register manipulation you'd have to do otherwise.)

Finally, Arduino will teach you C/C++, but it won't teach you good habits, so keep that in mind.
thanks, i get it now!
"It's only a stupid man, that believe stupid quotes they find on the internet" - Me, 2017

#### Ian.M

• Super Contributor
• Posts: 6897
##### Re: Arduino Schematic HELP!
« Reply #35 on: October 01, 2016, 08:48:13 pm »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++.
So what is the most similar programming language to Arduino?

c++
C++ is 99.9% backwards compatible with code written in ANSI C.  *MOST* 'sketches'  are very 'C'-like and the most blatant' C++'ism you will see is libraries for various types of hardware organised as classes  and their member functions.   C++ is quite big and complex to learn so start with learning C to the ANSI/ISO C99 standard which will get you reasonably competent at slinging code together as quickly as possible then learn the rest of C++ as you need it.  C and C++ compilers targeting all major PC OSes are readily available. e.g you can use an x86 targeted GCC (basically the same compiler the Arduino IDE invokes, but with different support files and options) or if you are on Windows, there's the alternative of Microsoft Visual C++.   If you are dabbling in PC programming for the first time, stick to writing console applications.  The complex mesh of events and callbacks needed to support any GUI application is a steep learning curve, no matter what OS you are on.

#### zapta

• Super Contributor
• Posts: 6004
• Country:
##### Re: Arduino Schematic HELP!
« Reply #36 on: October 01, 2016, 10:27:55 pm »

... Finally, Arduino will teach you C/C++, but it won't teach you good habits, so keep that in mind.

That's a very good observation.

Drain the swamp.

#### alsetalokin4017

• Super Contributor
• Posts: 1757
• Country:
##### Re: Arduino Schematic HELP!
« Reply #37 on: October 01, 2016, 11:43:16 pm »
would this kind of thing work? (it doesn't work, it says that A0 was not declared in this scope.)

const int Button = A0;
const int Light = 8;

unsigned long time;

int timeVal = 0;
int buttonstate = 0;
int timeoffVal = timeVal + 30000

void setup(){
pinMode(Light, OUTPUT);
pinMode(Button, INPUT);
}
void loop(){

if(buttonstate == HIGH){
timeVal = time;
digitalWrite(Light, HIGH);
}
else{
}

if(timeoffVal = time) {
timeoffVal = 0;
digitalWrite(Light, LOW);
}
}
You left off a semicolon up in the declarations. This resulted in a weird error on my Arduino IDE. I didn't get the "A0 not declared in this scope" error though. When I added the semicolon the error went away and the sketch compiled.

However... it still doesn't do what you want or need.

First, you initialize a variable "time" but you don't give it a value. This usually results in a value of 0 but you really ought to do it explicitly to be sure. Then, in loop{} , nothing happens to change this value.

Next, in the last if statement you have the wrong syntax. You are assigning (=) rather than comparing (==). This doesn't result in a compiler error because it's OK to do this if it's what you really need, but in this case it isn't, and it always makes the "if" statement enclosing it evaluate as "true".

Ok, so those are the easy errors. Now let's flow through the loop{} after corrections.

Now if the button is High, assign the value in "time" (0) to "timeVal". Now both "time" and "timeVal" contain the value 0.
Then turn ON the light.

Now test to see if "timeoffVal" is equal to "time", which of course is 0. But "timeoffVal" was declared as the initial value of "timeVal" (0) plus 30000, so the test fails and the next two statements aren't executed. So "timeoffVal" stays at 30000 and the light isn't turned off, so it stays ON.

Now go back to the top of the loop{} and read the button.
Now if the button is LOW, the first if statement fails so the next two statements aren't executed. "timeVal" stays at 0 and the light stays at whatever it was at before, which is ON.

Now test to see if "timeoffVal" is equal to "time", which of course is 0. But "timeoffVal" still has what it had the last time through which is 30000, so the test fails and the next two statements aren't executed. So "timeoffVal" stays at 30000 and the light isn't turned off.

And go to the top of the loop{} .....

So when you start the sketch, with the button "unpressed" (LOW), the light will be off. Then when you press the button the light will turn on and stay on, and remain on no matter what else happens with the button, until the sketch is restarted.

If you include
digitalWrite(Light, LOW);
inside that empty else statement at least the light will turn off when the button goes LOW, but I think you want the light to stay on for a while once it has been turned on, even if the button is "unpressed" after being pressed.

So I think what you need to do is to test to see if some length of time has elapsed since the light was turned on, and if it has, turn off the light.

« Last Edit: October 01, 2016, 11:54:14 pm by alsetalokin4017 »
The easiest person to fool is yourself. -- Richard Feynman

#### skillz21

• Frequent Contributor
• Posts: 266
• Country:
##### Re: Arduino Schematic HELP!
« Reply #38 on: October 02, 2016, 07:43:55 am »
Could i just do. When button pressed, light turns on, delay, light turns off?
"It's only a stupid man, that believe stupid quotes they find on the internet" - Me, 2017

#### Psi

• Super Contributor
• Posts: 6412
• Country:
##### Re: Arduino Schematic HELP!
« Reply #39 on: October 02, 2016, 10:16:00 am »
You can do anything you want.

Just store the Millis() value and then keep comparing this to the latest Millis() value.
When it has increased by 1000 then you know it's now 1 second later than before, and you can do some stuff etc..
« Last Edit: October 02, 2016, 10:18:23 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)

#### zapta

• Super Contributor
• Posts: 6004
• Country:
##### Re: Arduino Schematic HELP!
« Reply #40 on: October 02, 2016, 10:38:46 am »
You can do anything you want.

Just store the Millis() value and then keep comparing this to the latest Millis() value.
When it has increased by 1000 then you know it's now 1 second later than before, and you can do some stuff etc..
s/comparing this to/subtracting this from/
Drain the swamp.

#### Bukurat

• Regular Contributor
• Posts: 65
• Country:
##### Re: Arduino Schematic HELP!
« Reply #41 on: October 02, 2016, 10:44:38 am »
would this kind of thing work? (it doesn't work, it says that A0 was not declared in this scope.)

const int Button = A0;
const int Light = 8;

unsigned long time;

int timeVal = 0;
int buttonstate = 0;
int timeoffVal = timeVal + 30000

void setup(){
pinMode(Light, OUTPUT);
pinMode(Button, INPUT);
}
void loop(){

if(buttonstate == HIGH){
timeVal = time;
digitalWrite(Light, HIGH);
}
else{
}

if(timeoffVal = time) {
timeoffVal = 0;
digitalWrite(Light, LOW);
}
}

The compiler is telling you it doesn't know what A0 is.   Have you included the Arduino.h  header file?

#### zapta

• Super Contributor
• Posts: 6004
• Country:
##### Re: Arduino Schematic HELP!
« Reply #42 on: October 02, 2016, 12:28:45 pm »
The button is a digital in so you don't need the A notation. Just use pin number as you do for the light.
Drain the swamp.

#### alsetalokin4017

• Super Contributor
• Posts: 1757
• Country:
##### Re: Arduino Schematic HELP!
« Reply #43 on: October 02, 2016, 12:48:32 pm »
The compiler error is caused by the lack of a semicolon terminating the line

int timeoffVal = timeVal + 30000

in the sketch.

If the "A0 not declared in this scope" error persists after the semicolon is put on the end of that line, check to see if the IDE is installed properly with all files where they are supposed to be in their directories. This should be done automatically but sometimes it is possible to "orphan" a sketch so that the header files are not accessible as they normally should be.

Yes, it's kind of a waste to use an analog input pin for a (digital) button press. But it does work.

Could i just do. When button pressed, light turns on, delay, light turns off?

Yes, you can. Have you tried writing a sketch to do this? It's pretty simple and can be done in a number of ways.
I've got one working but it won't do you any good if I just give it to you. You should try to figure it out yourself, using the hints I and others have given you.

The easiest person to fool is yourself. -- Richard Feynman

#### skillz21

• Frequent Contributor
• Posts: 266
• Country:
##### Re: Arduino Schematic HELP!
« Reply #44 on: October 03, 2016, 08:28:56 pm »
ok, i'll try and do that.
"It's only a stupid man, that believe stupid quotes they find on the internet" - Me, 2017

#### skillz21

• Frequent Contributor
• Posts: 266
• Country:
##### Re: Arduino Schematic HELP!
« Reply #45 on: October 03, 2016, 09:00:38 pm »
YAY!  This works!

const int Light = 8;
const int Button = A0;

int buttonstate = 0;

void setup() {
pinMode(Light, OUTPUT);
pinMode(Button, INPUT);
}

void loop() {
digitalWrite(Light, HIGH);
delay(5000);
digitalWrite(Light, LOW);
}
}

With Help, I did it alone! (that doesn't even make sense, Thanks for all the help guys, i learnt lots ) You guys were right, it feels much better when you make the sketch alone!  However, the ping sensors should be here in a few weeks, i'll ask for help if i need it! Thanks for your help once more guys!
"It's only a stupid man, that believe stupid quotes they find on the internet" - Me, 2017

#### skillz21

• Frequent Contributor
• Posts: 266
• Country:
##### Re: Arduino Schematic HELP!
« Reply #46 on: October 03, 2016, 09:29:00 pm »
Ok, new question, i ordered in a arduino nano. i tried to find a low power mode library or something. sadly, i couldn't find anything. Do you guys know of any ways to get lower power consumption without removing the leds? or am i better off using my Uno?
"It's only a stupid man, that believe stupid quotes they find on the internet" - Me, 2017

• Super Contributor
• Posts: 4199
• Country:
• A qualified hobbyist ;)
##### Re: Arduino Schematic HELP!
« Reply #47 on: October 03, 2016, 09:56:48 pm »

#### alsetalokin4017

• Super Contributor
• Posts: 1757
• Country:
##### Re: Arduino Schematic HELP!
« Reply #48 on: October 04, 2016, 04:43:26 am »
YAY!  This works!

const int Light = 8;
const int Button = A0;

int buttonstate = 0;

void setup() {
pinMode(Light, OUTPUT);
pinMode(Button, INPUT);
}

void loop() {
digitalWrite(Light, HIGH);
delay(5000);
digitalWrite(Light, LOW);
}
}

With Help, I did it alone! (that doesn't even make sense, Thanks for all the help guys, i learnt lots ) You guys were right, it feels much better when you make the sketch alone!  However, the ping sensors should be here in a few weeks, i'll ask for help if i need it! Thanks for your help once more guys!

That's great, congratulations! I guess the "A0 not declared in this scope" compiler error went away once you got the syntax errors straightened out.

As you've realized by now there are usually many ways to achieve the same goal in programming. So here's the sketch I wrote yesterday, to illustrate two ways to do the same thing. One is essentially the same as what you wrote using delay(), and the other shows (one way) how to use millis() and a comparison to implement the On-time delay.

Code: [Select]
/*Get Out of Bed Light On with Off Delay  Two versions: using delay() or using millis() See comments and comment/uncomment lines as needed to change version */const int Button = 7;  // use digital IO pin for Buttonconst int Light = 13;  // use built-in LED for testing/debugging unsigned long time;    // this line can be commented out for delay() versionboolean buttonstate = false;long int timeOnVal = 5000; // On time in milliseconds is set herevoid setup(){  pinMode(Light, OUTPUT);  pinMode(Button, INPUT);  digitalWrite(Light, LOW);   // assures sketch starts with light Off}void loop(){  buttonstate = digitalRead(Button);  time = millis();            // comment out this line for delay() version    if(buttonstate == true) {    digitalWrite(Light, HIGH);    //    delay(timeOnVal);      // uncomment this line for delay() version    // comment out next line for delay() version    while(millis() < (time + timeOnVal)) { }   // stay here and check time until On time is exceeded  }  else digitalWrite(Light, LOW);  // turn light Off if On time is exceeded and buttonstate is LOW}
The easiest person to fool is yourself. -- Richard Feynman

#### IanB

• Super Contributor
• Posts: 9033
• Country:
##### Re: Arduino Schematic HELP!
« Reply #49 on: October 04, 2016, 05:24:40 am »
The following line would not work correctly if millis() wraps back round to zero:

Code: [Select]
    while(millis() < (time + timeOnVal)) { }   // stay here and check time until On time is exceeded
However, it will be safe if written this way instead:

Code: [Select]
    while( (millis() - time) < timeOnVal ) { }   // stay here and check time until On time is exceeded
This is because subtractions between two unsigned values will always produce the expected positive difference.
I'm not an EE--what am I doing here?

Smf