Pages: [1]   Go Down
Author Topic: Starter Kit Project 8 Hour Glass - if(led == 7)  (Read 158 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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() {
  Serial.begin(9600); // added by me
  // 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
    }
  }

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
Not entirely sure I understand your reply.

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 Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry I wrote "for clarity" before getting your second reply.
I am looking at your replies and still trying to fathom it out :-)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 == smiley-cool" 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 Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your reply.

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 Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 smiley-sad

(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
Jump to: