Help getting this code to work (or can I even?)

So, I just watched this numberfile episode, it was pretty good. The part at 9:04 got me interested, so I decided hey, why not throw that into an arduino?

I’m thinking I did it improperly but I am not sure how, I have a feeling it is simple but I am just not seeing it lol.

int DelayTime;

void setup() {
  pinMode(13, OUTPUT);
  int DelayTime = 60000;
  Serial.begin(9600);
  Serial.print(DelayTime);
  Serial.println();

}

void loop() {
  delay(DelayTime);
  Serial.print(DelayTime);
  Serial.println();
  digitalWrite(13, HIGH);
  DelayTime = (DelayTime / 2);
  Serial.print(DelayTime);
  Serial.println();
  delay(DelayTime);
  digitalWrite(13, LOW);
  DelayTime = (DelayTime / 2);
  Serial.print(DelayTime);
  Serial.println();
}

It starts out at -5536, and then just does all zeroes. What went wrong? Did I do it correctly and it just doesn't work because it's a but of a paradox?

You need to read up on scope.

int DelayTime;

A global variable, initialized by default to 0.

  int DelayTime = 60000;

A local variable, initialized to 60000.

  Serial.print(DelayTime);
  Serial.println();
}

After being printed, the variable then goes out of scope, and the value is lost.

  delay(DelayTime);
  Serial.print(DelayTime);

Now, you are accessing the global variable.

Having global and local variables with the same name is a bad idea.

(Nick, this should be on your traps/tips page.)

XOIIO: It starts out at -5536

60000 is too big for an variable of type int

guix:

XOIIO: It starts out at -5536

60000 is too big for an variable of type int

ahh, right. Revised the script, seems to be working fine :)

long DelayTime;

void setup() {
  pinMode(13, OUTPUT);
  DelayTime = 60000;
  Serial.begin(9600);
  Serial.print(DelayTime);
  Serial.println();

}

void loop() {
  delay(DelayTime);
  Serial.print(DelayTime);
  Serial.println();
  digitalWrite(13, HIGH);
  DelayTime = (DelayTime / 2);
  Serial.print(DelayTime);
  Serial.println();
  delay(DelayTime);
  digitalWrite(13, LOW);
  DelayTime = (DelayTime / 2);
  Serial.print(DelayTime);
  Serial.println();
}

Interesting, the serial output went like this...

60000
30000
15000
15000
7500
3750
3750
1875
937
937
468
234
234
117
58
58
29
14
14
7
3
3
1
0
0
0
0
0
0

And the zeroes just continued, the LED was sort of half on. I guess that's the result?

I guess that's the result?

Given the code you posted, that's what I'd expect.

Well I'm wondering if that is indeed the proper answer to the last question in the video , I guess it seems right, but then again i've been up way too long for math stuff.

Yup, times it and that is indeed the answer. Thanks for the help.

Well I'm wondering if that is indeed the proper answer to the last question in the video

How are we supposed to know? For all I know, "the video" is something you made in your parent's basement without their permission or knowledge.

man i always forget the link

http://www.youtube.com/watch?v=PCu_BNNI5x4