Hello,
I just started learning Arduino. I would like to make a door bell for my grandfather and grandma. They both are old and have problems with vision and hearing. I would like to make a bell for them to alert them when the front door opens. I would like to make a bell so that it will do the following. When the door opens, the light flashes for 30 seconds, at the same time, the bell rings for 10 seconds. The switch on the door must act as a pulse, since the grandfather sometimes sits on the porch and then the door is open. Therefore, it is not necessary for the bell and the light to be lit all the time, but only when the door opens.
I took the code from the arduino and made changes. When the switch is turned on the light comes on and the bell rings for 20 seconds. I'm new with arduino and and I do not know how to solve this problem. Since I would like to make a bell as soon as possible for my grandfather and grandmother, I ask you for help. My question is:
How to make the switch on ardino to act as a pulse
How to make the light and the bell light at the same time (the light is on for 30 seconds, the bell rings for 10 sec.
To better understand, I apply the scheme and the code.
I thank you for all the help
/*
Signaling for the open door. when the door opens, the light flashes and the bell rings.
*/
// constants won't change. They're used here to set pin numbers:
const int switchPin = 12; // switch door open
const int ledPin4 = 4; // light indication door open
const int beepPin5 = 5; // beep indication door open
// variables will change:
int switchState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the pins as an output:
pinMode(ledPin4, OUTPUT);
pinMode(beepPin5, OUTPUT);
// initialize the switch pin as an input:
pinMode(switchPin, INPUT);
}
void loop() {
// read the state of the switch value:
switchState = digitalRead(switchPin);
// check if the switch is pressed. If the switchState is HIGH:
if (switchState == HIGH) {
// turn LED on:
digitalWrite(ledPin4, HIGH);
digitalWrite(beepPin5, HIGH);
delay(10000);
} else {
// turn pins off:
digitalWrite(ledPin4, LOW);
digitalWrite(beepPin5, LOW);
}
}
How to make the switch on ardino to act as a pulse
Detect when the switch becomes pressed rather than when it is pressed.
See the StateChangeDetection example in the IDE
How to make the light and the bell light at the same time (the light is on for 30 seconds, the bell rings for 10 sec.
Use millis() for timing.
Save the value of millis() when the switch becomes pressed and turn on the bell.
Each time through loop() check whether the required period has elapsed and if so turn off the bell
To make the light flash when required do the same thing. Start flashing when the switch is closed and set a boolean variable to true
Each time through loop() whilst the variable is true change the state of the light if the required flash period has elapsed. When the total flashing period has elapsed set the variable to false to stop the flashing.
Hello,
I tried to edit the code for StateChangeDetection, but unfortunately, I do not understand enough programming for arduino and I have no success. Maybe someone already wrote a piece of code to get a pulse when the switch is concluded? Or, if anyone can fix the StateChangeDetection code to fit my bell (for inpulz only).
/*
State change detection (edge detection)
Often, you don't need to know the state of a digital input all the time, but
you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.
This example shows how to detect when a button or button changes from off to on
and on to off.
The circuit:
- pushbutton attached to pin 2 from +5V
- 10 kilohm resistor attached to pin 2 from ground
- LED attached from pin 13 to ground (or use the built-in LED on most
Arduino boards)
created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/
// this constant won't change:
const int buttonPin = 12; // the pin that the pushbutton is attached to
const int ledPin4 = 4; // the pin that the LED is attached to
const int ledPin5 = 5; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
pinMode(buttonPin, INPUT); // initialize the button pin as a input:
pinMode(ledPin4, OUTPUT); // initialize the LED as an output:
pinMode(ledPin5, OUTPUT); // initialize the LED as an output:
Serial.begin(9600); // initialize serial communication:
}
void loop() {
buttonState = digitalRead(buttonPin); // read the pushbutton input pin
if (buttonState != lastButtonState) // compare the buttonState to its previous state
{
if (buttonState == HIGH) // if the state has changed, increment the counter
{
buttonPushCounter++; // if the current state is HIGH then the button went from off to on:
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else
{
Serial.println("off"); // if the current state is LOW then the button went from on to off:
}
delay(50); // Delay a little bit to avoid bouncing
}
lastButtonState = buttonState; // save the current state as the last state, for next time through the loop
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);
}
else
{
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
}
}
The code you posted does something. You expect it to do something. You haven't told us what either thing is, or even whether or not they are the same thing (though I suspect that they are not).
What does the code actually do? How is the switch wired?
pinMode(buttonPin, INPUT); // initialize the button pin as a input:Is there anything keeping the input pin LOW when the pushbutton is not pressed ? How is the pushbutton wired ?
The switch is a microswitch with a lever and is mounted on the front door. At the switch, I use NC (normaly closed) contact. When the door is closed, the switch is pressed but has no contact. When the door is open the switch is no longer pressed and the contact is closed. At that time, Arduino receives a command to light the light and the bell.
I would like to create a program so that when the switch is closed, the bell will sound for 10 seconds and the light will light for 30 seconds. Then it goes out and repeats when the doors are opened again and the switch is closed.
The program will start only when state on the switch is changed from low to high and after than wait until the switch is not going again from low to high.
About the code that I wrote. I took an example from the arduino and tried to edit it for my needs. Since I'm about to start programming, there may also be some things in the code that might not belong there.
The switch is a microswitch with a lever and is mounted on the front door.
Irrelevant. Besides, you didn't tell us what color it was or what size mounting holes it has.
At the switch, I use NC (normaly closed) contact.
For what? To hang the dog's leash on?
Draw a picture showing how the switch is wired.
It is far simpler to connect one leg (the NC one or the NO one; doesn't really matter) to the digital pin and the other one (the common leg) to ground. Then, use INPUT_PULLUP as the mode. The pressed value may be HIGH or LOW, depending on whether you used the NC leg or the NO leg. Experiment to determine which.
Forget that sketch for now. Write a new one, based on the state change detect example. Do nothing more than print "Pressed" whenever the switch becomes pressed, and print "Released" when the switch becomes released.
If the output doesn't match what you do with the switch, is reversed, or appears when you don't do anything, you have a hardware problem. We need to rule that out FIRST.