How To Program a Switch to keep light on, then off again.

Hey there, so I am having an issue.

The goal is to get a light to stay permanently on when a switch is moved into the "open" position, and then when the switch moves back to the "closed" position, the light turns off.

I have this sample of my code, currently, when you turn the switch to "open", the light flashes on for a millisecond basically and then immediately turns off. As I said just before, I want the light to stay on when the switch is "open" and turn off when the switch is "closed."

else{ //If restraint button in open position...
    Keyboard.press(KEY_F9);
    Keyboard.press(KEY_F10);//Press F9 Key
    delay (100); //Delay 100ms
    Keyboard.releaseAll(); //Release F9 Key
   digitalWrite(DispatchLT, HIGH);
   RestraintHX = RestraintPOS;} //Save new button value

If anyone can help me figure out what I need to add to the code, it would be greatly appreciated!
Thanks!

the light flashes on for a millisecond basically and then immediately turns off

You don't have anything in your snippet called theLight, so we have no idea what you are talking about.

If anyone can help me figure out what I need to add to the code, it would be greatly appreciated!

The rest of it!

Snippets are best handled by http://snippets-r-us.com.

Doh, I kinda figured I should have posted the entire code.
So I have attached the file here.
Thanks for helping!

NoLimits2RCS_DD__flyinglightedit.ino (8.73 KB)

See the image below (which created by another member).

Thanks for helping!

I can't access rubbish bin, so I won't be.

There is a link below this window, Additional Options..., that lets you attach your code here.

^ Thanks for that, fixed the original post and edited my 1st reply!

The goal is to get a light to stay permanently on when a switch is moved into the "open" position, and then when the switch moves back to the "closed" position, the light turns off.

Which light? Which switch?

Your code would be a lot easier to read if the { and } lined up properly. Put each { on a new line, and use Tools + Auto Format.

        Keyboard.press(KEY_F10);//Press F9 Key

Looks like you are confused again. Comments that state the obvious do not add any value.

      if (DispatchON == LOW)
      { //If dispatch light is off...
        DispatchON = HIGH;
      } //Turn on...
      else //If dispatch light is on...
      DispatchON = LOW; //Turn off...
      digitalWrite(DispatchLT, DispatchON);
      digitalWrite(Dispatch2LT, DispatchON);

Inconsistent use of curly braces is going to bite you in the ass sooner or later.

Some comments that explain WHY you are doing something, rather than what you are doing would be useful.

Mixing millis() calls and delay() is pointless.

I'm gonna be honest. My friend wrote the code and I've been using it, so all the errors you see are pretty much his fault and I don't have the knowledge to clean it up. I'll have to nudge him to do that.

So I reformatted and attached the file here, hopefully it makes a difference.

The light I am talking about is DispatchLT and the switch I am talking about is RestraintBT

The specific code is this.

if (RestraintHX != RestraintPOS){  //If restraint button has moved...
      delay (100); //Delay 100ms
      if (RestraintPOS == LOW){ //If restraint button in close position...
        Keyboard.press(KEY_F11); //Press F12 Key
        Keyboard.press(KEY_F12);
        delay (100); //Delay 100ms
        Keyboard.releaseAll(); //Release F12 Key
        digitalWrite(DispatchLT, LOW);

        RestraintHX = RestraintPOS;
      } //Save new button value
      else{ //If restraint button in open position...
        Keyboard.press(KEY_F9);
        Keyboard.press(KEY_F10);//Press F9 Key
        delay (100); //Delay 100ms
        Keyboard.releaseAll(); //Release F9 Key
        digitalWrite(DispatchLT, HIGH);
        RestraintHX = RestraintPOS;
      } //Save new button value
    }

NoLimits2RCS_DD__flyinglightedit.ino (9.48 KB)

TTD03:
Hey there, so I am having an issue.

The goal is to get a light to stay permanently on when a switch is moved into the "open" position, and then when the switch moves back to the "closed" position, the light turns off.

Isn't this just as simple as

void loop() {
   ledState = ! digitalRead(switchPin); // assume ON = LOW so invert the value
   digitalWrite(ledPin, ledState)
}

Or perhaps the title and the question need a bit more thought.

...R

Sorry, not sure how much better I can word it.

Here is a video I took of myself, I modified the code by adding a delay onto it...
Here is the part I modified.

RestraintHX = RestraintPOS;
      } //Save new button value
      else{ //If restraint button in open position...
        Keyboard.press(KEY_F9);
        Keyboard.press(KEY_F10);//Press F9 Key
        delay (100); //Delay 100ms
        Keyboard.releaseAll(); //Release F9 Key
        digitalWrite(DispatchLT, HIGH);
        delay(1000);
        RestraintHX = RestraintPOS;
      } //Save new button value
    }

It makes make the DispatchLT go on when RestraintPOS changes to "High." It only stays on for 1 second, and while it is on, nothing else works because it is on a delay.

The goal is to get this light to stay on permanently while the switch is "high" and to allow the device to continue working and not be frozen up in the "delay" mode. Then, when the switch is turned back to the "Low" position, the DispatchLT light turns off.

TTD03:
The goal is to get this light to stay on permanently while the switch is "high" and to allow the device to continue working and not be frozen up in the "delay" mode. Then, when the switch is turned back to the "Low" position, the DispatchLT light turns off.

Isn't that what my little piece of code does - without a delay() anywhere - adapt/adopt the idea into your code.

Look at how things are managed without using delay() in the demo several things at a time and in this Thread Planning and Implementing a Program.

...R

test

TTD03:
test

I think that is your role ???

...R

Oh no, I didn't mean you test it, it was just a test post :stuck_out_tongue:

For some reason my account was removed and readded and all those posts are not associated with me anymore.

Says TTD03 "Guest"
Not "TTD03 "Newbie" like this post.

Weird.