I conduct experiments with Arduino and decided to torture a while loop (instead of a loop). I set the condition under which the cycle should work, but instead of triggering the LED just lights up and that's it. Here is the code:

int main() { int i = 0; DDRB = B00100000; while (i < 200) { PORTB |= B00100000; delay(1000); PORTB &= B00000000; delay(1000); i++; } } 

Maybe I'm stupid, or the compiler's brain swam?

  • There is a suspicion that your whole set knows nothing about the main ... - Vladimir Martyanov
  • @ VladimirMartiyanov He knew yesterday - Super_Puper_User

2 answers 2

The while() works, in this case delay() does not work, and in order for it to work (like other similar arduino functions), you must first initialize the arduino library itself with init() .

 int main() { int i = 0; init(); DDRB = B00100000; while (i < 200) { PORTB |= B00100000; delay(1000); PORTB &= ~B00100000; delay(1000); i++; } } 

An alternative would be to write your own implementation of delay() using active wait or a timer.

PS: This issue does not apply, but if you turn on the pin cleanly, then you should also turn it off cleanly:

  PORTB &= ~B00100000; 
  • It does not work (the init seems to have helped first, but then this problem returned - Super_Puper_User
  • Arduin's delay worked, but the delay from #include <util/delay.h> does not work correctly - it caught fire, waited a second, went out and that's it, although it is a cycle ... - Super_Puper_User
  • @Timoha_Timohavich, MWRI, the function is called _delay_ms() - Fat-Zer
  • @ Fat-Zet. Thank! - Super_Puper_User
  • why not just &= , but &= ~ ? ~ Invents - it was B00100000 , and it became B11011111 - This is not right ... - Super_Puper_User

Working directly with port registries is not a good idea.

Try to do it through functions: pinMode , digitalWrite

  • And nothing, that from this more memory is eaten? - Super_Puper_User
  • Well yes. The program is huge. Every bit counts. - newman
  • + the problem remains - Super_Puper_User
  • Problem still exists. Even if you change everything on pinMode, digitalWrite. - Super_Puper_User
  • one
    Working with ports instead of brake digitalWrite is a good idea - Vladimir Martyanov