simple program but difficult problem need help

good morning every one,working with simple sketch as given:my board is freeduinolite v2.0 and system windows xp,fdti usb adaptor .
code:

int led8Pin = 8;
int led11Pin = 11;
int led0Pin = 13;
int switchPin1= 3;
int switchPin2= 7;

int val1;
int val2;

void setup() {
Serial.begin(9600);
pinMode(switchPin1, INPUT);
digitalWrite(switchPin1, HIGH);
pinMode(switchPin2, INPUT);
digitalWrite(switchPin2, HIGH);
pinMode(led0Pin, OUTPUT);
pinMode(led8Pin, OUTPUT);
pinMode(led11Pin, OUTPUT);
digitalWrite(led8Pin, LOW);

}

void loop()

{
val1 = digitalRead(switchPin1);
delay(50);
if (val1 == HIGH) {

digitalWrite(led8Pin, HIGH);
digitalWrite(led0Pin, HIGH);
delay(1000);
digitalWrite(led0Pin, LOW);
delay(1000);
digitalWrite(led11Pin, HIGH);
delay(2000);
digitalWrite(led11Pin, LOW);
delay(18000);
digitalWrite(led8Pin, LOW);

}

val2 = digitalRead(switchPin2);
if (val2 == LOW) {
digitalWrite(led11Pin, HIGH);
delay(800);
digitalWrite(led11Pin, LOW);
delay(2000);
}
}

problem with above code is uploads perfectly and runs correctly by external power supply but at times when switched on supply pin 13 LED goes ON further program hangs,program not runs stop responding,then again have to switched OFF the external supply wait few seconds again switch ON then works smoothly,for this changed the program many times but of no use as unable to understand the reason please can anyone help is this a code problem or hardware problem.

I don't know what else is connected, but could you try using pin 2,3,4,5 and 6 or something?

Pin 13 is also used for the SCK and pin 11 for MOSI in some library's it could be in use.

I have no arduino here for testing but you can try this.

Is your supply the correct voltage? and What voltage?

actually it is a 9 volts adoptor printed on adoptor but the correct voltage is 12.50 volts DC

@ kenny_-_ sorry for late response,I had used 2,3,4,5and 6 pins result is same some times program hangs and some times 13 pin LED glow continuously

trail and error method works some times, but I want to know is it correct,actually my code was running correctly by cable but when connected to external power supply no response from it,hence by nett searching it was fouNd that to connect 10kohm resistence to ground form RX pin,when connected it worked where as some times code hangs when switched ON further connected a loop to RX AND TX, now code is running correctly by external power supply,is the thing is correct need help

Please post your circuit or describe the way you have the leds connected. - Scotty

Dear scottyjr
Please find the circuit image attachment,waiting for your kind reply

hello scotty where are you,waiting for reply

Do I understand it correctly that the code and circuit function correctly when using the USB supply but fails when you use an external supply? - Scotty

hello scotty goob evening actually the said code runs correctly by usb but it was not running by external power supply as per arduino trouble shooting blog suggested to connect 10k resistor RX pin to negative (ground),further connecting RX pin to ground the code runs correctly by external power supply but some times when switched ON external power the said code hangs,unable to find the reason still trying by trail and error method but no result need help

I uploaded your program to a atmega328p with a led on pin 13 with resistor and the arduino bootloader but mine doesn't hang even after 30 reboots.

i don't have all the parts you use, but i can't seem to run into the same problems as you.

Could you try to run your arduino with less hardware? I would think it's hardware related, because i can't get mine to hang with your program.

Your code is just very simple and i couldn't imagine something being wrong with it, but still wanted to test it

Thanks Kenny I am using atmega8, freeduinolite v2.0 board,yesterday uploaded the same code by clicking upload icon on compiler were as previously trails uploaded code by pressing RESET button on board then upload icon on compiler,hoping this may be the problem is this correct.Yet not tested on external power source ,lets see what happens.

Do you mean pressing reset after pressing upload? I usually do that when using a USB-ttl because then you don't need a hardware reset. This was what I did this morning. I don't own a atmega 8 but this code should just run on anything. If you think that the ttl adapter is the problem then you could always use a programmer like the heap avr tinyisp , the arduino will boot faster and you have some additional program space. :slight_smile:

I mean to reset first then upload as I am doing previously but today I tried a different thing, that is to upload the code directly without pressing reset it's working but I want to know the correct procedure,thanks for your patience and interest to reply the post, thankyou so much.

If it's uploading, it's uploading. Just don''t press reset.

The reset is for when it is not connected (like on a breadboard or something sometimes) then it can be a little tricky with the timing.

If the reset is not being enabled the program will not upload, so yours is okay on that front! :slight_smile: