Pages: [1]   Go Down
 Author Topic: Starter Kit Project 8 Hour Glass - if(led == 7)  (Read 146 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 8
 « on: June 02, 2014, 03:57:19 am » Bigger Smaller Reset

Hello,
Starter Kit Project 8 Hour Glass

I am working my way through the starter kit quite successfully, but on this project I have hit a problem for which I cannot find an answer to.
At the end of this project the book suggests adding my own code for an alert to say when the hour is up.

(For speed of testing I have changed the LED lighting times from 10 mins to seconds.)

Problem
if(led == 7)   My understanding of this code is when the LED is lit (LED number 6 in this project or LED attached to digital Pin number 7) then it will perform the added code below it.

Regardless of code added, this code if(led == 7) activates when LED number 5 is lit, (the LED attached to pin number 6) and not LED number 6, Digital pin 7, as I believe it should do.

Checked wiring on board, even copied  a new version of code from web to ensure I made no mistake in program.
LED number 6 is definitely wired to Digital Output 7.

Code is below, any help greatly appreciated - as you probably gathered I am quite new to Arduino.
Thanks

Code:
// 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 = 1000; // 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);
}

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++;

[b] if(led == 7){
digitalWrite(2, LOW); // added by me[/b]
Serial.println ("hour is up"); // added by me
// the hour is up
}
}

// 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;
}
 Logged

Seattle, WA USA
Offline
Brattain Member
Karma: 601
Posts: 48543
Seattle, WA USA
 « Reply #1 on: June 02, 2014, 04:06:13 am » Bigger Smaller Reset

You turn on a pin number. Then, you increment the number. Then, you test the number. Get the order of operations straight - turn on, test, increment.
 Logged

Offline
Newbie
Karma: 0
Posts: 8
 « Reply #2 on: June 02, 2014, 04:23:35 am » Bigger Smaller Reset

Hi,

By the way all this code is copied straight out of the Arduino Starter kit book - including  if(led == 7) - which the book refers to use as the end point to signify when the hour is up. (Currently works as 50 mins is up not an hour)

My question - should this code  if(led == 7) activate upon LED number 6 being lit? (Currently activates on LED no 5 lit)
Does the "7" refer to the Digital output pin number 7? (Currently activates on digital output pin number 6)

(Pin number 7 has LED 6 attached to it)

I am sure it is a simple explanation but your reply is not clear to what the problem here is.
Thanks
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 285
Posts: 25645
I don't think you connected the grounds, Dave.
 « Reply #3 on: June 02, 2014, 04:30:59 am » Bigger Smaller Reset

Code:
digitalWrite(led, HIGH);
You turn on a pin number.
Code:
led++;
Then, you increment the number.
Code:
if(led == 7){
Then, you test the number.

See the problem?
 Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline
Newbie
Karma: 0
Posts: 8
 « Reply #4 on: June 02, 2014, 04:32:45 am » Bigger Smaller Reset

For clarity everything else s working fine except for this part of code.

if(led == 7){
// the hour is up
}
}
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 285
Posts: 25645
I don't think you connected the grounds, Dave.
 « Reply #5 on: June 02, 2014, 04:38:50 am » Bigger Smaller Reset

For clarity, see replies #1 and #3.
 Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline
Newbie
Karma: 0
Posts: 8
 « Reply #6 on: June 02, 2014, 04:42:13 am » Bigger Smaller Reset

I am looking at your replies and still trying to fathom it out :-)
 Logged

Offline
Newbie
Karma: 0
Posts: 8
 « Reply #7 on: June 02, 2014, 05:22:09 am » Bigger Smaller Reset

So I believe this is the problem. - please confirm ?

When the variable LED is incremented to number 7 it has not yet lit up LED 6 (attached to digital Pin 7)  as it still has to wait another time period to elapse first.

The "LED" variable is incremented to the number 7 immediately after lighting up LED number 5, (attached to digital Pin)
(So only 50 minutes has elapsed and not an hour).

What I have realised then is;
The code if(led == 7) is checking the LED variable number and not checking Digital Pin 7. - My naivety
Now I believe that "if(led == 7)" should be after turning on a pin number but before incrementing the number.
(Or I can always change "if(led == 7)" to "if(led == " and leave in current location.

Finally if you confirm this is all correct and I hope it is then the Arduino Projects Book is incorrect see pic. - (I was really stuck to thinking the Arduino Projects book cannot be wrong!)

 image.jpeg (21 KB, 320x240 - viewed 4 times.) Logged

Offline
Newbie
Karma: 0
Posts: 8
 « Reply #8 on: June 02, 2014, 05:30:44 am » Bigger Smaller Reset

(Or I can always change "if(led == 7)" to "if(led == 8" and leave in current location.
 Logged

Offline
Newbie
Karma: 0
Posts: 8
 « Reply #9 on: June 02, 2014, 05:43:10 am » Bigger Smaller Reset

Ok changed the code as below and all functions correctly.
So I assume the book was wrong?
Thanks for your help in pointing me in the right direction.

Code:
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
if(led == 7){
digitalWrite(2, LOW); // added by me
Serial.println ("hour is up"); // added by me
// the hour is up

}
else if(led < 7){ //added by me

led++;

}
}
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 285
Posts: 25645
I don't think you connected the grounds, Dave.
 « Reply #10 on: June 02, 2014, 05:52:38 am » Bigger Smaller Reset

I don't have the book and have never seen it, and maybe it is a little advanced for the intended audience, but I would interpose a mapping between the logical LED number and the physical pin number by using a constant array.
This adds almost zero complexity to the code, but allows an increase in flexibility (allowing you to bypass pins with specific useful other functions like PWM) and simplifies porting.
 Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline
Newbie
Karma: 0
Posts: 8
 « Reply #11 on: June 02, 2014, 06:42:51 am » Bigger Smaller Reset

I'm probably not at that stage of understanding your suggestions yet :-)
I believe the book/starter kit takes me through the basics step by step, working up to more complex/easier ways to do things.

The Book comes with the official Arduino starter kit - hence why I beleived the book could not be wrong :-)

Thanks

 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 285
Posts: 25645
I don't think you connected the grounds, Dave.
 « Reply #12 on: June 02, 2014, 06:44:37 am » Bigger Smaller Reset

Quote
The Book comes with the official Arduino starter kit - hence why I beleived the book could not be wrong
The blink without delay example comes with every version of the IDE, but no-one seems to have got around to correcting it yet

(Like I said, I don't have the book, and I'm unlikely to acquire one now!)
 Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

 Pages: [1]   Go Up