Go Down

Topic: What is wrong with this code? (Read 593 times) previous topic - next topic

Ufoguy

:~
Code: [Select]

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.


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

AWOL

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

pYro_65

#2
Jan 15, 2012, 01:13 pm Last Edit: Jan 15, 2012, 01:17 pm by pYro_65 Reason: 1
Code: [Select]
ledPin += ledPin;

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

Code: [Select]
++ledPin;

this will increment by one

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

Code: [Select]
int ledPin = 13;

void setup()
{
 pinMode(ledPin, OUTPUT);
}

Ufoguy

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

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

AWOL

"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.

PaulS

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.


Stealth0113

Look at this:

Code: [Select]

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


Easy, clean and does the job!
Watch my stealth-bot @ let's make robots

Ufoguy

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.

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

Go Up