How do I use for statement to run the whole code twice, then stop

  • Thread starter Deleted member 750607
  • Start date

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
So answer your own question! Do you still need the for statement? It should be easy to answer because you understand the code so well.

(Sorry for any attitude. I’m trying to make a point)
you know I come on here for help with different concepts, and I was actually asking you to elaborate a little on how these terms and functions you use actually work in the program, while(true) for example. I don't deny that I'm still a beginner with code and microcontrollers. that's why I need help. hence the forum...

but you trying to make me feel bad for not understanding is making me feel like I shouldn't post anything ever. people have different learning styles. I have a disability myself. sometimes reading a book or an article isn't enough. sometimes I cant keep track of 20 different thread responses. that's not a crime. this is a public forum and im allowed to ask questions more than once.
 

djsfantasi

Joined Apr 11, 2010
9,156
you know I come on here for help with different concepts, and I was actually asking you to elaborate a little on how these terms and functions you use actually work in the program, while(true) for example. I don't deny that I'm still a beginner with code and microcontrollers. that's why I need help. hence the forum...

but you trying to make me feel bad for not understanding is making me feel like I shouldn't post anything ever. people have different learning styles. I have a disability myself. sometimes reading a book or an article isn't enough. sometimes I cant keep track of 20 different thread responses. that's not a crime. this is a public forum and im allowed to ask questions more than once.
Ok, I’m sorry. I didn’t mean to make you feel sorry. I was just using a style of teaching that I’m familiar with. I missed that it wasn’t the best style for you. It’s a problem with a forum for me. Visual clues that help in assessing the value of a particular teaching style is missing. Thank you for sharing your personal story. It will help me help you.

So, with that in mind, where do we stand? So I answer the correct question, can you repeat your last one? I hope you’ll let me try again.
 

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
Ok, I’m sorry. I didn’t mean to make you feel sorry. I was just using a style of teaching that I’m familiar with. I missed that it wasn’t the best style for you. It’s a problem with a forum for me. Visual clues that help in assessing the value of a particular teaching style is missing. Thank you for sharing your personal story. It will help me help you.

So, with that in mind, where do we stand? So I answer the correct question, can you repeat your last one? I hope you’ll let me try again.
thanks. ive actually encountered this a bit from others on the forum so I have come to understand its part of the culture...youre doing fine I also understand that Im not good with this format. maybe I should try something else

im just wondering... if halt and while(true) are kinda the same thing in this code, and it is "halting" - "while the statement is true"...why is it outside of those "for" loop brackets and not inside with the instructions for the LEDs to flash and whatnot?
 

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
thanks. ive actually encountered this a bit from others on the forum so I have come to understand its part of the culture...youre doing fine I also understand that Im not good with this format. maybe I should try something else

im just wondering... if halt and while(true) are kinda the same thing in this code, and it is "halting" - "while the statement is true"...why is it outside of those "for" loop brackets and not inside with the instructions for the LEDs to flash and whatnot?
actually I see now. I was reviewing the code and I think I get it now
 

BobTPH

Joined Jun 5, 2013
8,813
while(true); is a simple loop that keeps running as long as true is true, i.e. forever.

A micro, when it is running, is always executing some code. The halt hack (yes, it is a hack) gives it something ti execute forever without having any effect.

Bob
 

MrChips

Joined Oct 2, 2009
30,712
while (true) ;
computes to code that loops forever. It compiles to:

here: goto here

This is a simple way to make a program hang.

A HALT instruction depends on the compiler and platform. It could be compiled to a statement that returns control to the operating system that invoked the application code in the first place.

In embedded microcontroller applications there is never a need to HALT the program. The system is always running or perhaps in SLEEP mode.
 

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
while(true); is a simple loop that keeps running as long as true is true, i.e. forever.

A micro, when it is running, is always executing some code. The halt hack (yes, it is a hack) gives it something ti execute forever without having any effect.

Bob
yeah I thought that's what it was doing...like repeating nothing over and over
 

djsfantasi

Joined Apr 11, 2010
9,156
while (true) ;
computes to code that loops forever. It compiles to:

here: goto here

This is a simple way to make a program hang.

A HALT instruction depends on the compiler and platform. It could be compiled to a statement that returns control to the operating system that invoked the application code in the first place.

In embedded microcontroller applications there is never a need to HALT the program. The system is always running or perhaps in SLEEP mode.
However, in my example, halt refers to a macro or substitution to a do nothing loop. Externally, it looks like a HALT; internally it’s a do nothing loop b
 

djsfantasi

Joined Apr 11, 2010
9,156
im just wondering... if halt and while(true) are kinda the same thing in this code, and it is "halting" - "while the statement is true"...why is it outside of those "for" loop brackets and not inside with the instructions for the LEDs to flash and whatnot?
You want to repeat the LEDs flashing, correct? That’s what the for loop does. Repeat the flashing. If you were to put the do nothing loop inside the for loop, it would no longer repeat. It would run once and then do nothing forever.

Putting it outside the loop let’s the flashing repeat the desired number of times and then stop or do nothing.

YOU have defined halt to be the same as while(True) with the #define statement.

I suspect that with the many LED flashing statements, you’ve tried flashing the LEDs by manually repeating the code. Instead of repeating it with the for statements. Could this be true?
 
Last edited:

an_other

Joined Apr 22, 2021
2
Hallo, hbasile91.
May I make a couple of suggestions?
Several posters have told you what you wanted to know, but we seem to be having trouble getting the message across. First could I point you at the Arduino Reference (https://www.arduino.cc/reference/en/) - this deals with most of the 'C' stuff which applies to the Arduino, with examples. We all had to work through this an understand them - usually by writing test programs until we understand what is happening.
I would also like to stress one point which you seem not to have grasped - uPC don't stop under 'normal circumstances - they must always be executing some code or other. The C programming language used by the Arduinos also recognise this, and the structure of the programs always take the same form. There is a block of the program (setup) which only runs once when the program is run - this is used to set up various things, for example, port directions maybe - anything which only needs to be done once in order that the program will run.
Following this is a block called (loop). After the processor has run (setup) it will enter the (loop) block, and run the code inside this block repeatedly - it cannot be 'stopped' as such. It will go to the end of the block, then run it over and over again ad infinitum.
You are asking how you can run your code 2 (or 3) times then stop - this is actually two separate problems. The 'For' loop defines how often your block of code will run (NOT the void(loop) section. Originally, as you siad yourself, you started with the i value in the for loop set to 0, so the 'for' loop will run three times, for i = 1,2 and 3. In your original code, it will flash your LEDs 3 times. However, it will then have reached the end of the 'loop' block, so it will repeat the whole (loop) again, and again and again.... so the leds will apparently keep flashing.
You asked how to run your code once and stop - and this is technically not possible without a 'bodge' of some kind - bad program practise if you like. I and others suggested a simple never-ending loop at the end of the (loop) block - outside your 'for' loop. This means the 'for' loop runs (2 or 3 times) then the code enters a loop which it repeats for ever - and so apparently the whole process stops - but it hasn't, the processor is still running and executing the 'terminating' loop. The only way to get the LEDS to do their flash sequence again is to reset the board.
You seem to have been reading the answers here, and not understanding exactly what is happening, which is understandable when you are first trying to grasp the principles, so may I suggest you read through them again, and see if you can follow what we are trying to tell you - the Arduino Reference may also help you. If it is still not clear, come back to us with your question.
 
Top