simple question

Simple question. I have this piece of code, the reason it won't work is because the millis reset too soon, as i understood, and i need a flag in the terms of that "if". Now, from what i've read on another topic on this forum, i need to set that flag so that it tests a condition, that would stop the millis from counting or something like that. How do i do that? i can't find anything helpfull.

long previousMillis=0;
int gravity =1000;
int dif = 3000;
void setup (){
  DDRD=B11111111;
}

 void loop(){
unsigned long currentMillis = millis();

PORTD=B00011000;
if(currentMillis-previousMillis >=gravity); {PORTD=B00010000; 
previousMillis=currentMillis ;}
if(currentMillis-previousMillis >=dif);{PORTD=B01100000;
previousMillis=currentMillis ;}
if(currentMillis-previousMillis >=gravity);{PORTD=B01000000;
previousMillis=currentMillis ;}
if(currentMillis-previousMillis >=dif);{PORTD=B10000100;
previousMillis=currentMillis ;}
if(currentMillis-previousMillis >=gravity);{PORTD=B00000100;
previousMillis=currentMillis ;}
if(currentMillis-previousMillis >=dif);{ PORTD=B00011000;
previousMillis=currentMillis ;}
}

Google, "Finite State Machine". You need a variable to keep track of what step you're on. Just a regular old int will do. Something like this:

int step = 0;


void loop(){

   if(step == 0){
       if(current - previous >= interval){
           // do whatever
           previous = current;
           step = 1;
       }
   }
   else if(step == 1){

and so on and so forth.

simple fix:

You're initializing currentMillis to millis(), but not updating it on every loop.

unsigned long currentMillis; currentMillis = millis();

JustGoFly: simple fix:

You're initializing currentMillis to millis(), but not updating it on every loop.

unsigned long currentMillis; currentMillis = millis();

Um, yes, he is getting a new currentMillis on every single loop. It's the very first line of loop.

Shanon, i have tried your idea. Still doesn't work,though. I deleted it after that, to try JustGoFly's idea, even though even i figured out it won't work. the " currentMillis=millis(); " idea just resets the millis, the same problem as with previous=current.

Who is Shannon?

Oh, I'm a Shannon member, no My name is Delta_G on this board. I don't put my real name on the internet.

If you tried it and it didn't work, maybe you didn't do it quite right. Post what you tried and lets see if we can find the problem.

This is what i have tried now. The version i did before was similar, it was exactly what you told me to do. if->else if->else if->else if->etc

long previousMillis=0;
int gravity =1000;
int dif = 3000;
int cur=0;
void setup (){
  DDRD=B11111111;
}

 void loop(){
unsigned long currentMillis = millis();
if(cur==1){PORTD=B00011000; reviousMillis=currentMillis ; cur=0}
if(cur==0){
if(currentMillis-previousMillis >=gravity); {PORTD=B00010000; 
previousMillis=currentMillis ; cur=1; }}
if(cur==1){
if(currentMillis-previousMillis >=dif);{PORTD=B01100000;
previousMillis=currentMillis ; cur=0;}}
if(cur=1){
if(currentMillis-previousMillis >=gravity);{PORTD=B01000000;
previousMillis=currentMillis cur=0 ;}}
if(cur=0){
if(currentMillis-previousMillis >=dif);{PORTD=B10000100;
previousMillis=currentMillis ; cur=1;}}
if(cur=1){
if(currentMillis-previousMillis >=gravity);{PORTD=B00000100;
previousMillis=currentMillis ; cur=0; }}
if(cur=0){
if(currentMillis-previousMillis >=dif);{ PORTD=B00011000;
previousMillis=currentMillis ; cur=1;}}
}

Oh, I see. You misunderstood. I meant for you to carry that example all the way out. You just used step 0 and 1 over and over again. Looks like you'll probably have 5 or 6 steps.

Plus you have these errors all over it:

if (cur = 1)

= vs. == issue.

Please take a minute to think about what I wrote for you and what it was doing. I didn't give you code that you could copy and expect to have it work. I gave you an example of having an extra variable to keep track of which step you're on. I am leaving it to you to actually write that in a way that makes your particular code work.

Like, see how the different sections of loop are active or not based on that variable. This is going to allow you to have loop doing all sorts of different things at different time.

ok, first of all, that FSM machine is way above my level. second of all, i understand my code (trough your step1 and step2) and i don't get it why it doesn't work. Stage one, check if cur is 0 or not. before finishing stage one, make cur 1 for stage 2. And so on. This way the lines have to be executed one by one, they depend on each other. why is this happening?

You mean aside from the obvious coding errors? = vs == and the misspelled variable names? It won't even compile as written in #6 so I know you didn't test that code.

TraianGrg: ok, first of all, that FSM machine is way above my level

Lemme guess, you looked at the wikipedia page for it. It's not as complicated as all that math. It's really just step 1, step 2, step 3 etc.

TraianGrg: stage 2.

Nope, in the code you posted you only have 0 and 1. You don't have any stage 2 in that code. You just use 0 and 1 over and over.

isn’t 0 staeg1 and 1 stage 2? the typo’s were because by the time you asked me for the code i already changed it a few times, so i just kept CTRL+Z pressed until it was kinda what you said. I did compile it, it didn’t work. I am going to try it out again, like this (0,1,2,3,4,5,6 etc etc). I’m thinking maybe when it goes to cur=1 all if’s with cur = 1 run and that’s not good. I’ll write it up, test it and keep you updated. hang on.

TraianGrg: I'm thinking maybe when it goes to cur=1 all if's with cur = 1 run

Yes, if cur == 1 then all if's that say cur == 1 will obviously run.

However, all those with cur = 1 will run no matter what cur equals and cur will equal 1 afterwards no matter what it was before.

If all of those with cur=1 will run no matter what cur equals, why use it at all? This way, only one LED lights up.

long previousMillis=0;
int gravity =1000;
int dif = 3000;
int cur=1;
void setup (){
  DDRD=B11111111;
}

 void loop(){
unsigned long currentMillis = millis();
if(cur==1){PORTD=B00011000; previousMillis=currentMillis ; cur=2;}
else if(cur==2){
if(currentMillis-previousMillis >=gravity); {PORTD=B00010000; 
previousMillis=currentMillis ; cur=3; }}
else if(cur==3){
if(currentMillis-previousMillis >=dif);{PORTD=B01100000;
previousMillis=currentMillis ; cur=4;}}
else if(cur=4){
if(currentMillis-previousMillis >=gravity);{PORTD=B01000000;
previousMillis=currentMillis; cur=5 ;}}
else if(cur=5){
if(currentMillis-previousMillis >=dif);{PORTD=B10000100;
previousMillis=currentMillis ; cur=6;}}
else if(cur=6){
if(currentMillis-previousMillis >=gravity);{PORTD=B00000100;
previousMillis=currentMillis ; cur=7; }}
else if(cur=7){
if(currentMillis-previousMillis >=dif);{ PORTD=B00011000;
previousMillis=currentMillis ; cur=1;}}

}

TraianGrg: If all of those with cur=1 will run no matter what cur equals, why use it at all?

Exactly. It's pretty stupid to assign a value to a variable in the condition of an if statement. It would make a lot more sense to test whether or not cur was equal to 1 instead of setting it equal to 1 there.

What boggles my mind is that you do it right sometimes:

else if(cur==3){

But not others:

else if(cur=4){

The point that @Delta_G is trying to get across is:

'=' is an assignment operator. '==' is a test for equality. Each has its own purpose and must be used in the appropriate context.

Syntax matters. In fact, it's critical.

so tehnically, i am just using that cur for testing. It would work even if i wouldn't change it at all, just something like : if cur is 0 do this, else if cur is 1, do the same thing ? I have tried it another way now, and once again, all the LED's are on. Apparently i do s*ck at this.

long previousMillis=0;
int gravity =1000;
int dif = 3000;
int cur=1;
void setup (){
  DDRD=B11111111;
}

 void loop(){
unsigned long currentMillis = millis();
if(cur==1){PORTD=B00011000; previousMillis=currentMillis ; cur=2;}
else (cur==1);
if(cur==2){
if(currentMillis-previousMillis >=gravity); {PORTD=B00010000; 
previousMillis=currentMillis ; cur=3; }}
else (cur==3);
if(cur==3){
if(currentMillis-previousMillis >=dif);{PORTD=B01100000;
previousMillis=currentMillis ; cur=4;}}
else (cur=3);
if(cur==4){
if(currentMillis-previousMillis >=gravity);{PORTD=B01000000;
previousMillis=currentMillis; cur=5 ;}}
else (cur=4);
if(cur==5){
if(currentMillis-previousMillis >=dif);{PORTD=B10000100;
previousMillis=currentMillis ; cur=6;}}
else(cur=4);
if(cur==6){
if(currentMillis-previousMillis >=gravity);{PORTD=B00000100;
previousMillis=currentMillis ; cur=7; }}
else (cur=6);
if(cur==7){
if(currentMillis-previousMillis >=dif);{ PORTD=B00011000;
previousMillis=currentMillis ; cur=1;}}
else(cur=7);
}
else (cur==1);

Now you've gone off the deep. Else doesn't get a condition. Else is just else. You had this right before? Why did you suddenly decide to put conditions on the else statements?

You should also consider formatting your code. Hit Control - T and it will line up the blocks for you and help you to see the flow control in your code. Nothing helps spot bugs better.

else was supposed to have {} instead of () ; tried it, failed. Now i have 6 LEDs, 3 on, 3 dimmed :o I think i am running out of options here. why can't i see what i can't see? :stuck_out_tongue_closed_eyes: