Problem with pin 13 Arduino mega 2560

Hello guys, i am having a problem with the PIN 13, the led that most of arduinos have, i made a code that by pushing a button it will turn on or turn off a led, for the led i used the DIGITAL PIN 22 and for the button the DIGITAL PIN 23. the code works fine but after a while the led (PIN 22) turns off and the led (PIN 13) turns on by 2 seconds and turn off that situation happens time after time. I dont know why happen that...

This is the code that i'm using:

int led=22 , pulsador=23 , valor_pulsador, led_2=13;
void setup(){
pinMode(led,OUTPUT);
pinMode(pulsador,INPUT);
pinMode(led_2,OUTPUT);
}
void loop(){
digitalWrite(led_2,LOW);
valor_pulsador=digitalRead(pulsador);
if(valor_pulsador==HIGH){
digitalWrite(led,HIGH);
}
else
{
digitalWrite(led,LOW);
}
}

By the way that problem only happens when i use as a voltage source the arduino's USB cable, when i use a as voltage source a battery it doesn't happen.

I tried it with several codes (ADC,serial communication etc.) and the problem keeps happening, i don't know if it's the arduino's USB cable o arduino itself.

Try changing these 2 lines:
pinMode(pulsador,INPUT);
to
pinMode(pulsador,INPUT_PULLUP); // enable internal pullup resistor

and
if(valor_pulsador==HIGH){
to
if(valor_pulsador==LOW){
and wire the button to connect the pin to Gnd when pressed.

Then the pin will read as a solid HIGH when not pressed, and a solid LOW when pressed, and not float at some intermediate state in the meantime.

I did what you said but didn't work, i just realize that regardless the code a i use, the led (pin 13) keeps turning on and turning off some times by itself

You are not telling 13 to turn on/off tho - you are telling 22 to turn on/off:

int led=22
digitalWrite(led,HIGH);
digitalWrite(led,LOW);

If you want 13, change those to use led_2=13:
digitalWrite(led_2,HIGH);
digitalWrite(led_2,LOW);

I dont want the led(PIN 13) turn on,then i wrote in the code:
int led = 13;

pinMode(led,OUTPUT);
digitalWrite(led,LOW);

As i said that happen in every code, for example with the serial communication, every 30 seconds, the communication stops, the led (pin 13) turn on and turn off four times and them return to the serial communications, that happen with the ADC, PWM, etc. with every code i have wrote happens, i think the problem might be the arduino or arduinos USB cable.

Sounds like your board is resetting then. The flash would be the bootloader code starting up.

And can be fixed? or do i have to change my arduino board?

cr93:
And can be fixed? or do i have to change my arduino board?

If the cause is constant resets then it may be an electrical problem with what you may have wired up to the arduino board's pins?

Lefty

Well after reading a lot i FINALLY fixed it, the problem was that the arduino was auto - reseting by itself. To fixed i putted a 10 uF capacitor between the pins reset and gnd and works perfectly, however i really don't like how it looks the capacitor on the board, because it has to be always connected on the two pins that i mentioned before, therefore i read that if you cut the trace labbeled "RESET - EN" on the arduino's board the auto - reset will be disabled.

Cutting RESET-EN stops the PC from creating a Reset during a sketch download, or when opening the serial monitor. The board shouldn't normally be autoretting at other times.

Yes you are right, i realized it when i tried to download a sketch to arduino's board and i couldn't do it because the arduino needs to reset when a new sketch is going to be downloaded. Well right now i have to put the capacitor when i am testing physically, and i have to remove it when i download a new sketch, it's annoying do it everytime i change something in the sketch. But so far it's the only way to prevent the arduino reset by itself.

I want to try with other arduino's USB cable, because that problem only happen when the USB cable is connected to arduino's board, when i use a voltage source (battery) and the USB cable is disconnected, the board works fine. But when i need to send/recive data from/to my PC i need the USB cable and that's when the arduino reset by itself and i lost part of the information that i was sending / receiving.