SOLVED - Simple code not working unless I do something daft

This code used to work, it generates some random sounds a bit like R2D2 would make:-

// R2D2 by Grumpy Mike
const byte soundPin = 13;
void setup() {
  pinMode(soundPin,OUTPUT);
  randomSeed(analogRead(0));
  //Serial.begin(9600);
}

void loop() {
  for(int i; i<50; i++){
     note(random(100,2000));
  }
  delay(2000);
}

void note(long halfPeriod) {
  for (int j=0; j<20; j++) {
     digitalWrite(soundPin,HIGH);
     delayMicroseconds(halfPeriod);
     digitalWrite(soundPin,LOW);
     delayMicroseconds(halfPeriod);
  } 
}

Now it doesn’t produce any sound at all unless I include the totally redundant “Serial.begin” call. Even then the sound is continuous and doesn’t pause for the 2 seconds it should.

I am using a Uno ( tried two ) and version 1.8.12 of the IDE. I did upgrade to 1.8.13 but with no change. I am running the IDE on a Mac with OS 10.14.6 ( Mojave ).

This used to work the last time I checked it about five years ago. Been playing with this for an hour now.
Any ideas guys as to what might have changed?

Is Serial on pins 0 & 1 by any chance? Not sure why that should affect it though.

Oops:

  for (int i; i < 50; i++)

i is not initialized.

not the IDE version is relevant. the AVR boards version or even the avr toolchain version matters (megaavr boards package installs newer and avr core use it then)

wildbill:
Oops:

  for (int i; i < 50; i++)

i is not initialized.

You got it. The sketch works with the following structure: (There is no need to include Serial.begin() method).

  for (int i=0; i < 50; i++)

Working sketch:

// R2D2 by Grumpy Mike
const byte soundPin = 13;

void setup() 
{
  pinMode(soundPin, OUTPUT);
  randomSeed(analogRead(0));
  //Serial.begin(9600);
}

void loop() 
{
  for (int i=0; i < 50; i++) 
  {
    note(random(100, 2000));
  }
 delay(2000);
}

void note(long halfPeriod) 
{
  for (int j = 0; j < 20; j++) 
  {
    digitalWrite(soundPin, HIGH);
    delayMicroseconds(halfPeriod);
    digitalWrite(soundPin, LOW);
    delayMicroseconds(halfPeriod);
  }
}

Doh!

Thanks, I think wildbill was the first to get it.

I must have been looking at that for hours and not seeing it. Well it's off to the old folks home for me.

Cheers

Now, you or someone else may present an analysis why the sketch is not working with uninitialized i; instead, it is working with Serial.begin() method.

GolamMostafa:
Now, you or someone else may present an analysis why the sketch is not working with uninitialized i; instead, it is working with Serial.begin() method.

What on earth would the value of that be? Millions of sketches that use uninitialized variables might fail - but for a million different reasons. All it does is introduce an element of uncertainty into the program behaviour, and for extremely obvious reasons. That is all anyone needs to know.

aarg:
What on earth would the value of that be? Millions of sketches that use uninitialized variables might fail - but for a million different reasons. All it does is introduce an element of uncertainty into the program behaviour, and for extremely obvious reasons. That is all anyone needs to know.

The sketch has been behaving in the same pattern with uninitialized i; there is absolutely no random behavior observed. That means that there is a known and unexplored relationship between i and Serial.begin().

No immediate benefit of knowing that hidden fact; but, in the long run it adds value to human civilization.

Perhaps, you have heard the name of Mother Teresa who added just a drop of water with the vast volume of ocean when considering her humanitarian activities in the Calcutta city of West Bengal of India. Why did she do that? She showed to the mankind that she had only that drop of water to add and she had added; otherwise, the ocean had to live with a shortage of that water drop.

GolamMostafa:
No immediate benefit of knowing that hidden fact; but, in the long run it adds value to human civilization.

Until Human Civilisation takes another handbrake-turn next time there’s a new compiler/linker combination.l

Who (and why) caught so many spelling mistakes and unmatched () and {} in various posts of this Forum?

Who? @...

Why?
Just to add values! People learnt that they had done mistakes for some reasons and became alert not to make the same mistakes in the future.

GolamMostafa:
No immediate benefit of knowing that hidden fact; but, in the long run it adds value to human civilization.

Perhaps, you have heard the name of Mother Teresa who added just a drop of water with the vast volume of ocean. Why did she do that? She showed to the mankind that she had only that drop of water to add and she had added; otherwise, the ocean had to live with a shortage of that water drop.

The problem is, we're actually drowning in raw information, such that we have to use aggregation and filtering on a grand scale in order to use any of it. I think anyone would find the fine grained explication of exactly what the end results of an obvious mistake, difficult to learn anything from and quickly and easily forgotten. If there were anything subtle about it that you could use to diagnose another instance of the mistake, then I would agree with you. But I very much doubt that there is, as the effects are essentially open-ended and depend entirely on how the program uses the variable.

What benefits civilization, regards technology, is relevant, targeted, timely and practical knowledge. Not obsessive exposition of details that are only useful in one particular case or another. Not unless it is a case of something that does work, and many people wish to study or duplicate it.

GolamMostafa:
Who (and why) caught so many spelling mistakes and unmatched () and {} in various posts of this Forum?

Just to add values!

In this case, an uninitialized variable was found. That is valuable. But that is where the value ends. As I said, if there were anything subtle about the effect, it would make it worthwhile but in this case, it's obvious and not particularly instructive about any general principle or phenomenon.

GolamMostafa:
The sketch has been behaving in the same pattern with uninitialized i; there is absolutely no random behavior observed. That means that there is a known and unexplored relationship between i and Serial.begin().

It is obvious what is happening. The call to Serial.begin leaves the stack in a state that will be the same every time the sketch is run. The uninitialized i picks up something from the stack that the call left behind. There is no mystery. If you really care, you can go through the hex dump of the program and see exactly what i picks up. But there is nothing to be gained from such activity, except perhaps the ability to read the hex dump if you don't already know.

wildbill:
It is obvious what is happening.

Yes! It is now obvious because of long discussion. The i picks up some fixed value from somewhere (the stack as you are saying) following the execution of Serial.begin() in the setup() function.

A natural mind wants to know two things: the "result" of an experiment and the "events/steps that have produced the result".

To an end user, the "result" is good enough; but, to the producer both "result' and "process" are equally important.

GolamMostafa:
A natural mind wants to know two things:

The natural mind should try to see things in perspective.

In Reply #12 @aarg said

In this case, an uninitialized variable was found. That is valuable.

I don't even think it has any value for anyone other than @GrumpyMike - and even for him I suspect the value is small. It's not as if he did not know how the code should have been written.

And it's certainly not the type of error, or solution that provides a useful lesson for anyone else. Reading this Thread is not the way to learn how to create a FOR loop.

Further analysis is not going to add useful general value to something that starts with no general value.

In any case @GrumpyMike posted his code so there is nothing to prevent you taking it away, studying it in as much detail as you wish and then writing your thesis.

...R

Robin2:
The natural mind should try to see things in perspective.

Amen.

See specifically, Trin Tragula.

According to Zaphod Beedalbrox the last thing anyone need is a sense of perspective.

Robin2:
The natural mind should try to see things in perspective.

A "Natural Mind" is an "Unpolluted Mind" and asks only two questions -- "what is this?" and "how has it come here?".

To see a thing in "perspective", there is a need of a "polluted mind" which is enriched with the faculty of "art".

The natural mind says “I broke the rules. I know the rules. I won’t break that rule again”