Pages: [1]   Go Down
Author Topic: lil help please  (Read 761 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);

}
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26482
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Count the opening { braces.
Count the closing } braces.
The two numbers should be the same.
Logged

"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.

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could you post all your code?
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

CTRL-T to get started.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50157
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while(x = false)
Why are you assigning false to x in the expression? == is the equality test operator, not =.

Code:
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
 
}

Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK I think I got it yay.. here's what I did.
Code:
...

And what happened when you ran it?

John
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"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

Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Virginia
Offline Offline
Newbie
*
Karma: 1
Posts: 8
powerfully insane
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: