code repeat

Thread Starter

gatoulisss

Joined Jan 23, 2015
69
hello guys!
is there any way of repeat this code line without writing them?

delay_ms(5000);
if (porta.f0==0) {goto start;}

because if i write it the programm crashes i dont know why... maybe the delay times are too big
 

spinnaker

Joined Oct 29, 2009
7,830
What do you mean "repeat"?

Have you heard of the for loop?

Code:
for (i=0; i<5; i++)
{
   delay_ms(5000);
   if (porta.f0==0) {goto start;}
}

And you should avoid using goto. It makes for messy code. Use should be very rare. Using it a lot is as sign of newbie code.
 

Thread Starter

gatoulisss

Joined Jan 23, 2015
69
What do you mean "repeat"?

Have you heard of the for loop?

Code:
for (i=0; i<5; i++)
{
   delay_ms(5000);
   if (porta.f0==0) {goto start;}
}

And you should avoid using goto. It makes for messy code. Use should be very rare. Using it a lot is as sign of newbie code.

i use loops but after 5 times of writting this then the programm excecutes ( i dont know why) i want to make 4 modes of checking an alarm sensor
mode A: no ring only sensor check
mode B: every 15sec ring with every 5sec sensor check
mode C: every 30 sec ring with every 5sec sensor check
mode D: every 60sec ring with every 5sec sensor check

im using lot of time delays and also 12 different sounds so the program is big and maybe thats why i cant use more delays i dont know...
the goto im usisng it because i want to break from loops but continue check them
 

spinnaker

Joined Oct 29, 2009
7,830
You are going to have to be more clear than that. I am not sure what you are trying to say.

If I do understand you can have a variable to store the"mode". The have a switch statement and a case for each mode. Look up switch / case for C on google.
 

ErnieM

Joined Apr 24, 2011
8,377
hello guys!
is there any way of repeat this code line without writing them?

delay_ms(5000);
if (porta.f0==0) {goto start;}

because if i write it the programm crashes i dont know why... maybe the delay times are too big
I don't now why either as you have shows such a small fragment of your code it is impossible to see what you are doing.

What is this mysterious "start" label you refer to?

What are these loops?

Have you ever heard of the "break" command in C?
 

ErnieM

Joined Apr 24, 2011
8,377
One thing I do note in the following:
mode A: no ring only sensor check
mode B: every 15sec ring with every 5sec sensor check
mode C: every 30 sec ring with every 5sec sensor check
mode D: every 60sec ring with every 5sec sensor check
Everything happens in an integer multiple of 5 seconds. Thus I have no idea why there is any reason for a delay other than 5 seconds.

15 seconds between rings means wait 3 periods between rings.
30 seconds between rings means wait 6 periods between rings.
60 seconds between rings means wait 12 periods between rings.
 
Top