yea, so i'm trying to add a button to my project to switch the relay on and off when i press it.
But it only works when i hold the button down...
Anyone can help me?
int inputPin1 = 5;
boolean inputPin = true;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(2, OUTPUT);
pinMode(inputPin1, INPUT_PULLUP);
}
// the loop function runs over and over again forever
void loop() {
inputPin=inputPin;
if (digitalRead(inputPin1) == false) {
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(1000);
// wait for a second
}
inputPin=!inputPin;
if (digitalRead(inputPin1) == true) {
digitalWrite(2, LOW);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}
}
FSstudios1:
Hi! They are the exact same, except the maximum current the 3904 is 200mAh, as the 2n2222 is 1 amp
kinda like they are identical twins in every way, except they do not look alike ?
3904 - 2222
c/e Volt 40 - 50
c/b volt 60 - 75
they are completely different in ratings.
but, for the application, the coil current is the main selection criteria. if your coil current is not over the rating for your chips, you should be good to go.
I used two, because i have two states with the relay( NC - NO). I want to make the relay stop clicking (in the mean time my leds will stop flashing) and when i press again it runs back on, and my leds start back blinking
So i think you want to push a button attached to pin 5.
One push the led on pin 2 turns on and stays on.
Another push the led goes off and stays off.
Then this repeats.
you will have to change the input from pin 2 to pin 5 and the output from pin 13 to pin 2
/* switch
*
* Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
* press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
* a minimum delay between toggles to debounce the circuit (i.e. to ignore
* noise).
*
* David A. Mellis
* 21 November 2006
*/
int inPin = 2; // the number of the input pin
int outPin = 13; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
pinMode(inPin, INPUT_PULLUP);
pinMode(outPin, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(outPin, state);
previous = reading;
}
int inPin = 2; // switch is connected to this pin
int outPin = 13; // led is connected to this pin
byte reading; // the current reading from the input pin
byte flag = LOW; // if LOW the switch was pushed, if HIGH the switch was released
unsigned long time = 0; //
unsigned long debounce = 200UL; // the debounce time
void setup()
{
pinMode(inPin, INPUT_PULLUP); //the N.O. switch is connected to GND
pinMode(outPin, OUTPUT);
digitalWrite(outPin, LOW);
time = millis();
flag = LOW;
} // END of setup()
void loop()
{
reading = digitalRead(inPin);
//have we pushed the switch
if (reading == LOW && flag == HIGH && (millis() - time > debounce))
{
time = millis(); //get ready for the next iteration
//toggle the LED
digitalWrite(outPin, !digitalRead(outPin));
//show that the switch was pushed
flag = LOW;
}
//have we let go of the switch
if (reading == HIGH && flag == LOW && millis() - time > debounce)
{
time = millis(); //get ready for the next iteration
//show that the switch was let go
flag = HIGH;
}
} // END of loop()