Control LED, radio button, with external 5v DC pulse

Thanks to Paul__b, I have a radio button 2 LED code. I want to control the on/off of the LEDs with an external 5v DC pulse. The external pulse is used to activate a solenoid. The pulse is split and rectified to 5v DC. The purpose of the 5v DC pulse is to turn on/off LEDs indicating the direction of the solenoid. I have used the input pulse in the digital and analog inputs of the Arduino UNO with no success. How do I use this pulse to turn LEDs on/off? What am I missing? Thank for your help.

If you post the code you've tried and the results it gave then maybe we could say what you're missing.

But currently I can't even understand your description. What does "The pulse is split and rectified to 5v DC" mean? Can you post a circuit diagram, particularly including whatever is doing this splitting and rectifying.

Steve

The code is below. Both LEDs are on, no change when button pushed. See attached file for circuit diagram. Essentially want the LEDs to indicate which way the duel solenoids are set. Sorry for any confusion. Thanks for you help.

//Atlas

const int redledPin = 13;              //led pin
const int greenledPin = 11;
const int redPin = 2;           //red input
const int greenPin = 3;

int redpinValue = 0;                     //value of input
int greenpinValue = 0;

  
//"marker" choses which counter to check
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) {
    *marker += interval;    //move on ready for next interval
    return true;
  }
  else return false;
}

//Uses analog reading input,  
boolean  pinvalue(char analog, unsigned long *marker, char *pinvalue, unsigned long interval) {
  switch (*pinvalue) {                                 //odd states if was presses, >= 2 if debounce in progress
    case 0: //button up so far,
      if (pinvalue == HIGH) return false;                     //nothing happening!
      else {
        *pinvalue = 2;                                   // record that is now pressed
        *marker = millis();                                 // note when was pressed
        return false;                                       //and move on
      }

    case 1:   //pinvalue low so far,
      if (pinvalue == LOW) return false;               // noting happening!
      *pinvalue = 3;                              //record that is now released
      *marker = millis();                            //note when was released
      return false;                                  // and move on

    case 2:                                        //pin value high, now down.
      if (pinvalue == HIGH) {
        *pinvalue = 0;                          // jackpot! update the state
        return true;                              //because we have the desired event!
      }
      else
        return false;                                    //not done yet; just move on

    case 3: // button was down mow up
      if (pinvalue == LOW) {
        *pinvalue = 1;                               //no, not debounced; revert the state
        return false;                                   // false alarm!
      }
      else {
        if (millis() - *marker >= interval) {
          *pinvalue = 0;                             //debouncd; update state
          return false;                                 // but not the event we want
        }
        else
          return false;                                   //not done yet; just move on
      }
    default:                                        //error; recover anyway
      {
        *pinvalue = 0;
        return false;                                  // definitly false!

      }
  }
}

void setup() {
  pinMode(redledPin, OUTPUT);
  pinMode(greenledPin, OUTPUT);
  pinMode(redPin, INPUT);
  pinMode(greenPin, INPUT);
  digitalWrite (redledPin, LOW);
  digitalWrite (greenledPin, HIGH);
  digitalWrite (redPin, LOW);
  digitalWrite (greenPin, HIGH);

 // pinMode(redpinValue, INPUT);
 // digitalWrite(redpinValue, HIGH);      //enable pullup resistor

 // pinMode(greenpinValue, INPUT);
 // digitalWrite(greenpinValue, HIGH);    //enable pullup resistor
}

void loop() {
      
    redpinValue = digitalRead(redPin);
    digitalWrite (redledPin, HIGH);
    digitalWrite (greenledPin, LOW);
    

      greenpinValue = digitalRead(greenPin);
      digitalWrite (redledPin, LOW);
      digitalWrite (greenledPin, HIGH);
   
    }

Attached is the diagram.

Atlas.pdf (313 KB)

Atlas.pdf (313 KB)

...control the on/off of the LEDs with an external 5v DC pulse... The pulse is split and rectified to 5v DC

So you are rectifying DC to... DC? Perhaps you should correct any errors in your original post.

I can't see anything in your "diagram" doing any splitting or rectifying. But anyway it isn't a circuit diagram because it doesn't say what the components are nor indicate what wires connect anything to anything else.

And the code is incomprehensible. You have two functions timeout() and pinvalue() which are defined but never used but your main code in loop() just continually reads a couple of pins then ignores their values.

Steve

I know my diagram is not an electrical circuit, it is intended to indicate my goal, to use the 16v AC pulse used to activate the solenoid to also turn on/off LEDs indicating the direction of the solenoid. I do know how to convert 16v AC to 5v DC, bridge rectifier and voltage regulator, possibly a cap on the output. The 16v AC goes to a 2 button switch. When pushed the switch in one direction it activated the solenoid in one direction, and in the other direction activates the solenoid in the other direction. All I want is to have the pulse to turn an LED indicating the direction. I also know the Arduino operates on 5v DC. My thought was if I had a 2 button, 2 LEDs code, (radio buttons) I could modify it to triggered the LEDs by the same signal. I could not. I know my code is a mess. My question is now do I feed the 5v DC momentary pulse in to the Arduino to switch the LEDs? Thank you.

You connect the 5V pulse signals to two Arduino input pins. Sounds like you already did that. Check the voltage at the pins with your multimeter, to test that they are either 0V or 5V as expected when you activate the switch.

If that's working as expected, your code is probably reading the signals ok, but as Steve said earlier, your code is not doing anything different as a result. You need some if() statements.

Input from external switch in Arduino input 2 and 3. When external 5v is off, voltage of 5v is measured, when button pushed voltage go to 0.4v. LEDs switch on and off as expected. With external 5v on, one LED is on and does not switch when button is pushed. The voltage measured is always 5v, does not go to 0v. Any thoughts? Thanks.

//Atlas

const int redledPin = 13;              //led pin
const int greenledPin = 11;
const int redbutton = 2;           // 5v red input
const int greenbutton = 3;         // 5v green input

char redbuttonstate = 0;
char  greenbuttonstate = 0;

unsigned long redbuttoncount = 0;
unsigned long greenbuttoncount = 0;

  
//"marker" choses which counter to check
// Paul__b radio button
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) {
    *marker += interval;    //move on ready for next interval
    return true;
  }
  else return false;
}

//Uses reading of red button input,  
boolean  buttondown(char button, unsigned long *marker, char *buttonstate, unsigned long interval) {
  switch (*buttonstate) {                                 //odd states if was presses, >= 2 if debounce in progress
    case 0: //button up so far,
      if (button == HIGH) return false;                     //nothing happening!
      else {
        *buttonstate = 2;                                   // record that is now pressed
        *marker = millis();                                 // note when was pressed
        return false;                                       //and move on
      }

    case 1:   //button down so far,
      if (button == LOW) return false;               // noting happening!
      *buttonstate = 3;                              //record that is now released
      *marker = millis();                            //note when was released
      return false;                                  // and move on

    case 2:                                        //pin value high, now down.
      if (button == HIGH) {
        *buttonstate = 0;                          // jackpot! update the state
        return true;                              //because we have the desired event!
      }
      else
        return false;                                    //not done yet; just move on

    case 3: // button was down mow up
      if (button == LOW) {
        *buttonstate = 1;                               //no, not debounced; revert the state
        return false;                                   // false alarm!
      }
      else {
        if (millis() - *marker >= interval) {
          *buttonstate = 0;                             //debouncd; update state
          return false;                                 // but not the event we want
        }
        else
          return false;                                   //not done yet; just move on
      }
    default:                                        //error; recover anyway
      {
        *buttonstate = 0;
        return false;                                  // definitly false!

      }
  }
}

void setup() {
  pinMode(redledPin, OUTPUT);
  pinMode(greenledPin, OUTPUT);
  pinMode(redbutton, INPUT);
  pinMode(greenbutton, INPUT);
  digitalWrite (redledPin, LOW);
  digitalWrite (greenledPin, LOW);
   
  pinMode(redbutton, INPUT);
  digitalWrite(redbutton, HIGH);      //enable pullup resistor

  pinMode(greenbutton, INPUT);
  digitalWrite(greenbutton, HIGH);    //enable pullup resistor
}

void loop() {

     //Select LED if button debounced
    if (buttondown(digitalRead(redbutton), &redbuttoncount, &redbuttonstate, 10UL )) {
    digitalWrite (redledPin, HIGH);
    digitalWrite (greenledPin, LOW);
    }

   //Select LED if button debounced
    if (buttondown(digitalRead(greenbutton), &greenbuttoncount, &greenbuttonstate, 10UL)) {
      digitalWrite (redledPin, LOW);
      digitalWrite (greenledPin, HIGH);
   
    }
}

What external 5V? What is it connected to? Is it powering the Arduino? What is powering it when the external 5V is off? And if everything works when you have no external 5V why don't you just leave it off?

So much missing information. Do you see why we need a circuit diagram?

Steve

Steve,
I don't see why you need a circuit diagram, sorry. I would like to use the 16v Ac which powers a dual solenoid turnout on my model railroad to power indicator LEDs indicating the direction of the turnout. The turnout is powered by an external switch 2 button switch. The turnout only needs a momentary pulse of 16v AC. Arduino can not handle 16v AC. I want to use this pulse to turn on/off LEDs indicating direction. I can rectify the 16v AC to 5v DC. The 16v AC comes from a external power supply. The Arduino is powered by it's own source. My radio button code has 5v on pin 2,3, when the button is pushed the voltage goes to 0 and LEDs switch, red off, green on and visa versa. The code works when the external 5v is off. With the external 5v is on the voltage is 5v all the time, it does not go to 0. Putting the external 5v in pin 2,3, which also has 5v in the off position will not goto 0 when the button is pushed. The problem is not the circuit to create the 5v, it's in the code or how the Arduino is wired. How do I have have pin 2,3 at 0v and 5v when the button is pushed? Opposite to what I have in the code. Set redbuttonstate, green buttonstate=5 instead of 0? How can you trigger the LEDs with this external source? Thanks.

gfvesley:
I don't see why you need a circuit diagram, sorry.

Apology accepted. It's simply a reflection of your current level of experience. It won't be long before you begin to understand that schematics is the natural language of electronics. "A picture says a thousand words" etc. And says them much more unambiguously. Imagine trying to describe a complex railway layout only in words and not being allowed to draw a diagram.

O.k. I can't really follow that cascade of words. But I think you are saying that in your preferred configuration the pins you want to use for switching are stuck at 5V and never go down to 0V. If that's true then it ain't a coding problem. No clever software will let you do something useful when the inputs don't ever change.

But good luck with it anyway.

Steve

Steve,
Still not sure why a schematic is necessary. The only electronics is a bridge rectifier and voltage regulator. Hopefully the attached diagram will explain better what I tried to explain in the previous post. As you see I want to use the pulse which activates the solenoids to also switch indicator LEDs. Using the code in previous post, there is 5v between 2,3 and ground. When the button is pushed the voltage goes to 0. It does not make sense to me to put the external 5v in those inputs. Hope this makes it clearer. Thank you.

gfvesley:
Still not sure why a schematic is necessary.

At least you are no longer sure that a schematic is not necessary. That's progress.

gfvesley:
Hope this makes it clearer.

Sorry, not quite yet.

Not sure what you want. I see the mistake, top red line should be extended further left. Sorry.

Google for schematic diagrams. Look at ones that include switches, resistors, capacitors, Arduinos, LEDs, rectifiers, voltage regulators, solenoids. All those things you mentioned are in your circuit. Draw a diagram in that style, using those symbols, annotated with values and part numbers with every wire and connection clearly shown. Hand drawn is fine. A proper schematic, hand drawn on paper, is much better than anything vague and ambiguous drawn with some general purpose drawing app.

So your picture looks like you have two bridge rectifiers and two voltage regulators. You say you only have one of each but if that's true how do you get two different pulses out of it?

I'm guessing that 5V DC over Red LED is supposed to mean "5V signal for the Red LED" but why does none of that apparently connect to the Arduino? And the Arduino doesn't seem to have anything powering it.

But then where in that picture is the "External 5V" that you keep talking about? Where does it come from? What is it for? And why do you want to put it into the inputs if everything works without it?

The trouble we're having is that by drawing your pictures in that strange way you're effectively making up your own language instead of using the normal language of electrical schematics that the rest of us understand. It's like you've decided to ask your questions in a made-up language instead of English and are then surprised that no-one understands you.

Steve

Thank you for your comments. It is obvious we are not communicating. I do understand electrical schematics although I am not an electrical engineer, which is unusual for organic chemists. I still don't understand why you want a schematic and where the 5v DC comes from. I have a 5v DC pulse, I measure it. For what it is worth, I have a model railroad road. Turnouts (track switches) are controlled by two solenoids which operate on 16v AC with 2 push buttons, one for each direction. The 16v AC comes from a train transformer. I want to use this signal to turn on off red/green LEDs indicating direction of turnout. I chose 5v DC because the Arduino operates on 5v, I could make it any value. So I have two 5v DC signals and 2 buttons and I want to use these signals to indicate the direction of the turnout. My question is how do I do this? I have a radio button, 2 button, code thanks to Paul__b of the forum, posted previously. The buttons are connected between pin 2 and 3 and ground. Pushing the button turns one LED on and the other off. Pin 2 reads 5v and when button is pushed the voltage goes to 0 and one LED goes off and the other on. I want to use the 5v DC pulse to do the same thing. If I use the pulse as a digital input, only one LED comes on and it does not switch. How do I change the code? So I want use the same signal the activates the solenoid to switch the LEDs. I can't explain it any better. Thanks for your help.