Go Down

Topic: Read signal presence and switch output. (Read 2547 times) previous topic - next topic

dlloyd

#15
Feb 08, 2016, 07:33 pm Last Edit: Feb 08, 2016, 07:39 pm by dlloyd
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.

Code: [Select]
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
}

jhsa

#16
Feb 08, 2016, 10:23 pm Last Edit: Feb 08, 2016, 10:54 pm by jhsa
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..

dlloyd

Corrected:

Code: [Select]
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
}

jhsa

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?

Thanks

João

jhsa

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..

João

dlloyd

#20
Feb 09, 2016, 12:52 pm Last Edit: Feb 09, 2016, 12:57 pm by dlloyd
This will provide a stronger sensing signal that is pulled down:

Replace  pinMode(inputPin, INPUT_PULLUP);
with     pinMode(inputPin, INPUT);

Connect 10K from inputPin to GND

EDIT: To test with pushbutton, connect the pushbutton from 5V to inputPin (or 3.3V if you have a 3.3V Arduino board).

jhsa

#21
Feb 09, 2016, 01:14 pm Last Edit: Feb 09, 2016, 01:27 pm by jhsa
Now LED is always ON. Even if signal wire is not connected to the pin, and whether I connect or not pin 2 to ground through the resistor.. I'm using a 22K resistor, but that shouldn't be a problem?

Thank you

João

What's wrong With this forum? I can't even edit my post within 5 minutes of having posted..  :(

dlloyd

#22
Feb 09, 2016, 01:36 pm Last Edit: Feb 09, 2016, 11:31 pm by dlloyd
Had to move the inputPreviousState update down one line. Here's the code for when the input is pulled down:

Code: (please share) [Select]
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);         // pushbutton from inputPin to VCC, 10K 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
}

dlloyd

Quote
What's wrong With this forum? I can't even edit my post within 5 minutes of having posted..  :(
I think that after you've reached a certain number of posts, there'll be no timeout.

jhsa

Thanks.. I think we have a winner.. Thank you so much..
I think this should go in some examples page.. I didn't find any code that can emulate an monostable device.
dlloyd you're the man. Thank you very much..
I will now scope the output to make sure it is stable enough.. This will be used to power an RC RF module, so it has to be reliable..
I also just compiled your code for an Attiny85. Will see if it works ;-) it will make it small and with less components.
Would you mind if I share your code here?

http://openrcforums.com/forum/viewtopic.php?f=85&t=8471

Of course i will post a link to this thread as well.

João

UKHeliBob

Quote
This will be used to power an RC RF module
I hope that you are not powering the RF module directly from the Arduino pin.

As a matter of interest what are you going to use the program for in practice ?  Why the need to turn the RF module on and off in this way ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jhsa

#26
Feb 09, 2016, 05:31 pm Last Edit: Feb 09, 2016, 05:38 pm by jhsa
I hope that you are not powering the RF module directly from the Arduino pin.
No, of course not ;) I will use a MOSFET transistor..

As a matter of interest what are you going to use the program for in practice ?  Why the need to turn the RF module on and off in this way ?
There is a couple open source RC transmitters based on the very cheap Turnigy TH9x radio sold in China.
With the open source firmware these are very powerful systems. My radios for example have voice, telemetry, rotary encoder, Trainer over bluetooth, telemetry to an android device over bluetooth, support extra pots and switches, etc, etc.. The firmware is way more powerful than many/most (if not all) the commercial units. We can deal with logical switches, global variables, scalers, etc.
Some guys even have designed boards that mount on top of the original 9x radio boards, and also others that can replace the original board. one of the boards, the 9xtreme, can have 2 RF modules installed, one internally and the other in the module bay, and their power can be switched by software.
The other boards, have signal output for 2 RF modules, but not enough pins to switch the power by software. So, I had the idea of monitoring the module signal input, and turn it ON or OFF depending on if there is a signal. I requested Mike (ersky9x and er9x developer) if he could allow to turn the protocols OFF in the firmware. So, like that we can control the module's power on those radios as well. Having both modules ON when not needed is a waste of battery power and also introduces more RF noise, which is not good.. If you want to follow these projects, please check the forum I linked on my other post above  :)

João

Go Up