Arduino Schematic HELP! - Page 1
Forum section: electronics / beginners

author: skillz21
author_rank: Frequent Contributor
timestamp: September 30, 2016, 10:37:38 am
content: Hey, guys!I am trying to build a... thing () that tracks motion and turns on some led lights when I step off my bed. I will be using two HC-SR04 ping sensors to track both sides of my bed. a problem that I am currently having is, I need to make a variable continually count down every second, and to stop when it reaches around -10. I want the LEDs to light up when the variable is above 0. how do I make the variable stop once it reaches -10? If you need more info, please askThanksBTW: I have had some issues on the forum when I wanted to get straight answers and people were trying to teach me C#. Guys, I'm doing that at the moment. I'm still only 12.

author: skillz21
author_rank: Frequent Contributor
timestamp: September 30, 2016, 10:47:08 am
content: Also, how do I make the value count down 1 every second?

author: JPortici
author_rank: Super Contributor
timestamp: September 30, 2016, 10:55:29 am
content: Quote from: skillz21 on September 30, 2016, 10:37:38 amwhat else should have they done?the logic is pretty straightforward- you have a variable that should be updated every second-ifthe variable value is greater than zerothenswitch on the leds- otherwise (else),ifthe variable value is lower than tenthenswitch off the leds and stop countingit's pretty basic [insert language name], nobody will seriously bother telling you because if you can't do/learn that on your own it probably means that you don't care (worst scenario) or you can't undestand what's going on anyway (best scenario)note that the worst case was put first, for a very good reason. in case you genuinely don't know the language you must learn it before so you can understand what you are being answeredthen you need to generate an event every second or to wait idling for a second, there are lots of ways to do that. for the second one you usually have ready made functions

author: madires
author_rank: Super Contributor
timestamp: September 30, 2016, 10:59:33 am
content: For the 1s timer:http://playground.arduino.cc/Code/Timer1

author: skillz21
author_rank: Frequent Contributor
timestamp: September 30, 2016, 11:02:34 am
content: wait, are you saying that I don't care about learning c#? I do, very much. please don't get the wrong impression just because I'm a kid

author: skillz21
author_rank: Frequent Contributor
timestamp: September 30, 2016, 11:05:13 am
content: Quote from: madires on September 30, 2016, 10:59:33 amIs it possible to do it just with a variable and not with a timer and a dedicted library.

author: skillz21
author_rank: Frequent Contributor
timestamp: September 30, 2016, 11:16:44 am
content: Here is the sketch that I made. please correct me in any place that I am wrong. also, the time reset is now a pushbutton, setting the value to 5. This sketch does not work.int timeVal = 0;void setup() {pinMode(8, OUTPUT);pinMode(A0, INPUT);}void loop() {timeval - 1if(timeVal > 0){digitalWrite(8, HIGH);}else{digitalWrite(8, LOW);}if(A0, HIGH){timeVal = 5;}}Also, I have gained most of my knowledge of C# from Scratch. I am trying to use the same... components of the programming, if that makes any sense.

author: madires
author_rank: Super Contributor
timestamp: September 30, 2016, 11:29:19 am
content: Quote from: skillz21 on September 30, 2016, 11:05:13 amUsing a timer for such things is a standard solution. Either you use an interrupt (with an interrupt handler) or have to check for the timer flag regularly. The library is convenient, but you can do it also directly. I'm sure you'll find a lot of examples when searching the internet.

author: skillz21
author_rank: Frequent Contributor
timestamp: September 30, 2016, 11:34:52 am
content: Quote from: madires on September 30, 2016, 11:29:19 amok

author: Ammar
author_rank: Regular Contributor
timestamp: September 30, 2016, 11:58:03 am
content: Happy to give you some hints.You can use the millis() function to find the time in ms since power on in milliseconds. Something like:timeNew = millis();See if you can use your loop and some if statements with the millis() function to solve your problem.You could use a while loop with the condition you outlined to make the variable stop when you want. I hope that helps and good luck!

author: Psi
author_rank: Super Contributor
timestamp: September 30, 2016, 12:03:48 pm
content: I dunno if this will compile, but ive tried to do what you want using the millis function.int TimeVal = 0;unsigned long PreviousMS = 0;unsigned long CurrentMS = 0;void setup() {pinMode(8, OUTPUT);pinMode(A0, INPUT);}void loop(){// loop runs VERY fast, we need to run some code slower to do the subtraction at 1 second.// Arduino has a millis() function to read the number off milliseconds// since the arduino started running.// We can store this number and then wait for the current time to equal the prev time plus 1000ms// then update the previous time to current time and let it repeat forever. This lets us run some code once a second.// get current timeCurrentMS = millis();// if more than 1000 ms have past since previous time, do stuff.if ( CurrentMS > (PreviousMS + 1000)){// code inside here happens once a second (1000ms)// only count down if value is larger than -10, once it reaches -10 it stops count and stays at -10if (TimeVal > -10){TimeVal = TimeVal - 1;}// last thing we do is update our previous time variable to the current time// so the process can repeat again in another 1000ms.PreviousMS = CurrentMS;}// this is outside of our 1 second if statement, so it repeats very fast.// which is ok for setting leds on or checking inputs.if (TimeVal > 0){digitalWrite(8, HIGH);} else {digitalWrite(8, LOW);}if (digitalRead(A0) == HIGH){TimeVal = 5;}}

author: madires
author_rank: Super Contributor
timestamp: September 30, 2016, 12:12:14 pm
content: If you're using millis() you should readhttps://www.arduino.cc/en/Reference/Millis, especially:QuoteSo you need to add an overflow detection.

author: Ian.M
author_rank: Super Contributor
timestamp: September 30, 2016, 01:16:06 pm
content: Why would you want to learnC#?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++.Seehttp://hackaday.com/2015/07/28/embed-with-elliot-there-is-no-arduino-language/for the details.

author: zapta
author_rank: Super Contributor
timestamp: September 30, 2016, 01:32:33 pm
content: Quote from: Ian.M on September 30, 2016, 01:16:06 pmThe Arduino programming language *is* C++.They just don't tell you that. It's a conspiracy to trick the population into learning embedded C++ programming.

author: stj
author_rank: Super Contributor
timestamp: September 30, 2016, 02:11:02 pm
content: Quote from: skillz21 on September 30, 2016, 10:37:38 ama pressure-mat or a pir sensor would do that!

author: alsetalokin4017
author_rank: Super Contributor
timestamp: September 30, 2016, 04:23:36 pm
content: Quote from: skillz21 on September 30, 2016, 11:16:44 amYour code doesn't compile because of the errors in the lines I have bolded up above.1. You have not initialized the variable "timeval".... you obviously mean to use "timeVal" but the compiler can't read your mind like I can. C++ variable names are case sensitive.2. You did not terminate that line with a semicolon.3. You have to explicitly read the A0 pin and use the conditional test == to compare to a value.4. You need to tell it what to do if the conditional test fails (if A0 is LOW): include an else statement to reset timeVal.So if you replace your linetimeval -1with the linetimeVal -1;ortimeVal --;and the lineif(A0, HIGH){with the lineif(digitalRead(A0)==HIGH){and add the else statementelse timeVal=0;after the final "if" statementyour sketch should compile and run.But will it do what you intend? I think you will need to insert some delay() or test for some millis() values as Psi suggests. (His suggested pseudocode makes the same error about the capital V in timeVal though.)If you don't intend to stay in bed for more than 50 days, I wouldn't worry about overflowing the millis() timer.

author: skillz21
author_rank: Frequent Contributor
timestamp: October 01, 2016, 12:37:43 am
content: Quote from: Ian.M on September 30, 2016, 01:16:06 pmSo what is the most similar programming language to Arduino?

author: skillz21
author_rank: Frequent Contributor
timestamp: October 01, 2016, 12:40:10 am
content: Quote from: Psi on September 30, 2016, 12:03:48 pmIs it possible to make different loops? with different speeds?

author: skillz21
author_rank: Frequent Contributor
timestamp: October 01, 2016, 12:43:37 am
content: Quote from: stj on September 30, 2016, 02:11:02 pmThats true, but wouldn't that be more expensive?

author: skillz21
author_rank: Frequent Contributor
timestamp: October 01, 2016, 12:50:32 am
content: Quote from: madires on September 30, 2016, 12:12:14 pmwait... so can I change the value of millis?

author: stj
author_rank: Super Contributor
timestamp: October 01, 2016, 01:45:35 am
content: Quote from: skillz21 on October 01, 2016, 12:43:37 ampir sensors cost a couple of $ on ebay.http://www.ebay.co.uk/itm/321614189142

author: skillz21
author_rank: Frequent Contributor
timestamp: October 01, 2016, 01:53:59 am
content: 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() {buttonstate = digitalRead(Button);if(timeVal > 0){digitalWrite(Light, HIGH);}else{digitalWrite(Light, LOW);}if(buttonstate == HIGH){timeVal = 5;}}

author: skillz21
author_rank: Frequent Contributor
timestamp: October 01, 2016, 01:58:44 am
content: Quote from: stj on October 01, 2016, 01:45:35 am

author: alsetalokin4017
author_rank: Super Contributor
timestamp: October 01, 2016, 02:17:40 am
content: Quote from: skillz21 on October 01, 2016, 12:37:43 amc++

author: alsetalokin4017
author_rank: Super Contributor
timestamp: October 01, 2016, 02:19:16 am
content: Quote from: skillz21 on October 01, 2016, 12:40:10 amYes, using interrupts, but we aren't quite ready for that yet.

author: alsetalokin4017
author_rank: Super Contributor
timestamp: October 01, 2016, 02:50:27 am
content: Quote from: skillz21 on October 01, 2016, 01:53:59 amOK... 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: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.

author: zapta
author_rank: Super Contributor
timestamp: October 01, 2016, 02:51:22 am
content: Quote from: skillz21 on October 01, 2016, 12:37:43 amIt'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.

author: exmadscientist
author_rank: Frequent Contributor
timestamp: October 01, 2016, 03:46:52 am
content: Quote from: madires on September 30, 2016, 12:12:14 pmOn 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: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 overflowwhile (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 Arduinohardwareschematics. 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.)

author: skillz21
author_rank: Frequent Contributor
timestamp: October 01, 2016, 07:48:56 am
content: Quote from: zapta on October 01, 2016, 02:51:22 amIsn't C++ one of the hardest programming languages?

author: skillz21
author_rank: Frequent Contributor
timestamp: October 01, 2016, 08:08:59 am
content: 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?

author: IanB
author_rank: Super Contributor
timestamp: October 01, 2016, 08:18:24 am
content: Quote from: madires on September 30, 2016, 12:12:14 pmQuote from: exmadscientist on October 01, 2016, 03:46:52 amThere 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

author: skillz21
author_rank: Frequent Contributor
timestamp: October 01, 2016, 09:08:01 am
content: 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 + 30000void setup(){pinMode(Light, OUTPUT);pinMode(Button, INPUT);}void loop(){buttonstate = digitalRead(Button);if(buttonstate == HIGH){timeVal = time;digitalWrite(Light, HIGH);}else{}if(timeoffVal = time) {timeoffVal = 0;digitalWrite(Light, LOW);}}

author: zapta
author_rank: Super Contributor
timestamp: October 01, 2016, 09:51:58 am
content: Quote from: skillz21 on October 01, 2016, 07:48:56 amIt has many features, some are simple and some aren't. When you write, you choose what features to use.

author: timb
author_rank: Super Contributor
timestamp: October 01, 2016, 10:16:22 am
content: Quote from: skillz21 on October 01, 2016, 07:48:56 amNot 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.

author: skillz21
author_rank: Frequent Contributor
timestamp: October 01, 2016, 10:32:12 am
content: Quote from: timb on October 01, 2016, 10:16:22 amthanks, i get it now!

author: Ian.M
author_rank: Super Contributor
timestamp: October 01, 2016, 10:48:13 am
content: Quote from: alsetalokin4017 on October 01, 2016, 02:17:40 amC++ 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 asclassesand 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.

author: zapta
author_rank: Super Contributor
timestamp: October 01, 2016, 12:27:55 pm
content: Quote from: timb on October 01, 2016, 10:16:22 amThat's a very good observation.

author: alsetalokin4017
author_rank: Super Contributor
timestamp: October 01, 2016, 01:43:16 pm
content: Quote from: skillz21 on October 01, 2016, 09:08:01 amYou 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.First, read the button.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 includedigitalWrite(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.

author: skillz21
author_rank: Frequent Contributor
timestamp: October 01, 2016, 08:43:55 pm
content: Could i just do. When button pressed, light turns on, delay, light turns off?

author: Psi
author_rank: Super Contributor
timestamp: October 01, 2016, 11:16:00 pm
content: 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..

author: zapta
author_rank: Super Contributor
timestamp: October 01, 2016, 11:38:46 pm
content: Quote from: Psi on October 01, 2016, 11:16:00 pms/comparing this to/subtracting this from/

author: Bukurat
author_rank: Regular Contributor
timestamp: October 01, 2016, 11:44:38 pm
content: Quote from: skillz21 on October 01, 2016, 09:08:01 amThe compiler is telling you it doesn't know what A0 is. Have you included the Arduino.h header file?

author: zapta
author_rank: Super Contributor
timestamp: October 02, 2016, 01:28:45 am
content: The button is a digital in so you don't need the A notation. Just use pin number as you do for the light.

author: alsetalokin4017
author_rank: Super Contributor
timestamp: October 02, 2016, 01:48:32 am
content: The compiler error is caused by the lack of a semicolon terminating the lineint timeoffVal = timeVal + 30000in 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.Quote from: skillz21 on October 01, 2016, 08:43:55 pmYes, 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.

author: skillz21
author_rank: Frequent Contributor
timestamp: October 03, 2016, 09:28:56 am
content: ok, i'll try and do that.

author: skillz21
author_rank: Frequent Contributor
timestamp: October 03, 2016, 10:00:38 am
content: YAY!This works!const int Light = 8;const int Button = A0;int buttonstate = 0;void setup() {pinMode(Light, OUTPUT);pinMode(Button, INPUT);}void loop() {if(digitalRead(Button) == HIGH){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!

author: skillz21
author_rank: Frequent Contributor
timestamp: October 03, 2016, 10:29:00 am
content: 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?

author: madires
author_rank: Super Contributor
timestamp: October 03, 2016, 10:56:48 am
content: You might want to look into sleep modes:-http://playground.arduino.cc/Learning/ArduinoSleepCode-http://www.engblaze.com/hush-little-microprocessor-avr-and-arduino-sleep-mode-basics/

author: alsetalokin4017
author_rank: Super Contributor
timestamp: October 03, 2016, 05:43:26 pm
content: Quote from: skillz21 on October 03, 2016, 10:00:38 amThat'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:/*Get Out of Bed Light On with Off DelayTwo 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/debuggingunsigned 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() versionif(buttonstate == true) {digitalWrite(Light, HIGH);// delay(timeOnVal); // uncomment this line for delay() version// comment out next line for delay() versionwhile(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}

author: IanB
author_rank: Super Contributor
timestamp: October 03, 2016, 06:24:40 pm
content: The following line would not work correctly if millis() wraps back round to zero:Code:while(millis() < (time + timeOnVal)) { } // stay here and check time until On time is exceededHowever, it will be safe if written this way instead:Code:while( (millis() - time) < timeOnVal ) { } // stay here and check time until On time is exceededThis is because subtractions between two unsigned values will always produce the expected positive difference.

author: retrolefty
author_rank: Super Contributor
timestamp: October 03, 2016, 08:35:20 pm
content: Quote from: skillz21 on October 03, 2016, 10:29:00 amWell the power on led is not under any kind of software control so sleeping won't do anything for it.

author: skillz21
author_rank: Frequent Contributor
timestamp: October 08, 2016, 04:59:49 am
content: sleep mode will at least save a bit of power.

author: sleemanj
author_rank: Super Contributor
timestamp: October 08, 2016, 05:19:01 am
content: Kill the leds. If you are not using the onboard regulator cut it's legs (the cheap AMS1117 regs from china have a high quiescent even if you're powering after the reg). If you don't need the ADC, shut it down.

author: skillz21
author_rank: Frequent Contributor
timestamp: October 08, 2016, 11:22:00 am
content: ok, i might do that. i havent actually gotten the arduino yet, so i'll test it out when i get it.

author: Ian.M
author_rank: Super Contributor
timestamp: October 08, 2016, 06:27:37 pm
content: Rather than butchering Arduinos, why not start from an ATmega328P chip, flash it with an Arduino bootloader then program it via a USB <=> logic level serial cable?That way you don't have to hack off LEDs or regulators and you can easily isolate the MCU to check its power consumption.There's even apage about doing this at Arduino.cc

author: skillz21
author_rank: Frequent Contributor
timestamp: October 08, 2016, 11:27:00 pm
content: Quote from: Ian.M on October 08, 2016, 06:27:37 pmThats actually a good idea