Pages: [1]   Go Down
Author Topic: Code doesn't do it's job when I recycle power  (Read 482 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
    }
  }
}




Logged

Offline Offline
Sr. Member
****
Karma: 25
Posts: 499
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do you recycle power?!?

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What Arduino are you using?

The code looks fine so I'd suspect some kind of hardware problem.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26435
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

  pinMode(pirPin, INPUT);
  digitalWrite (pirPin, HIGH); // enable pullup resistor
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley)
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
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have any extra hardware connected to your UNO?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Sr. Member
****
Karma: 25
Posts: 499
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe try a pull-down resistor?
Logged

Offline Offline
Edison Member
*
Karma: 43
Posts: 1556
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Pete
Logged

Where are the Nick Gammons of yesteryear?

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 198
Posts: 12753
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Current limiting resistors?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 198
Posts: 12753
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This statement doesn't look quite right:
Code:
    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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: