please someone tell me? almost finished project.(problem solved)

i hope there is someone who can tell me why both these sketches don't work without computer.
as soon as i unplug the usb from my computer the sketches don't work.

/*
This program uses a normally open pushbutton to control whether an LED is on or off.
The important feature is that the LED will remain in a state of on or off unless the 
pushbutton is pushed.
Time delay debouncing was also integrated into the code to control unintended pushes.

Author: Ty McKnight, June 2010
*/



// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  4;      // relay

// Variables that will change:
int ledStatus = LOW;               // the state of the output pin
int buttonState = LOW;             // the accepted reading from the input pin
int lastButtonState = LOW;         // the button state after debounce filter
int lastButtonState2 = LOW;        // the previous reading from input pin
int buttonCount = 0;               // the number of times the button has been pressed


long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 100;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);   // view serial output to debug button counter
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // Check to see if you just pressed the button 
  // and you've waited long enough since the last press to ignore any noise: 
  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState2) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // Whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  }
  lastButtonState2 = reading;

  // If the accepted button state is different than the last button state,
  // increment the counter one time. Also print number of counts to serial output.
  if(buttonState != lastButtonState) {
     lastButtonState = reading;
     buttonCount++;
     Serial.print("Count: ");  // for debugging
     Serial.println(buttonCount, DEC);
  }
    
  // Since one button push equates to two counts, each time the count increments
  // twice, the state of the LED will change. 
  if(buttonCount % 4==0) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

rfid_lock-sketch.ino (19.1 KB)

How is the Arduino powered when not plugged into a computer?

+1 power

CrossRoads:
How is the Arduino powered when not plugged into a computer?

i got one aruino powered with a 9 volt adapter
and the other one using the usb cable in a usb adapter

Think you will find its your code, it does not appear to run on my Uno connnected to the pc or not.

Confirm your system is ok by running Examples Blink , then Button.

as soon as i unplug the usb from my computer the sketches don't work.

You have this line in it:-

Serial.begin(9600);   // view serial output to debug button counter

Which is fine without a computer if you have a Uno or a Megs, but can screw you up with a Leonardo or Micro.

Grumpy_Mike:
You have this line in it:-

Serial.begin(9600);   // view serial output to debug button counter

Which is fine without a computer if you have a Uno or a Megs, but can screw you up with a Leonardo or Micro.

i got 2 uno's i already tried it without this line aswell.

ricky101:
Think you will find its your code, it does not appear to run on my Uno connnected to the pc or not.

Confirm your system is ok by running Examples Blink , then Button.

what code did you run?
because they work fine for me when connected to my pc?

because they work fine for me when connected to my pc?

There is nothing in that code that is waiting for a response from an external unit. Therefore if it does not work when not connected to your PC then there is something else wrong because it should.

Most likely it is something to do with a miss wired interface not having a common ground that your PC incidentally supplies.

the problem was solved. it was a wiring mistake on my end.
since these 2 arduino's are in a master-slave config they had to share the same ground wire.
i had this in my first prototype on my desk but when installing i looked at the wrong pictures and i forgot about it.

either way thanks for the help everybody!
great community.