Looking for an example that Arduino set pin and hold

and reset the pin when got a signal?
If there are any.
Thanks
Adam

Something like

// change below LOW to HIGH in case of an active HIGH signal
#define ISPRESSED LOW

const uint8_t outputPin = LED_BUILTIN;
const uint8_t signalPin = 3;

void setup()
{
  pinMode(signalPin, INPUT_PULLUP);
  pinMode(outputPin, OUTPUT);
  digitalWrite(outputPin, HIGH);

}

void loop()
{
  if (digitalread(signalPin) == ISPRESSED)
  {
    digitalWrite(outputPin, LOW);
  }
}

Code assumes that the signal is normally HIGH and goes LOW; for a button, wire button between pin and GND.

1 Like

if just an echo, why not simply

void loop()
{
    digitalWrite (outputPin, digitalread (signalPin));
}

@gcjr

If not mistaken, that does not work on all Arduinos. I know it works on AVR based ones.

why wouldn't it?

Thanks.
The function I am looking for is:

  1. get a pulse HIGH from pin2 (pin2 go LOW after pulse), set the pin7 HIGH and hold;
  2. set the pin7 LOW when get a pulse HIGH from pin3 (pin3 goes LOW after pulse).
    can have this?

If I understand you what you mean, you are trying to create a bistable latch. Is that correct?

1 Like

Thanks.
Yes.
I've checked few bistable multivibrator circuit, all of that used a button to trigger, which is not my situation.

This.

1 Like

Thanks.
I'll test that.
I found one here but didn't get idea result:

What do you mean? Did you actually build the circuit in hardware? Or are you trying to emulate it on the µC and that isn't working?

1 Like

There is no need to do anything to hold a pin’s state, it will remain at the state you set it until you set it to another state.

You need to detect when a pulse input goes high not if it is high.

See the state change example in the examples menu of the IDE to do this. Then apply it to your two inputs. Note you will need a previous state variable for each of the two inputs when you extend this to your problem.

1 Like

Thank.
I build a circuit does work stable, I'd say maybe the wiring?

Thanks.
attached is the code I used, that need check the pin state, and the trigger pin just issue a pulse and then go LOW, the code need do some thing when get a LOW signal.

const int StartPin = 2;     // the number of the pushbutton pin
const int StopPin = 3;     // the number of the pushbutton pin
const int ledPin =  7;      // the number of the LED pin

// variables will change:
int StartState = 0;         // variable for reading the pushbutton status
int StopState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(StartPin, INPUT);
  pinMode(StopPin, INPUT);

}

void loop() {
readStartState();
readStopState();
  if (StartState == HIGH ) {
    if (StopState == LOW)
    {
      digitalWrite(ledPin, HIGH);
      readStopState();
    }
    else if (StopState == HIGH) {
      readStartState();
      if (StartState == LOW)
      {
        digitalWrite(ledPin, LOW);
      }
    }
  }
}

void readStartState() {
  StartState = digitalRead(StartPin);
}

void readStopState() {
  StopState = digitalRead(StopPin);
}

Can you explain? What did you build the circuit with? Can you post a drawing of what you built?

You have not done what I said you should. Did you look at the state change example in the IDE?

Sorry,
I don't know exactly what you mean?
I have a led attached to pin7, if the pin go HIGH, led is ON.
in my testing code, the led ON when pin2 HIGH, and led off when pin3 HIGH, the problem is it not stable, some times doesn't do this way.


#define Button  A1
#define LED     10

enum { Off = HIGH, On = LOW };

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

    pinMode (Button, INPUT_PULLUP);

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

void
loop ()
{
    static byte butState = Off;
           byte but      = digitalRead (Button);

    if (butState != but)  {
        butState = but;

        if (On == but)  {
            digitalWrite (LED, ! digitalRead (LED));

            const char *s = digitalRead (LED) ? "Off" : "On";
            Serial.println (s);
        }

        delay (10);     // debounce
    }
}
1 Like

Have you looked at the example in the IDE menu
File -> Examples -> 02.Digital -> StateChangeDerection

This is an example of how you can detect either a rising or falling edge from a changing digital input.

So how have you wired your switches?
You don’t seem to be using the internal pull up resistors, so do you have a pull up or pull down resistor fitted? If not then that is why it is not reliable.

1 Like

Thanks.
The code works fine when put LOW signal into A1.

the thing is in my application need a HIGH pulse work as a signal, because of the trigger board function which normally LOW and pulse HIGH for a signal.