Go Down

Topic: Code doesn't do it's job when I recycle power (Read 683 times) previous topic - next topic

ferkheusa

I hope someone could help with this issue. when I upload the code to arduino, it works perfect: Turning 6 LEDs on and off when a pir is triggered but the issue is: once I unplug the USB and put it back again the circuit doesn't do anything so I need to upload the code again for it to work.
What could be the problem: Is it the code or the arduino resetting itself or what?
I had this issue before, but with other project it worked fine.
Here is my code:
Code: [Select]
int DL= 20;
int val = 0;
int pirPin = 8;
void setup() {
  for(int pinled =2; pinled <=7; pinled=pinled + 1)
    pinMode(pinled, OUTPUT);
  pinMode(pirPin, INPUT);
}
void loop()
{
  val = digitalRead(pirPin);

  if(HIGH == val){
    for(int pinled=2; pinled<=7; pinled ++)
    {
      digitalWrite(pinled, HIGH);
      delay(DL);
      digitalWrite(pinled,LOW);
    }
    for(int pinled=6; pinled>=2; pinled --)
    {
      digitalWrite(pinled, HIGH);
      delay(DL);
      digitalWrite(pinled,LOW);
    }
  }
}





TanHadron

How do you recycle power?!?

The code looks OK to me.  Maybe there is a hardware issue?  How is the circuit hooked up?

johnwasser

What Arduino are you using?

The code looks fine so I'd suspect some kind of hardware problem.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

PeterH

The most obvious reason for it not to do anything is that a digitalRead of pirPin is not returning HIGH.

The Serial port is invaluable as a tool to help debug problems like this. If you put Serial.begin(9600) you can then use Serial.println() elsewhere in your code to tell you what code is executing and what values your variables have. Look at the example sketches with serial output to see how this can be used.
I only provide help via the forum - please do not contact me for private consultancy.

CrossRoads

PIRs often have open collector or open drain outputs.
Try enabling the internal pullup:

  pinMode(pirPin, INPUT);
  digitalWrite (pirPin, HIGH); // enable pullup resistor
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

ferkheusa

I am using an Arduino Uno.
When I upload the code the LEDs work as they are supposed to (when I come close to the pir. the pir pin turn high and LEDs start blinking. No problem :))
The problem is when I unplug the USB from the laptop and putting it back ; I need to upload the code again and make sure no one unplugs the USB. otherwise nothing works

zoomkat

Do you have any extra hardware connected to your UNO?
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

ferkheusa

The PIR and the LEDs and wires that's it.


el_supremo

I can't see why it doesn't start up when USB is plugged in but this statement doesn't look quite right:
Code: [Select]
    for(int pinled=6; pinled>=2; pinled --)

I think the initialization should be "int pinled=7;"

Pete

Coding Badly


ferkheusa

what you mean current limiting resistor? I don't have that. You mean for each LED I need to put a current limiting resistor in the negative side and tie it to ground. Right?

Coding Badly

You mean for each LED I need to put a current limiting resistor in the negative side and tie it to ground. Right?


Either side of the LED.

Quote
what you mean current limiting resistor? I don't have that.


You have to have something in the circuit to limit the current passing through the LEDs.  (Well, you don't "have to" but the side-effect is that there is going to be permanent damage; most likely to the processor.)  The simplest "something" is a resistor.

johnwasser


This statement doesn't look quite right:
Code: [Select]
    for(int pinled=6; pinled>=2; pinled --)
I think the initialization should be "int pinled=7;"


Only if he wants the 7 LED to blink both on the way up and on the way down.  I think 6 is reasonable.  Might even want to stop at 3 so the LED on 2 doesn't blink twice.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up