Go Down

### Topic: lil help please (Read 1 time)previous topic - next topic

#### perkunas

##### Jan 01, 2013, 06:27 pm
If trying to write a little code,  lets call them Leds, actually they are relays. They all start same time
run for a bit but end at different times.
I think I can do it in a while loop, this gives me a Void loop': and a expected } error

Code: [Select]
`void loop (){boolean x = false;while(x = false){  unsigned long Task1Time = millis();  if (Task1Time >= 0){   digitalWrite(5, HIGH);   }   if (Task1Time >= minutes_in_ms(5)){   digitalWrite(5, LOW); }}`

#### AWOL

#1
##### Jan 01, 2013, 06:29 pm
Count the opening { braces.
Count the closing } braces.
The two numbers should be the same.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### ash901226

#2
##### Jan 01, 2013, 06:30 pm
could you post all your code?

#### marcello.romani

#3
##### Jan 01, 2013, 06:32 pm
CTRL-T to get started.

#### PaulS

#4
##### Jan 01, 2013, 06:33 pm
Code: [Select]
`while(x = false)`
Why are you assigning false to x in the expression? == is the equality test operator, not =.

Code: [Select]
` unsigned long Task1Time = millis();  if (Task1Time >= 0){`
By the time this code gets executed, millis() will return a non-negative, non-zero value. Therefore this will ALWAYS be true. Seems silly to test an expression that can not possibly be false.

Running an infinite loop inside an infinite loop is pointless.

You do not have a matching number of } for the number of { you have.

#### perkunas

#5
##### Jan 01, 2013, 07:19 pm
your right "By the time this code gets executed, millis() will return a non-negative, non-zero value. Therefore this will ALWAYS be true. Seems silly to test an expression that can not possibly be false."
I want it to always be true, as I want them all to start the same time, real dumb I should of said just start no expression.
Loop within a loop is stupid, maybe, but what if you want to run some more code after this loop is finished, its going to fire 4 relays then do other stuff after they all finish.
The while(x = false) was my way out of the loop after my 4 relays were finished.
Why are you assigning false to x in the expression? == is the equality test operator, not =
Not sure what you mean, I guess it should be while(x) or while(x==true) some thing like that
got to go to work now I will think about this some more try again later thanks

#### perkunas

#6
##### Jan 01, 2013, 10:20 pm
OK I think I got it yay.. here's what I did.
pin 5,6,7 come on same time
5 runs 3min.
6 runs 7min.
7 runs 9 min.
then it breaks out of the loop and
turns pin 4 on and the whole loops and starts over.
This is not exactly what I want, but it is the Idea for what I'm trying to do.
Hope its right

Code: [Select]
`#define seconds_in_ms(s) ((s)*1000UL)#define minutes_in_ms(m) ((m)*60UL*1000UL)#define hours_in_ms(h)   ((h)*60UL*60UL*1000UL)#define days_in_ms(d)    ((d)*24UL*60UL*60UL*1000UL)#define weeks_in_ms(w)   ((w)*7UL*24UL*60UL*60UL*1000UL)boolean x = true;void setup() {          pinMode(2, OUTPUT);      pinMode(3, OUTPUT);     pinMode(4, OUTPUT);     pinMode(5, OUTPUT);    pinMode(6, OUTPUT);      pinMode(7, OUTPUT);    digitalWrite(2, LOW);     digitalWrite(3, LOW);    digitalWrite(4, LOW);    digitalWrite(5, LOW);    digitalWrite(6, LOW);    digitalWrite(7, LOW);    }void loop (){  digitalWrite(5, HIGH);  digitalWrite(6, HIGH);  digitalWrite(7, HIGH);  unsigned long Task1Time = millis();  while(x){   if (Task1Time >= minutes_in_ms(3)){   digitalWrite(5, LOW);     }  if (Task1Time >= minutes_in_ms(7)){     digitalWrite(6, LOW); }  if (Task1Time >= minutes_in_ms(9)){     digitalWrite(7, LOW);      x = false; }}  digitalWrite(4, HIGH); //not really but a whole bunch other code goes here before it all loops }`

#### marcello.romani

#7
##### Jan 01, 2013, 10:44 pm
Looks to me your code will work just once after a powerup or a board reset. Is this what you want ?
Otherwise look closer to the blink without delay example.

#### johncc

#8
##### Jan 01, 2013, 10:52 pm

OK I think I got it yay.. here's what I did.
Code: [Select]
`...`

And what happened when you ran it?

John

#### perkunas

#9
##### Jan 01, 2013, 11:57 pm
"Looks to me your code will work just once after a powerup or a board reset. Is this what you want ?"
Actually yes and no, nice catch. (this is just a test more to come)
Its going to do a bunch of stuff 14 days, then start over at the end of the main loop (perfect) or start over on a power up or board reset...so yes
but no...Id prefer it to pickup where it left off, on lets say a power outage. Code wise that's too hard to do, so battery backup is prob the answer.
unless you got other ideas.

Haven't tried it yet I will tomorrow

#### marcello.romani

#10
##### Jan 02, 2013, 12:35 am
I think you're taking the wrong approach. If you need to plan actions 14 days in the future, then repeat them, I think you'd be better off with an RTC module (e.g. DS1307) and the Time library.

#### rujoking

#11
##### Jan 02, 2013, 12:53 am

Why are you assigning false to x in the expression? == is the equality test operator, not =
Not sure what you mean, I guess it should be while(x) or while(x==true) some thing like that

Actually, (x == false) would work just fine too.  Every time you run (x = false), you're setting x equal to false, not checking to see whether or not it is, in fact, false.  So you're basically saying "Just keep doing this all the time because it's always going to be time to do it."  Using (x == false) actually checks the truthity or falsitude of x, then does stuff between the braces only when the falsitude is confirmed.

Go Up