start and stop code with a push button

Hi
I'm trying to get my code to turn on and off at the push of a button but I'm having no luck could anyone help please, the code is to just cycle through 4 leds and 3 RGB's. i have pieced the code together from what i have found online. thanks in advance.

  const int ledRed         =11; 
  const int ledGreen      =10; 
  const int ledBlue        =9;
  const int ledPinUV      =6;
  const int LedPin1	  =5;
  const int LedPin2	  =3;
  const int LedPin3	  =2;


void setup() {
  pinMode (ledRed,       OUTPUT);
  pinMode (ledGreen,    OUTPUT);
  pinMode (ledBlue,      OUTPUT);
  pinMode (ledPinUV,     OUTPUT);
  pinMode (LedPin1,	  OUTPUT);
 
}
void loop() { 
  
  analogWrite(ledPinUV, random(120)+135);
  delay(random(250, 500));
  
  CommandPWM_ON(ledBlue);    
  delay(random(250, 500));
 
  CommandPWM_ON(ledGreen);
  delay(random(250, 500));
  CommandPWM_OFF(ledBlue);   
    
  CommandPWM_ON(ledRed);
  delay(random(250, 500));
  CommandPWM_OFF(ledGreen);
 
  CommandPWM_ON(ledBlue);
  delay(random(250, 500));
  
  CommandPWM_ON(LedPin1);
  delay(random(250, 500));
  
   CommandPWM_ON(LedPin2);
  delay(random(250, 500));
     CommandPWM_ON(LedPin3);
  delay(random(250, 500));

 }
void CommandPWM_ON(int NumPin)  {
  for( int i = 0; i <= 255; i++ ) {
        analogWrite(NumPin, i); 
        delay(5);
   } 
} 
void CommandPWM_OFF(int NumPin) {
for( int i = 255; i >= 0; i-- ) {
        analogWrite(NumPin, i);
        delay(5);
  }
}

CODE.png

You don't "turn code on and off". You write code that performs one thing or another - it branches - according to the state of certain things, such as whether a button connected between a pin and ground with that pin set to a pinMode of INPUT_PULLUP, is HIGH (button not pressed) or LOW (button pressed).

However you may - and I suspect you do - want the "state" to switch one way on one press of the button and another way on the next press. This is much more complicated than it seems due to something called "contact bounce".

There is no connection from the breadboard to Arduino GND, you shouldn't use pin 1 for the button input, it's the serial "TX" pin.

JCA34F:
There is no connection from the breadboard to Arduino GND, you shouldn't use pin 1 for the button input, it's the serial "TX" pin.

Sorted thanks I've been swapping wires about to make the diagram cleaner, missed the earth and changed button input to pin 4 also.

@Paul__B

That's the plan yeah, but not having any joy with the code,

I had thought of just switching the power on and off but no lol.

Hi,
What does your code do now?

Tom.. :slight_smile:

TomGeorge:
Hi,
What does your code do now?

Tom.. :slight_smile:

]
when power is on it cycles though dimming LEDs its a simple idea but the code took ages to get working lol