Go Down

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

#### Ufoguy

##### Jan 15, 2012, 01:06 pm
:~
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.

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

#### AWOL

#1
##### Jan 15, 2012, 01:08 pm
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 pmLast 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

#3
##### Jan 15, 2012, 01:52 pm
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

#4
##### Jan 15, 2012, 01:57 pm
Because 13+13 = 26
"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

#5
##### Jan 15, 2012, 02:10 pm
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

#6
##### Jan 15, 2012, 03:26 pm
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

#7
##### Jan 18, 2012, 04:33 pm
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

Please enter a valid email to subscribe