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)