Suggest programming for a sketch

Hi All, Can anybody suggest a short program to solve the problem I have. When I have an external circuit connected to my nano a led flashes quite rapidly. If the external circuit is not connected the led is always off. The program would need to be of the form ' if the led is permantly off follow one course of action, if led is flashing follow another course of action'.
Can anyone suggest a good short program for this? The flashing of course involves alternate highs and lows being applied to the led.

What is the "external circuit"?

are you saying the external circuit is alternately toggling an input to the Nano On and Off?

if so, capture a timestamp (millis()) whenever there is a change in the input state and if there hasn't been a change in some period of time (timestamp - millis()) declare the input as Off

+1.

Your prose makes it hard to see what the you talking about.

Please draw a schematic showing how all the parts of this project are wired, be sure to include the source(s) of power.

a7

Hi,
is it a school work?

Due to these unspecific requirements "' if the led is permantly off follow one course of action, if led is flashing follow another course of action'." try this sketch. Check it and mod it to your needs.

/* BLOCK COMMENT
  - https://forum.arduino.cc/t/suggest-programming-for-a-sketch/1064110
  - Suggest programming for a sketch 
  - This sketch may contain traces of C++.
  - In case of indisposition:
  - https://www.learncpp.com/
  - Hardware:
  - Thanks to LarryD
  - https://aws1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
  - Tested @ Arduino: Mega[X] - UNO [ ] - Nano [ ]
  - https://forum.arduino.cc/u/paulpaulson/summary
*/
#define ProjectName "Suggest programming for a sketch " 
// make variables
constexpr byte ButtonPin{A0}; 
// make structures
struct BLINKLED 
{
  const byte pin;
  const unsigned long Duration[2];
  unsigned long stamp;
};
BLINKLED blinkLed {LED_BUILTIN,{500,10},0}; 
// make functions
void runInputIsLow() 
{
  // do other things
}
void runInputIsHigh() 
{
  if (millis()-blinkLed.stamp>=blinkLed.Duration[digitalRead(blinkLed.pin)])
  {
    blinkLed.stamp=millis();
    digitalWrite(blinkLed.pin,!digitalRead(blinkLed.pin));
  }
  // do other things
}
void setup()
{
  Serial.begin(9600); 
  Serial.print(ProjectName),Serial.print(" in "),Serial.println(__FILE__);
  pinMode(ButtonPin,INPUT_PULLUP);
  pinMode(blinkLed.pin,OUTPUT);
} 
void loop()
{
  digitalRead(ButtonPin)==LOW?runInputIsLow():runInputIsHigh();
}

Have a nice day and enjoy coding in C++.

petercl14's questions have never been school work; but always excel in vagueness.

That sounds like a good solution. The external circuit is a transmitter. There is a RF connection to the nano from another nano. When there is a valid connection the led is on. Unfortunately it is also blinking. I gather this is from data being transmitted. When there is no transmission the led is always off.
I have used millis before but this has always been for fixed periods. They are better than delays because they don't stop the rest of the program from running. For those on the forum that don't know what millis is; it measures in the background the length of time the program has been running.
Not sure how to use it in this instance. Can you suggest some coding? Thanks.

Too complicated Paul. Probably my fault for not being specific enough as somebody has complained. The external circuit is a RF connection to another NANO board which provides the transmitted data. When there is a valid RF connection between the boards an led on the receiving board flashes. No valid connection and the led is off. I am trying to use this to shut down power on the receiving board.
I see that somewhere in your solution you are using millis as has been suggested. I can usually manage to come up with much simpler programming. I guess I am no genius and just don't understand programming that is too far removed from expressions which look like language in English.

You might be more comfortable wiring up a '555 in a retriggerable monostable multivibrator configuration.

Way less code.

a7

1 Like

const byte PinOut = LED_BUILTIN;
const byte PinInp = A1;

byte inpState;

unsigned long MsecPeriod = 2000;
unsigned long msecLst;
bool          active = false;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();

    byte inp = digitalRead (PinInp);
    if (inpState != inp) {
        inpState = inp;
        msecLst = msec;
        active  = true;
        digitalWrite (PinOut, On);
    }

    if (active && msec - msecLst >= MsecPeriod) {
        active = false;
        digitalWrite (PinOut, Off);
    }
}

void
setup (void)
{
    Serial.begin (9600);

    pinMode (PinInp, INPUT_PULLUP);
    inpState = digitalRead (PinInp);

    pinMode (PinOut, OUTPUT);
    digitalWrite (PinOut, Off);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.