Pages: [1]   Go Down
Author Topic: Blinking and Serial Monitor  (Read 573 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

Another newbie here.

I am trying to make an Led blink and send the status either on or of to serial monitor. I got the status display smiley-grin but not alternately as what I want. What I am doing wrong here.

Please Help. smiley-sad

Code:
 
const int ledPin = 13;       // the pin that the LED is attached to
//int ledPin =  13;    // LED connected to digital pin 13
int ledState = 0;         // current state of the button
// The setup() method runs once, when the sketch starts

void setup()   {                
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop() {
  ledState = digitalRead(ledPin);
  
  if (ledState == HIGH){
    Serial.println("on");
  }
  else if (ledState == LOW );{
    Serial.println("off");
    }
  {
  digitalWrite(ledPin,LOW);    // set the LED off
  delay(1000);                  // wait for a second
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);    // wait for a second
  }
}
    
Logged


          http://arduinostudio.wordpress.com/

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int ledPin = 13;       // the pin that the LED is attached to
int ledState = 0;         // current state of the button

void setup()   {                
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  ledState = digitalRead(ledPin);
  
  if (ledState == HIGH){
    Serial.println("on");
  }
  else if (ledState == LOW );

In the line of code above, the semi-colon after the closing parentheses terminates the if.  Which means off is printed every time through the loop.  The semi-colon should be removed.

Code:
{
    Serial.println("off");
    }

The braces around the code below aren't harmful but they serve no useful purpose.

Code:
 {
  digitalWrite(ledPin,LOW);    // set the LED off
  delay(1000);                  // wait for a second
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);    // wait for a second
  }
}

Does that help?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

After remove the semi colon now it's only shows on status.

What I want is to show the status of the Led either on or off accordingly.

Thanks for reply.
Logged


          http://arduinostudio.wordpress.com/

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

if (ledState == HIGH){
    Serial.println("on");
  } else {  // If it isn't HIGH, it must be LOW, so no need for an "if"
    Serial.println("off");
}
« Last Edit: September 29, 2009, 04:49:33 am by AWOL » 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.

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 94
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem comes from:

Code:
void loop() {
  ledState = digitalRead(ledPin);
  
  if (ledState == HIGH){
    Serial.println("on");
  }
  else if (ledState == LOW );{
    Serial.println("off");
    }
  {
  digitalWrite(ledPin,LOW);    // set the LED off
  delay(1000);                  // wait for a second
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);    // wait for a second
  }
}

If you go through the program, it first checks the state of ledPin, and prints the value, high or low.

You then set ledPin low, wait, set ledPin high, and then wait again.

The problem comes because you are setting ledPin low then high each time. This means that each time the loop starts after the first iteration, ledPin will always be high (as that was the last state setting).

What you need to do is something like:

Code:
void loop() {
  ledState = digitalRead(ledPin);
  
  if (ledState == HIGH){
    Serial.println("on");
    digitalWrite(ledPin,LOW);
  } else {
    Serial.println("off");
    digitalWrite(ledPin,HIGH);
  }
  delay(1000);
}

This only toggles the state of ledPin once per loop, so each time round the loop it will have a different state to the previous iteration.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

Yes. That settle the problem. Thanks programmer.

And thanks to all have reply.
Logged


          http://arduinostudio.wordpress.com/

Pages: [1]   Go Up
Jump to: