Seattle, WA
Offline
Newbie
Karma: 0
Posts: 45
|
 |
« Reply #15 on: December 19, 2012, 04:23:31 pm » |
Seems having it outside an IF works just as well, as any state change gets you into the IFs to do stuff.
|
|
|
|
|
Logged
|
|
|
|
|
California
Online
Edison Member
Karma: 41
Posts: 1877
|
 |
« Reply #16 on: December 19, 2012, 04:34:09 pm » |
Any idea why I have to declare "int swState" twice? It won't compile when I omit it from the loop, but I thought the void setup was where you set those up.
Scope (Google Keyword). Declared variables inside curly braces are only valid within those, or any nested curly braces. If you want both setup and loop to use the same variable, it needs to be declared in the global scope (outside of both functions).
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA
Offline
Newbie
Karma: 0
Posts: 45
|
 |
« Reply #17 on: December 19, 2012, 04:36:49 pm » |
} <-- The close goes here
I see, I was misreading Nick's code. Apparently, when you have an IF followed by one line of code, you don't need brackets at all, hence me thinking the bracket past the state change write was the end of the IF, but it wasn't. Thanks!
|
|
|
|
« Last Edit: December 19, 2012, 05:55:56 pm by cosmos275 »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #18 on: December 19, 2012, 05:19:29 pm » |
It's a style thing. Some people always like to have the braces, personally I think it is clutter to do something like this: if (minutes > 59) { hours++; }
Instead of: if (minutes > 59) hours++;
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA
Offline
Newbie
Karma: 0
Posts: 45
|
 |
« Reply #19 on: December 19, 2012, 05:34:26 pm » |
Cool, thanks
Speaking of minutes > 59, do you know how to make a 24 hour clock easily? I'd like to have scheduled events happen throughout a 24 hour day maybe based on dip switch inputs for which hour for them to occur. Is there an easy way to make a reliable clock from millis() or something?
Thanks!
|
|
|
|
|
Logged
|
|
|
|
|
France
Online
God Member
Karma: 19
Posts: 624
Scientia potentia est.
|
 |
« Reply #20 on: December 19, 2012, 05:46:34 pm » |
It should be accurate enough uint32_t time_old, time_new;
void setup() { time_old = millis(); }
void loop() { time_new = millis();
if ( time_new - time_old >= ( 24UL * 60 * 60 * 1000 ) ) { time_old = time_new; //do something every 24 hours here } } Edit: after re reading your post I doubt that is useful for you...sorry I'm not english, sometimes I don't understand 
|
|
|
|
« Last Edit: December 19, 2012, 06:31:32 pm by guix »
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6396
-
|
 |
« Reply #21 on: December 19, 2012, 05:46:51 pm » |
It's a style thing. Some people always like to have the braces, personally I think it is clutter to do something like this:
I'm firmly in the 'always use braces' camp. It's so easy for a careless code change or macro substitution to add a second statement which appears to be included in the condition, but really isn't. And then when you have somebody used to coding like this (but not experienced enough to be aware of all the gotchas) you can have all sorts of bother with nested ifs and so on. As far as I'm concerned, the benefits of always using braces and indentation to make the control structure clear and verifiable outweigh all considerations about compactness. The best way I know to achieve for myself that is to put each brace on a separate line with matching pairs indented by the same amount and the lines between the indented an extra level, although others prefer different layouts and that's fine.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA
Offline
Newbie
Karma: 0
Posts: 45
|
 |
« Reply #22 on: December 19, 2012, 06:00:29 pm » |
uint32_t time_old, time_new;
if ( time_new - time_old >= ( 24 * 60 * 60 * 1000UL ) )
Couple questions: What is "uint32_t"? Also, what is the UL in 1000UL? Still new over here, thanks!
|
|
|
|
|
Logged
|
|
|
|
|
France
Online
God Member
Karma: 19
Posts: 624
Scientia potentia est.
|
 |
« Reply #23 on: December 19, 2012, 06:02:21 pm » |
"uint32_t" is the same as writing "unsigned long", and UL is to inform the compiler that the resulting number (of the multiplications) will be a unsigned long as well  Edit: If you do for example: uint32_t n1 = 24 * 60 * 60 * 1000; uint32_t n2 = 24UL * 60 * 60 * 1000;
Serial.println(n1); Serial.println(n2);
Here is the result: 23552 86400000
So it is important to put the 'UL' when needed 
|
|
|
|
« Last Edit: December 19, 2012, 06:31:17 pm by guix »
|
Logged
|
|
|
|
|
Seattle, WA
Offline
Newbie
Karma: 0
Posts: 45
|
 |
« Reply #24 on: December 19, 2012, 06:12:09 pm » |
uint32_t time_old, time_new;
Is that comma after time_old correct or is it supposed to be a ";" Thanks
|
|
|
|
|
Logged
|
|
|
|
|
France
Online
God Member
Karma: 19
Posts: 624
Scientia potentia est.
|
 |
« Reply #25 on: December 19, 2012, 06:20:13 pm » |
No it's correct, it's like writing uint32_t time_old, time_new;
or uint32_t time_old; uint32_t time_new;
All the same  PS: I fixed a little mistake in my previous post: do not place UL after the last number. To any other numbers but the last, else it will give a bad resulting number!
|
|
|
|
« Last Edit: December 19, 2012, 06:47:03 pm by guix »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35535
Seattle, WA USA
|
 |
« Reply #26 on: December 20, 2012, 05:28:35 am » |
"uint32_t" is the same as writing "unsigned long" Not quite. The uint32_t type explicitly creates a 32 bit variable on any platform - Windows, Max, Linux, 64 bit, 32 bit, 16 bit, 8 bit, whatever. It doesn't matter. The same hold true for the uint16_t, int32_t, int8_t, etc. types. The number defines the number of bits. The prefix defines the type - int, unsigned int. An unsigned long or int or byte can be different sized on different platforms.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35535
Seattle, WA USA
|
 |
« Reply #27 on: December 20, 2012, 05:33:44 am » |
PS: I fixed a little mistake in my previous post: do not place UL after the last number. To any other numbers but the last, else it will give a bad resulting number! That's not true, either. The UL has to close enough to the front so overflow doesn't occur before the type change. uint32_t myVar1 = 80 * 1000 * 1000UL; is wrong, because 80 * 1000 will be performed using ints, and will overflow. uint32_t myVar2 = 80UL * 1000UL * 1000UL; is OK, because all the values are unsigned long, so unsigned long arithmetic will be used. uint32_t myVar3 = 10 * 1000 * 1000UL; is also OK, because 10 * 1000 will be performed using ints, but no overflow will occur.
|
|
|
|
|
Logged
|
|
|
|
|
|