Pages: [1]   Go Down
Author Topic: What is wrong with this code?  (Read 567 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 181
So, you want to make something, huh?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley-confuse
Code:
int delayTime = 1000;
int ledPin = 13;

void setup()
{
  pinMode(ledPin, OUTPUT);
}
void loop()
{
  if(ledPin == 14)
  {
    ledPin = 9;
  }
  digitalWrite(ledPin, HIGH);
  delay(delayTime);
  ledPin += ledPin;
}
 

It compiles well but only pin 13 is always high and don't get the expected result i.e; scanning effect.

This is the first time I've tried something at this level on my own.

Expectation:
Pin 13 must be high for 1000 ms and then ledPin must be set to 14 and the if function must check the value and set it back to 9 and then 10, then 11, then 12, then 13, then 9 and so on again and again(Loop).

What really happens:
The pin 13 is always high.
I'm using Arduino 1.0

What the problem might be:
Maybe something with the if statement.

Please help me out.


Logged

If you want to meet a beautiful nurse you must be patient.

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

How many pins have you made outputs?
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.

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 76
Posts: 2247
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
ledPin += ledPin;

this will always add 13, your code will never enter the if statement

Code:
++ledPin;

this will increment by one

And AWOL has a point, only pin 13 is an output

Code:
int ledPin = 13;

void setup()
{
  pinMode(ledPin, OUTPUT);
}
« Last Edit: January 15, 2012, 07:17:54 am by pYro_65 » Logged


Offline Offline
Full Member
***
Karma: 2
Posts: 181
So, you want to make something, huh?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys. I get the not declaring other pins as outputs point but why is ledPin += ledPin going to work Pyro_46 ?

Logged

If you want to meet a beautiful nurse you must be patient.

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

Because 13+13 = 26
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.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50947
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pins don't turn off automatically. Once you turn it on, it stays on. If you want it to go off, you must turn it off.

Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 80
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at this:

Code:
for (int i=9; i <= 13; i++){
      digitalWrite(ledPin[i], HIGH);
      delay(1000);
      digitalWrite(ledPin[i], LOW);
   }

Easy, clean and does the job!
Logged

Watch my stealth-bot @ let's make robots

Offline Offline
Full Member
***
Karma: 2
Posts: 181
So, you want to make something, huh?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys.
Here's a summary of what I learnt from this post.

ledPin += ledPin = 2*ledPin NOT ledPin + 1

You have to set a pin low to turn it off.(LOL).

You have to declare every pin you're gonna as OUTPUT or INPUT use in void start().

I've also learnt how to use for command.

Logged

If you want to meet a beautiful nurse you must be patient.

Pages: [1]   Go Up
Jump to: