About loop() and setup() function

why does the following code not work?

#define LED 3

void mydelay();

void setup(){
    DDRD |= (1<<LED);
}

void loop(){
   PORTD ^= (1<<LED);
   mydelay();
}

void mydelay(){
int c, d;
   
   for (c = 1; c <= 32767; c++)
       for (d = 1; d <= 32767; d++)
       {}
       
}

And why does this not work?

#define LED 3

void mydelay();

void setup(){
    pinMode(LED,OUTPUT);
}

void loop(){
   digitalWrite(LED, HIGH);
   mydelay();
   digitalWrite(LED, LOW);
   mydelay();
}

void mydelay(){
int c, d;
   
   for (c = 1; c <= 32767; c++)
       for (d = 1; d <= 32767; d++)
       {}
       
}

Are there any requirements on using loop() and setup() ?

Because optimizers remove code without any actions.

Instead of

I would use

  PIND = 1 << LED;

The question is: what do you expect that the code does?

From what I see, you are assigning the result of PORTD XOR 8 to PORTD

If you meant to toggle the LED connected to PORTD, then
PORTD ^= 1 is much better.

Nope, that would toggle the wrong bit.

Actually the optimizers probably remove the mydelay () nested loops, as they don't serve a purpose. The body of the nested loop is empty. Why looping ?

(of course to burn time)

will this toggle?i don't think so

That would toggle the first (LSB) bit, correct. If eestudent wants to toglle the 3rd bit, then eestudent is correct to do PORTD ^= (1<<LED)

The crap you quoted will not.

PIND = 1 << LED;

will toggle the bit.

An 'int' variable on an Arduino UNO will always be "<= 32767" so both of your loops are infinite. Once you enter the 'mydelay()' function you never get out again.

1 Like

what do you mean?

hmm? this is a simple delay function, why would not exit the loop, the first counts from 1 to 32767 and second loop counts similarly and exits. what do you mean loops are infinite? take any number for c and d the code will not function.

I strongly agree to johnwasser.

integers are signed numbers (here 16 bit wide). Therefore, they are never going to exceed 32767. They rather overflow to -32768. Therefore you only turn the LED on once and never toggle again.
You need to exit earlier at 32766 or something less than that.

Assume d = 32766, then it (d) will be incremented to 32767 which the max +ve count limit.

You have tested for d = 32767 which is satisficed; now, d will be incremented to (0x7FFF + 1 = 0x8000 in hex's 2's complement code = - 32768 in decimal. d will be again incremented and incremented, ..., infinite looping!!

So, the for() could be like the following:

for(d = 1; d < 32767; d++)
{
     ;
}

You are checking for "<= 32767". It would have to count to 32768 to get out of the loop but 32768 can't be represented in a 16-bit signed integer.

is this ok?

 void mydelay(){
int c, d;
   
   for (c = 1; c <= 1000; c++)
       for (d = 1; d <= 1000; d++)
       {;}
}

It looks like it doesn't have the integer overflow problem. Does it do what you want?

Probably not, because it doesn't do anything, and the optimizer will remove all the code.

it does not work

#define LED 3

void mydelay();

void setup(){
    pinMode(LED,OUTPUT);
}

void loop(){
   digitalWrite(LED, HIGH);
   mydelay();
   digitalWrite(LED, LOW);
   mydelay();
}

 void mydelay(){
int c, d;
   
   for (c = 1; c <= 1000; c++)
       for (d = 1; d <= 1000; d++)
       {;}
}

why?

why would the optimizer remove it? it is counting but if it makes you happy let say we do something in the loop.

 void mydelay(){
int c, d;
   
   for (c = 1; c <= 1000; c++)
       for (d = 1; d <= 1000; d++)
       {
	 digitalWrite(8,HIGH);
       }
}

it will not work

The optimiser might see that you are doing only one thing, and just do that.
You could qualify the variables "volatile".

Or you could use avr-objdump and examine the generated code