Doorbell receiver module had it's speaker removed, does not produce sound. Instead, the signal generated is read through A0 and is used to turn a LED block ON for a few minutes.
It´s working as a wireless lamp, so I can light my way to the bathroom at night.
When the powerbank is turned on, the arduino runs at 5V, the LED block is connected to 5V and the doorbell receiver is running at 3.3V. The output of the audio chip is being listened through A0 and, when I press the transmitter, the audio chip output goes higher, which turns the LED block on through D3 by PWM. I would like to:
Press one time, LEDs turn on for OnTime=2min
Press again immediately and OnTime becomes Ontime=4min
Press again, etc...
I go to the bathroom, come back and it's still on? I decide to press again and D3 goes LOW.
I wrote this simple code using delays to test the overall setup. I guess I have to use milis() or a Timer library but for that I need your help.
/* Wireless doorbell modified to turn ON a block of LEDs instead of playing a melody.
When the switch is pressed on the transmitter, a voltage change occurs in the audio
chip output of the receiver, which corresponds to the playing of the melody. This voltage
change is read through and analog pin. When the voltage rises over a defined threshold,
pin D3 outputs a PWM signal for a certain interval of time. The base of a 3904 transistor
is connected to D3 through a 1K Ohm resistor; the collector is connected to the load and the emitter do GND.*/
const int analogPin = A0; // A0 connected to audio chip output
const int ledPin = 3; // D3 to resistor to transistor base to trigger LED block
const int threshold = 650; // an arbitrary threshold level that's in the range of the analog input
unsigned long interval = 2 * 60 * 1000UL; //2 minutes ON time
void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
// Serial.begin(9600); // initialize serial communications:
}
void loop() {
int analogValue = analogRead(analogPin);
//Serial.println(analogValue); // print the analog value:
//delay(10); // delay in between reads for stability
/*When the switch in pressed, analogValue rises over threshold, it's time to turn LED ON for interval */
if (analogValue > threshold) {
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(10);
}
delay(interval);
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(10);
}
}
}
- Press one time, LEDs turn on for OnTime=2min
- Press again immediately and OnTime becomes Ontime=4min
- Press again, etc...
- I go to the bathroom, come back and it's still on? I decide to press again and D3 goes LOW.
So each time you press a button on the remote, increment the time until the lights go off by two minutes, unless you are done going to the bathroom, in which case pressing the button should turn the lights off.
Sounds good, as long as the Arduino has some way of knowing when you are done going to the bathroom. Care to explain how it will know THAT? To yourself. I really don't want to know those details.
I'd say allow a time window of maybe 20 seconds after the first press where each subsequent press adds 2 minutes to the ON time. Or, maybe extend the window for 20 seconds after each press. Either way, after the window closes, the next press turns OFF the light.
Press again immediately and OnTime becomes Ontime=4min
Press again, etc...
I go to the bathroom, come back and it's still on? I decide to press again and D3 goes LOW.
So each time you press a button on the remote, increment the time until the lights go off by two minutes, unless you are done going to the bathroom, in which case pressing the button should turn the lights off.
Sounds good, as long as the Arduino has some way of knowing when you are done going to the bathroom. Care to explain how it will know THAT? To yourself. I really don't want to know those details.
I really dont understand how can someone with 70k posts can make and observation like this. gfvalvo got it spot on.
I will post the code here once it is finished. I still need to get my head around using millis() but code structure is clear.
PaulS:
gfalvo made a guess. I don't like guessing games. I prefer that YOU define your requirements VERY CLEARLY.
Gfalvo did not make a guess, he showed a particular ability to make an accurate interpretation of a help request posted by a beginner. I'm sorry you did not have a pleasant experience. However I would ask you to leave the thread instead of contributing with idiosyncratic considerations.
This is a working sketch to toggle the LEDs on and off based on state-change and millis() code. The millis() section adds 5 seconds between it's possible to toggle the LEDs in order to let the audio chip output stabilize first (otherwise it would give false inputs, a sort of switch debouncing).
In theory, this code will allow (or is a basis) to modify any wireless doorbell to turn a Load on and off.
/*Wireless doorbell modified to turn ON a block of LEDs instead of playing a melody.
When the button is pressed on the transmitter, a voltage change occurs in the audio
chip output in the receiver, which corresponds to the playing of the melody. This voltage
change is read through and analog pin. When the voltage rises to it's max, pin D3 becomes HIGH until the button is pressed again.
The base of a 3904 transistor is connected to D3 through a 1K Ohm resistor; the collector is connected to the load and the emitter do GND.
The sketch uses parts of code from Arduino IDE Examples and from Nick Gammon tutorials*/
const unsigned long LEDinterval = 5000;
unsigned long LEDtimer;
// A0 connected to audio chip output
const int ANALOGPIN = A0;
// D3 to resistor to transistor base, triggers LED block
const int LEDPIN = 3;
// During power-up, audio chip output fluctuates a little. Starting maxchipoutput at 500 ensures the LEDs aren't accidentaly turned ON before a button press
int maxchipoutput = 600;
void setup() {
// initialize serial communications:
// Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
LEDtimer = millis ();
}
void toggleLED () {
if (digitalRead (LEDPIN) == 0) {
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
analogWrite(LEDPIN, fadeValue);
delay(20);
}
}
else {
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
analogWrite(LEDPIN, fadeValue);
delay(20);
}
}
// remember when we toggled it
LEDtimer = millis ();
}
void loop() {
int chipoutput = analogRead(ANALOGPIN);
if (chipoutput >= maxchipoutput) {
// maxchipoutput stores highest voltage when switch is pressed
maxchipoutput = chipoutput;
}
else {
maxchipoutput = 500;
}
// LED block lits when swith is pressed, melody plays, voltage raises to or over maxchipoutput
if (chipoutput >= maxchipoutput) {
//wait 5 seconds between toggles to avoid the fluctuating chip output while melody plays
if ( (millis () - LEDtimer) >= LEDinterval)
toggleLED ();
}
// Serial.print(maxchipoutput);
// Serial.print(" ");
//
// Serial.println(chipoutput);
// delay(1);
}
FranciscoB:
In theory, this code will allow (or is a basis) to modify any wireless doorbell to turn a Load on and off.
Yes. I am looking forward to the parts about your original problem, lighting the way to the bathroom in extendable increments.
It also seems that you are still relying on delay() calls, so I look forward to how you work that part out.
And as far as "immediately", you will have to train users just how fast that doorbell button can be pressed to get over the odd signal from the doorbell your theoretical code seems to imply.
The flashlight solution is looking better and better. And there are real RF controlled switches… as admirable a hack as this is.
Development of this project ended because I found the wireless signal to not be powerful enough to penetrate the walls between the rooms. It worked regularly while keeping the same distance without obstacles. I will just use one of the 433mhz modules from ebay with proper antennas..