I typed all this up before seeing your true situation. You will need to learn some coding to do what I wrote or you will need a friend to write code for you, or hire someone.
Programming Arduinos is not rocket science but it still takes many hours total to get going.
I don't know how brilliant you are, you might be or you might show this to your friend if it's beyond what you are prepared to do.
Phury:
Sorry if I was unclear.
I want to turn a fade effect on and off with a button.
It is only from one pin with multiple led lights.
And it should go on until I press the button again.
Just looking at the fade process, two ways to do it are
- a loop inside of void loop() that makes the fade before void loop() can end (to run again).
void loop()
{
// other code, like button code that sets myFadeFlag only when press just happened
if ( myFadeFlag == 1 )
{
for ( byte i = 0; i < 256; i++ )
{
// use PWM to light the led to level i
delay( 100 );
}
}
}
This way has problems. It can't do "And it should go on until I press the button again." because as long as the code is stuck in that for-loop the button code can't run. That is the crux of your problem.
- use void loop() as the small wheel that drives the action of all the parts in your sketch.
Imagine that your fade code is a person who watches a light and a clock. If the light is on, the person waits for the second hand to reach 12 and turns the fade up or down a notch. In this way the seconds go by, the person sees, and action is only done if the light is on and the minute is up.
Imagine that the button code is another person who watches the button and turns the light on and off only when the button has just been pressed. The button can be pressed and let go, the light gets switched and stays. One press, light on, next press, light off... not light on if down and light off if up but press-change, press-change.
With Arduino we can tell time by the millis() clock. It uses unsigned long variables to count milliseconds, as a beginner you want all your time variables to be type unsigned long.
unsigned long timeStart, timeWait;
byte fadeFlag; // default is zero
void setup()
{
// code
}
void loop()
{
// button code changes fadeFlag
if ( fadeFlag) // this is the the person seeing if the light is on
{
if ( millis() - timeStart >= timeWait ) // this is the the person watching the second hand
{
// fade the light 1 notch... use a variable to keep track
timeStart = timeStart + timeWait; // set up the timer for the next 'minute'
}
}
}
This is missing pieces of course. It doesn't know when to stop fading. There is nothing to start the fade or about using PWM to control led brightness. A few other details are missing.
All this is to show is a way to let the things run together. There are tutorials that show details, not exactly what you want but close with working code. In my signature space under this post are 3 addresses for Nick Gammon tutorials, the first one can help you more than my post here.
In this forum there is a 'sticky thread' always near the top that has the lesson in another way.
This "only an assignment" could be pivotal in your understanding of automation control. And yes, you can automate fashion and it can serve you well.