Using momentary push button to Start/Stop a program

So what I'm trying to make is essentially a display for some glass art work with an LED to back light the glass.

Currently, I have a code and circuit that run off an arduino nano exactly how I want. How I foresee the finished product is some sort of switch that turns on the arduino, runs the program for a few minutes then turns off. As well, if I press the switch a second time it turns off the program.

My experience with coding is limited but I have a fairly good understanding of circuits. I have found a few bits of code that seem to accomplish what I want, but either I don't understand them or when I modify it into my program it doesn't work.

Any help with this would be greatly appreciated.

Here's the program for the Color phasing RGB

const int red = 11;

const int green = 10;

const int blue = 9; 

int r = 255;
int b; 

int g; 

int t = 250;

void setup() {}

void loop() {

for (/* no initialization */; r>=0, b<255; b++, r--) /*red -> blue*/

{

analogWrite(red, r);

analogWrite(blue, b);

delay(t);

}

for (/* no initialization */; b>=0, g<255; g++, b--) /*blue -> green*/

{

analogWrite(blue, b);

analogWrite(green, g);

delay(t);

}

for (/* no initialization */; g>=0, r<255; r++, g--) /*green -> red*/

{

analogWrite(red, r);

analogWrite(green, g);

delay(t);

}

}

That's just a switch between the power supply and the Arduino. The sketch you have loaded will run automatically.

As in it suspends the program? Very doable with a second switch. The trouble with your idea is that the switch that powers the Arduino on and off can't be the same one that's keeping track of the number of button presses, since the Arduino keeps track of nothing when it's off.

You need to read a button input. Check arduino/reference.

Start with the basic arduino IDE sample sketches. Also, please use more meaningful names that just r, b, g, t etc
Use the Auto format under the Tools menu so your code is more readable.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.