Go Down

Topic: Blinking and Serial Monitor (Read 748 times) previous topic - next topic

vios

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 :D but not alternately as what I want. What I am doing wrong here.

Please Help. :(

Code: [Select]
 
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
 }
}
   

         http://arduinostudio.wordpress.com/

Coding Badly

Code: [Select]
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: [Select]
{
   Serial.println("off");
   }


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

Code: [Select]
 {
 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?

vios

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.

         http://arduinostudio.wordpress.com/

AWOL

#3
Sep 29, 2009, 08:39 am Last Edit: Sep 29, 2009, 11:49 am by AWOL Reason: 1
Code: [Select]

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");
}
"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.

programmer

The problem comes from:

Code: [Select]
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: [Select]

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.

vios

Hi.

Yes. That settle the problem. Thanks programmer.

And thanks to all have reply.

         http://arduinostudio.wordpress.com/

Go Up