arduino misbehaviour when disconected from pc

Hello,

I made a simple sketch for reading values from an analog input (mic) and it changes the brightness of some LEDs accordingly (on PWM output)... Everything seems to be alright while i run the arduino over USB from my pc... problem starts when i try running it from ext power w/o pc.

I use a standard 5V power supply with a USB connector (similar to a phone charger). When i turn on the power the sketch runs alright for a minute or so, but after that as if it gets overcrowded.. it starts misbehaving... So far I tried a lot of different things to try and fix that (like powering over VIN, 5V pins, commenting out serial parts of the code etc), but the problem doesn't go away.. If someone knows a solution, it would be of great help

Here is the code (don't laugh, I'm a machinist, not programmer):

const int analogPin = A0;
const int ledPin = 10;
int mic = 0, led = 0;
void setup() {
pinMode(ledPin, OUTPUT); // this doesn't do anything... i just tried declaring it to see
pinMode(A0, INPUT); //<--- if it would make a difference
digitalWrite(ledPin, LOW);
Serial.begin(9600);
}

void loop() {
mic = 0;
mic = analogRead(analogPin);
Serial.println(mic);
Serial.print("\t");
Serial.println(led);
led = 0;
delay(1);
led = mic - 586; //these numbers give the right values with the hardware i use
if (led < 4) {
led = 0;
}
analogWrite(ledPin, led * 8);
delay(1);

}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup (the smiley face in your code above for example), leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

sorry... not too often here

(repost with code tags)

Hello,

I made a simple sketch for reading values from an analog input (mic) and it changes the brightness of some LEDs accordingly (on PWM output)... Everything seems to be alright while i run the arduino over USB from my pc... problem starts when i try running it from ext power w/o pc.

I use a standard 5V power supply with a USB connector (similar to a phone charger). When i turn on the power the sketch runs alright for a minute or so, but after that as if it gets overcrowded.. it starts misbehaving... So far I tried a lot of different things to try and fix that (like powering over VIN, 5V pins, commenting out serial parts of the code etc), but the problem doesn't go away.. If someone knows a solution, it would be of great help

Here is the code (don't laugh, I'm a machinist, not programmer):

const int analogPin = A0;
const int ledPin = 10;
int mic = 0, led = 0;
void setup() {
  pinMode(ledPin, OUTPUT);        //         this doesn't do anything... i just tried declaring it to see
  pinMode(A0, INPUT);               //<---   if it would make a difference
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);
}

void loop() {
  mic = 0;
  mic = analogRead(analogPin);
  Serial.println(mic);
  Serial.print("\t");
  Serial.println(led);
  led = 0;
  delay(1);
  led = mic - 586;              //these numbers give the right values with the hardware i use
  if (led < 4) {
    led = 0;
  }
  analogWrite(ledPin, led * 8);
  delay(1);


}

(deleted)

Thanks spycatcher2k... I will try it as soon as I'm home

the power supply is a universal power brick (adjustable 3, 4.5, 5 6, 7, 9, 12, 13.5, 15 volts, with up to 1.5 amps), it has a universal dc output connector (different size connectors can be connected one of witch is USB shaped), last time I was running it, it was connected into the USB plug of the arduino (adjusted to 5V)

If you are plugged into the barrel jack, 5 volts is too low, try it with 7 or 9 volts.

No, even though I tried that as well, Im connected over the USB plug of the arduino

spycatcher2k.. just noticed your code is missing a line... (whitch is not important for your good point, but still)

led = mic/4;

after the line

mic = analogRead(analogPin) - Adj;

(deleted)

(deleted)

What exactly is it misbehaving here?

Analog in gives a value from 0 to 1023. After adjustment roughly -600 to 400. Negatives are discarded. Divided with 4 and later multiplied with 8. My guess is that led will light with full brightness ost of the time.