arduino stops working when I turn on or off something else!

ey people!

I have found myself face to face with a funny and disturbing behaviour.

I am using an arduino mega 2560 conected via usb to the computer. I am detecting voltages variations in several photoresistors and triggering midi notes via serial. It happens VERY OFTEN that when I turn on or off an artefact, something completely unrelated to the project besides the fact that uses electricity, the arduino stops doing what it was doing. To make it run again, I have to disconnect the usb and load the sketch again.

It is very important to say that the computer that I am using is running without a battery. I tried it with my main computer (which has a battery) trying to recreate the behaviour, and it doesnt fail. it works all the time.

I also attached a 9v battery to see if somehow this would affect, but it continues failing.

What could be going on and is there a way to prevent this to happen?

It is not unusual that interference from switching things on an off affect an arduino.

It is very important to say that the computer that I am using is running without a battery.

I think this is giving us a clue as to what is going wrong. However I don't understand exactly what you mean by this.

I also attached a 9v battery to see if somehow this would affect,

Where did you attach this battery? What sort of 9V battery was it? The small square ones are mainly useless.

There are two possible causes.

  1. Interference is being picked up on the long leads of your sensors. Remove all external components and see if you can get it to fail.
  2. Interference is disturbing your computer's power supply to the Arduino.

It is not possible that the Arduino actually stops, try and narrow it down as to what fails by using LEDs.

ey mike!

I think this is giving us a clue as to what is going wrong. However I don't understand exactly what you mean by this.

I mean that the computer I am usign has no battery, and I am implying that perhaps slight changes of voltage or current or power in the main electricity rails are being transfered to the arduino board provoquing its failure.

Where did you attach this battery? What sort of 9V battery was it? The small square ones are mainly useless.

I attached it to the arduino power supply connector. it was a square small one

Remove all external components and see if you can get it to fail.

I am only able to see if the sketch is working by interacting with the photoresistors (i think), so I wouldnt know hot to test it without external components.

It is not possible that the Arduino actually stops, try and narrow it down as to what fails by using LEDs.

The arduino remains on and there is nothing externally visible happening when the failure occurs, but evidently something happens to the sketch. could you please guide me in the process of recreating the failure by using LEDs? I dont know what to do

I mean that the computer I am usign has no battery, and I am implying that perhaps slight changes of voltage or current or power in the main electricity rails are being transfered to the arduino board provoquing its failure.

So I assume that this computer is a laptop? I can't see why this would make a difference. Even if the external supply did vary these are normally at 18V or so. There is a lot of regulation down to 5V for the USB so I would not expect that to be reflected in the 5V supply.

I am only able to see if the sketch is working by interacting with the photoresistors (i think), so I wouldnt know hot to test it without external components.

I meant load something else into it like the blinking LED sketch does this fail?

could you please guide me in the process of recreating the failure by using LEDs?

You can add extra lines of code to turn on and off an LED at certain points in the sketch. If you see the LED flash on and off then you know that code is being passed through. In that way you can work your way through the sketch and see where it is failing.
For example every time round the loop() function you can toggle (invert ) the state of the LED. Remember this is normally fast so it should look like the LED is on all the time at half brightness. If the LED suddnley gets brighter or goes out you know the code has stalled in the loop.

When ever a beginner thinks the Arduino has stopped it always turns out not to be the case.