OutputPin allways HIGH

Hello all,
I'm newest and sorry for my english.
Last week bought an Mega 2560 and try to starter with a simply code.

int Pin7 = 7;
int Pin40 = 0;

void setup() {

pinMode(7,INPUT);
pinMode(40, OUTPUT);
Serial.begin(9600);
}
void loop() {

Pin7 = digitalRead(7);
if(Pin7 == HIGH)
{
digitalWrite (40,HIGH);
}
else
{
digitalWrite (40,LOW);
}
delay(100);
Serial.print(" Pin7= ");
Serial.print(Pin7);
Serial.print("\t");
Serial.print(" Pin40= ");
Serial.print(Pin40);
Serial.print("\n");
}

my surprise was when I switch on "Pin7" the led works ok but when switch off, the led remains ON during 3 seconds more without any INPUT.

What I do wrong?
Thanks in advance.

Carlos

First make your code easy to read and meaningful, so you don't confuse yourself as to what you THINK you're doing.

You have a variable called 'Pin40' and you assign it the value 0.

You then do a a digitalWrite(40,HIGH), turning Pin 40 NOT 'Pin40' (actually Pin 0) on.

Then in your serial output you go back to trying to display the state on your variable 'Pin40', which is actually Pin 0, a pin you never actually set state for!

Probably clearer to call your variables something less confusing, like signalInPin and signalOutPin.

Fix that first and see where your are :wink:

but when switch off, the led remains ON during 3 seconds more without any INPUT.

Sounds like a floating input.
How is the switch wired?

Thanks Tack I'm retyping the code.

int Pin40 = 40;

but the led wasn't collaborate :astonished: it's remain On during 3 secons without In signal on Pin52.

AWOL, I'm using a protoboard just to try. But now I'm wired directly on the Arduino. Just one more, if I let the Arduino on the table ( wood table ) and only the led wired on pin 40 ( well, a litle pin post insert on pin 7 )and put my finger on the pin poste, the led blink on and off during a little time, non repetitive blinking.

Perhaps a shield is needed?, and Why you recomended wired in?
I'm use the IOREF for Inputs

The Arduino be supplied with a standard 223vAC to 12vDC 1.5A (18w) conversor

Thanks in advance

Carlos

AWOL, I'm using a protoboard just to try.

No, I meant "What are the connections you have made between the switch, the Arduino, the supply rails and the pullup or pulldown resistors?"

Do you have your button wired similar to this?

Oh my god!. Thank you very much. WORKS PERFECT!!!! :blush: :blush: :blush: :blush:

You can close this post
Carlos

Maybe you haven't found the tutorials yet?

By the way, best thing to do when you are satisfied with the answers to a thread is edit your first post and add the word "SOLVED" at the front of the subject. Don't close the thread.....