Wanting to see things quickly in project 08 I made interval 200.
This is great, and means you don't have to wait an hour too see the thing working. But a curious thing happens, it all works as expected... but after a certain length of time the whole hourglass starts all over again!
So I got curious and added some debug code, namely printing out the value of led. And when led gets to 76 things go weird. Can anyone explain this to me?
sketch:
/*
Arduino Starter Kit example
Project 8 - Digital Hourglass
This sketch is written to accompany Project 8 in the
Arduino Starter Kit
Parts required:
10 kilohm resistor
six 220 ohm resistors
six LEDs
tilt switch
Created 13 September 2012
by Scott Fitzgerald
http://arduino.cc/starterKit
This example code is part of the public domain
*/
// named constant for the switch pin
const int switchPin = 8;
unsigned long previousTime = 0; // store the last time an LED was updated
int switchState = 0; // the current switch state
int prevSwitchState = 0; // the previous switch state
int led = 2; // a variable to refer to the LEDs
// 600000 = 10 minutes in milliseconds
long interval = 200; // interval at which to light the next LED
void setup() {
// set the LED pins as outputs
for(int x = 2;x<8;x++){
pinMode(x, OUTPUT);
}
// set the tilt switch pin as input
pinMode(switchPin, INPUT);
Serial.begin(9600);
}
void loop(){
// store the time since the Arduino started running in a variable
unsigned long currentTime = millis();
// compare the current time to the previous time an LED turned on
// if it is greater than your interval, run the if statement
if(currentTime - previousTime > interval) {
// save the current time as the last time you changed an LED
previousTime = currentTime;
// Turn the LED on
digitalWrite(led, HIGH);
// increment the led variable
// in 10 minutes the next LED will light up
led++;
Serial.print("led: ");
Serial.println(led);
if(led >= 7) {
Serial.print("led is more than 7!!!!!!!!!!!! ");
}
}
// read the switch value
switchState = digitalRead(switchPin);
// if the switch has changed
if(switchState != prevSwitchState){
// turn all the LEDs low
for(int x = 2;x<8;x++){
digitalWrite(x, LOW);
}
// reset the LED variable to the first one
led = 2;
//reset the timer
previousTime = currentTime;
}
// set the previous switch state to the current state
prevSwitchState = switchState;
}
typical readout:
led is more than 7!!!!!!!!!!!! led: 70
led is more than 7!!!!!!!!!!!! led: 71
led is more than 7!!!!!!!!!!!! led: 72
led is more than 7!!!!!!!!!!!! led: 73
led is more than 7!!!!!!!!!!!! led: 74
led is more than 7!!!!!!!!!!!! led: 75
led is more than 7!!!!!!!!!!!! led: 76
led is more than 7!!!!!!!!!!!! led: 4429
led is more than 7!!!!!!!!!!!! led: 4430
led is more than 7!!!!!!!!!!!! led: 4431
led is more than 7!!!!!!!!!!!! led: 3
led: 4
led: 5
led: 6
led: 7
led is more than 7!!!!!!!!!!!! led: 8
led is more than 7!!!!!!!!!!!! led: 9
led is more than 7!!!!!!!!!!!! led: 10
led is more than 7!!!!!!!!!!!! led: 11
led is more than 7!!!!!!!!!!!! led: 12etc
Seems it's maybe some sort of memory thing? Because when I make the fabulously informative write " Serial.print("led is more than 7!!!!!!!!!!!! ");" have a few more exclamation marks in it it starts to behave differently