Hi Guys, I am a complete beginner to arduino programming, so thanks in advance for your help.
I'm trying to have my arduino keeping an LED ON as long as a signal is present at an input pin. This signal could be PPM, PWM, serial, whatever. It doesn't matter. I just want to keep the LED ON while the signal is there regardless of its polarity as well..
Then If the signal stopped for say 3 seconds the LED should turn OFF, and turn ON again if the signal comes back..
I know this is quite easy for some of you, but not for me.. I'm also searching and testing but didn't find anything yet.,. The closest I found was some code to turn OFF an audio amplifier when there is no sound for a while.. I just can't make it work as he uses stereo signal for this, and each channel of the audio output goes into 2 different analog pins. he doesn't use ground..
Do you want individual solutions for each type of "signal" or are you expecting to deal with the signal whatever type it is in the same program ?
You will need to define the signals much more precisely. Levels, frequency, type of serial input, baud rate etc. It might help if you shared with us the purpose of this project and how you envisage it being used.
Thanks for your answer. No, not individual signals. I mean, whatever signal is applied to the input. If there is any signal, no matter what it is, the output pin should stay ON. If there is no signal at all at the input for about 3 seconds, then the output should turn OFF. I don't need to read frequencies or anything..
as long as the state of the input is changing, the output should be ON. Stops changing, output OFF after 3 seconds..
Thank you very much.. As I said I'm a complete beginner but wanting to learn.
That is not good enough I am afraid. Let's suppose that you use one of the analogue input pins then what voltage should be considered as high enough to trigger the output ? What range of voltages do you intend to use ? Your OP mentions signals of either polarity, but without external hardware the Arduino can only deal with voltages above GND, not negative ones. You also mention serial input. What form would that take ?
Please share with us what you are doing so that we can provide help.
The only requirement about time, is that the output should turn OFF if there is no signal at the input for 3 seconds.
I have been reading about digitalRead(), but am not sure it will do what I'm looking for?
When a read is done, it could be that the pin is HIGH or LOW? I think what I need to detect is a change on the state of the pin?
Or am I going completely South here??
UKHeliBob:
That is not good enough I am afraid. Let's suppose that you use one of the analogue input pins then what voltage should be considered as high enough to trigger the output ? What range of voltages do you intend to use ? Your OP mentions signals of either polarity, but without external hardware the Arduino can only deal with voltages above GND, not negative ones. You also mention serial input. What form would that take ?
Please share with us what you are doing so that we can provide help.
Ok, sorry.. All voltages above ground..
Possible inputs are a PPM signal from a model aircraft transmitter, or serial output also from the same transmitter. Some RF modules accept serial communication. Therefore I said it could be different types of signals. I just want to switch the RF module power ON or OFF depending if there is a signal or not..
This is what I'm trying to achieve.
Possible inputs are a PPM signal from a model aircraft transmitter, or serial output also from the same transmitter. Some RF modules accept serial communication. Therefore I said it could be different types of signals.
PPM signals are usually read using pulseIn(), because it is the width of the pulse, not the fact that there is a pulse, that is important. Having an LED high while the PPM signal is high doesn't make sense.
Serial data is a series of very short pulses, representing the arrival of a bit. If you turn the pin on only while a bit is arriving (assuming that the bit is a one), and turn it off when the bit pulse ends, you won't even see the LED light up, since the duration is so short.
I think you really need to re-think your approach/requirements.
PaulS:
PPM signals are usually read using pulseIn(), because it is the width of the pulse, not the fact that there is a pulse, that is important. Having an LED high while the PPM signal is high doesn't make sense.
Serial data is a series of very short pulses, representing the arrival of a bit. If you turn the pin on only while a bit is arriving (assuming that the bit is a one), and turn it off when the bit pulse ends, you won't even see the LED light up, since the duration is so short.
I think you really need to re-think your approach/requirements.
Paul, thanks for your answer.. I think you still didn't understand (my fault probably) what I'm trying to achieve.. I did my little project a couple posts above.. It doesn't matter what frequency the signal is. I just want to detect if A signal is present at an input pin.. If so, turn an output pin ON.. It could even be audio as an example..
dlloyd:
Also take a look at StateChangeDetection ... you'll probably need to combine the two methods.
Do you want the output to go high when the input changes state? Then if the input is stable for 3 seconds, then the output goes low?
I want the output to be HIGH if the input keeps changing.. If the input doesn't change for 3 seconds then the output should go LOW. I guess it is what you said
By the way the debounce code didn't work.. The LED was going ON and OFF quite randomly, but mainly stayed ON, even when there was no signal at the input..
Had a little spare time ... you could try this. This acts as a retriggerable one-shot. When activity on the input stops (is stable) for 3 seconds, the output goes low. Otherwise, it will remain high.
const int inputPin = 2; // the pin that the pushbutton is attached to
const int outputPin = 13; // the output pin (LED)
unsigned long previousMillis = 0; // will store last time the output was updated
const long interval = 3000; // interval (milliseconds) to leave output HIGH (ON delay)
byte inputPreviousState = HIGH; // default startup condition to have output LOW
void setup() {
pinMode(inputPin, INPUT_PULLUP); // pushbutton is connected from inputPin to GND
pinMode(outputPin, OUTPUT); // set the digital pin as output
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) { // 3 seconds has elapsed
if (digitalRead(inputPin) == inputPreviousState) { // input is stable
digitalWrite(outputPin, LOW);
} else { // input has changed
digitalWrite(outputPin, HIGH);
previousMillis = currentMillis; // update the interval timer
inputPreviousState = digitalRead(inputPin); // update inputPreviousState
}
}
// any extra code can go here
}
dlloyd you absolutely nailed it.. Thank you so much.. Doing some research I had found out that this is the same as a retriggable monostable timer. And I built one on my breadboard. Then I saw your post, and I had to try your code.. It works, but,l the LED flickers sometimes when connected to the my RC transmitter outputting PPM on its trainer port. Every few seconds the led flickers once..
Thank you.
João
EDIT:Also, when I remove the signal sometimes the LED goes LOW immediately and some times takes a bit longer than 3 seconds..
const int inputPin = 2; // the pin that the pushbutton is attached to
const int outputPin = 13; // the output pin (LED)
unsigned long previousMillis = 0; // will store last time the output was updated
const long interval = 3000; // interval (milliseconds) to leave output HIGH (ON delay)
byte inputPreviousState = HIGH; // default startup condition to have output LOW
void setup() {
pinMode(inputPin, INPUT_PULLUP); // pushbutton is connected from inputPin to GND
pinMode(outputPin, OUTPUT); // set the digital pin as output
}
void loop() {
unsigned long currentMillis = millis();
byte inputState = digitalRead(inputPin);
if (inputState != inputPreviousState) { // input has changed
digitalWrite(outputPin, HIGH);
previousMillis = currentMillis; // reset timer
}
if (currentMillis - previousMillis >= interval) { // 3 seconds has elapsed
if (inputState == inputPreviousState) { // input is stable
digitalWrite(outputPin, LOW);
}
inputPreviousState = inputState; // update inputPreviousState
}
// any extra code can go here
}
Thanks.. Ok, this one doesn't make the LED flicker, but after turn ON, it doesn't turn OFF anymore if I remove the signal. Only if I disconnect the plug completely. If you use it with a push button it works fine, but not with the radio. the previous version at least reacted to the radio..
The thing is, this is looking for a connection to ground. Maybe it should detect when the pin goes HIGH?
I am connecting the PPM signal through a 1K resistor to Pin 2 of the arduino.. No resistors to ground.. Tried that and it didn't work.. tried a transistor and it didn't seem to work as well..