Strange problem - need unused variable to make (Blink like) sketch run

Hello, I tried to little enhance Blink script and got stuck there:

it compiles without problems, but once uploaded, it lights pin 7 and does nothing more

But if I uncomment the two lines with led, it uploads and start blinkind all pins 7-13.

If I comment out the led, it stuck again.

Can anybody explain me, where the problem is, and how I should solve it clear way?

(Edit: IDE is 1.0.5, emerged this week from Gentoo. Board is UNO, all examples from book works)

//int led = 7;
void setup() {                
  //led++;
  for ( int i=7;i<14;i++){
    pinMode(i, OUTPUT);
  }
}

void loop() {
  for ( int i=7;i<14;i++){
    digitalWrite(i, HIGH);
    delay(100);
    digitalWrite(i, LOW);
    delay(100);
  }
}

Interesting. I would expect the compiler to optimize those statements out of existence, since assigning led a value and incrementing it never does anything useful.

Can anybody explain me, where the problem is

No because there is nothing wrong with that code you posted, if you uncomment those two lines then there will be no change in what the sketch does because the variable led is not used in any other part of the sketch. Are you sure you posted the right thing?

I am sure, I posted the right thing. I tryit more times and did more variants of it, this one is the simplest, which does something visible.

I also tried avr-objdump -DCfhlSwz Ugh.cpp.elf >Ugh.cpp.asm and found, that there is difference, as the working version have .data segment and does incrementing the led

    led++;
 104:   80 91 00 01     lds     r24, 0x0100
 108:   90 91 01 01     lds     r25, 0x0101
 10c:   01 96           adiw    r24, 0x01       ; 1
 10e:   90 93 01 01     sts     0x0101, r25
 112:   80 93 00 01     sts     0x0100, r24

Do you have access to another computer? I'd be interested in knowing whether the object code is different on Windows vs. GenToo.

int led = 7;         //  This assigns the value 7 to a variable called led
void setup() {                
  led++;                //  This increments led, making it have a value of 8
  for ( int i=7;i<14;i++){      // these two lines set pins 7 - 13 to OUTPUT
    pinMode(i, OUTPUT);
  }
}

void loop() {
  for ( int i=7;i<14;i++){
    digitalWrite(i, HIGH);  // This line sets pins 7 - 13 HIGH, 1 pin per loop
    delay(100);
    digitalWrite(i, LOW);  // This line sets pins 7 - 13 LOW, 1 pin per loop
    delay(100);
  }
} // and this line makes it go all around the loop, setting pins 7 - 13 HIGH/LOW in turn

You need to use the variable led in your digitalWrite(). Just because you called it led, doesn’t make it automatically know what to do.

No, I am now far from home with netbook only. Iven in home I do not have any Windows, just couple of Gentoo machines, usually even without Xs or monitor attached (ssh only access)

lar3ry:

int led = 7;         //  This assigns the value 7 to a variable called led

void setup() {                
  led++;                //  This increments led, making it have a value of 8
 for ( int i=7;i<14;i++){      // these two lines set pins 7 - 13 to OUTPUT
   pinMode(i, OUTPUT);
 }
}

void loop() {
 for ( int i=7;i<14;i++){
   digitalWrite(i, HIGH);  // This line sets pins 7 - 13 HIGH, 1 pin per loop
   delay(100);
   digitalWrite(i, LOW);  // This line sets pins 7 - 13 LOW, 1 pin per loop
   delay(100);
 }
} // and this line makes it go all around the loop, setting pins 7 - 13 HIGH/LOW in turn




You need to use the variable **led** in your digitalWrite(). Just because you called it led, doesn't make it automatically know what to do.

I am using variable i in digitalWrite, which goes from 7 to 13 to make blik different diodes. Just namind the variable led does not mean I want it use for anything. This code works and makes running light over leds 7-13, regardles value of led variable.

The problem is, when I comment out the led variable (both declaration and incrementation) the sketch stops working and lights only led 7. And I do not understand why, because the variable led here is totally nonsencial (it is rest from Blik sketch and when i deleted it it istopped working. When I returned it, it worked again.)

The problem is, when I comment out the led variable (both declaration and incrementation) the sketch stops working and lights only led 7.

Hey guys, this is beginning to ring bells from quite a few years ago. We had something like this then. Trouble is I can't remember the details but I think it was something to do with the compiler.

Also - if i comment out both delay(), then it runs ok even with led commented out (only all leds are blinking so fast, that they look just dimmed)
also if i use my own delay, it works ok everytime

void delay2(int d) {  //does some delay, do not care about correct timing
  volatile int a; // do not want be optimized out by compiler
  for (int i=0;i<d;i++) {
    for (long unsigned l=0;l<0xFFF;l++) {
      a=d; // do not want be optimized out by compiler
    }
  }
}

Another try - I installed git-version of Arduino (not suported by portage) and from it works. Still I do not understand, what happend and why (and I would like to understand it)

gilhad:
it compiles without problems, but once uploaded, it lights pin 7 and does nothing more

But if I uncomment the two lines with led, it uploads and start blinkind all pins 7-13.

If I comment out the led, it stuck again.

I used IDE 1.0.4 on Windows 7 Pro 64-bit to upload the code in the original post to a UNO R3, and pin 13 blinked briefly at a bit less than 1Hz. That's what I'd expect from that code. Then I tried again using IDE 1.5.2 - no problem there either. In other words, I couldn't reproduce the problem.

Has anyone else reproduced the problem?

PeterH:
I used IDE 1.0.4 on Windows 7 Pro 64-bit to upload the code in the original post to a UNO R3, and pin 13 blinked briefly at a bit less than 1Hz. That's what I'd expect from that code. Then I tried again using IDE 1.5.2 - no problem there either. In other words, I couldn't reproduce the problem.

Has anyone else reproduced the problem?

I have not been able to reproduce the failure on IDE 1.5.4

Tried on Linux Mint 15 and Xubuntu with whatever version came from the repositories on my Uno. Got the same result PeterH did.

Trying to install Gentoo on a VM and see if I can replicate the problem.