1 button + 1 switch for 1 led

Hi, I know that there is another forum about two buttons and one led but its not really how I want it to work. I don't know if this is possible but here is what I'm trying to do: I want to use one switch (the kind that has two positions; on and off. And it stays in each position) and one button (push in turns on, let it go and it turns off) to turn on and off an led. When I turn the switch to the on position, I want the led to turn on, when I press and hold the button, the led just stays on. As soon as I let go of the button (so it turns off), I want there to be a 10 second delay, then the led slowly fades out till its off, (but that switch is still in the on position). If I press and hold that button again, the led turns on again and stays on till I let go of the button. Then there is a 10 second delay again and the led fades out and turns off. Turning the switch to the off position should imediatly fade the led out till its off, no delay there. (If the led is still on at that time of course) So I hope this understandable how I want it work and hopefully someone could help with this. If its impossible, then no problem.

Welcome to the forum hphorsepowerful.

So I hope this understandable how I want it work and hopefully someone could help with this.

You should draw a schematic of the circuit then post it for us to see.
Write the sketch, if you run into problems, ask for help.

.

That is definitely possible. Arduino would be a pretty sucky computer if it couldn't handle something as simple as that.

What have you tried so far? Do you have any experience writing code? If not then you might want to start simple, learn to make an LED fade, then learn to read a button and switch, then learn to handle timing without using the delay function, then finally put it all together to build a program to do what you want.

See if you can get started. Don't just throw something together and show up back here with some random junk written and frustrate everyone and think it's gong to work out. Go do some reading and tinkering and learning about those things one at a time and ask specific questions when you get stuck.

I am new to this arduino and coding stuff but I’ve done a bit of research and practiced quite a bit. I tried to put something together from all the stuff that I learned. In the attachments I’ve included a picture of how I wired it and the code I have is below. It works but the problem is after the led has faded out from pressing the button, it jumps back to the switch part of the code and the led is on again. So what I need help with is how to write the code so that it doesn’t jump back to the switch part.
This is the code I have:

const int ledPin = 9;
const int buttonPin = 2;
const int fadingDelay = 50;

int switchPin = 4;
int val = 0;
int buttonState = 0;
boolean fadingState = false;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(switchPin, INPUT);
}

void loop() {
val = digitalRead(switchPin);
if (val == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
if (fadingState == false) {
for (int i = 0; i <= 255; i += 5) {
analogWrite(ledPin, i);
delay(fadingDelay);
}
} else {
for (int i = 255; i >= 0; i -=5) {
analogWrite(ledPin, i);
delay(fadingDelay);
}
}
fadingState = !fadingState;
}
}

tmp_17511-one button1475979011.PNG

It is late here but here are some thoughts

2ea74217a87130d15710fedbd2992b2116b92c90.png

You need a series resistor for the LED 220 ohms

You need a 10K resistor from pin 4 to GND (as a pulldown)

The 220 resistor should be moved to pin 2 then to GND.
The 220 ohm resistor should be changed to 10K.

I recommend:
Rather than running +5 volts to the switches, go through two 220 ohm resistors then to the switches.
This can help prevent accidental shorting things out and damaging circuits.

+5V-----220----switch-----10K and your pin 2 ---- other end of 10K-----GND
+5V-----220----switch-----10K and your pin 4 -----other end of 10K-----GND

Use to format your sketches.

“it jumps back to the switch part of the code and the led is on again.”
That’s what loop() does, it goes back to the beginning of loop() and starts all over.

You have to learn how to ‘not’ use the delay() function as they are blocking code in your for loops.
More on this later unless the other side of the world can take over.

:sleeping:

Write what your sketch should do in point form.
1.
2.
3.
4.
5.
6.

etc.

What are you going to use this circuit for?

.

Sorry for my late reply, I've been real busy lately so I don't have much time to play with the arduino. I was going to use it on my car for the under glow that I have on there, but I needed it for this weekend for a show thing, but I'm just going to leave it for now. Just thought perhaps someone could help me write a code but its all good. I'll try and figure it out some other time.