how can I switch between these two basic programs using a pushBUTTON

Right now these two programs are working with two loops non stop, but I want to add a pushBUTTON to select one or the other. tnx

#define REDPIN 3	// LED connected to digital pin 3
#define GREENPIN 5      //LED connected to digital pin 5
#define BLUEPIN 6       //LED connected to digital pin 6
#define FADESPEED 5  

void setup()
{ pinMode(3, OUTPUT);     // sets the digital // pin as output
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  }
  
void loop(){
 loop1();
 loop2(); }
 
 void loop1()
 
{ digitalWrite(3, HIGH);   // turns the LED on
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);
  delay(1000);              // waits for (x)ms
  digitalWrite(3, LOW);     // turns the LED off
  digitalWrite(5, HIGH);    // turns the LED on
  digitalWrite(6, LOW);
  delay(1000);             // waits for (x)ms
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, HIGH);   // turns the LED on
  delay(1000);  
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);    // turns the LED off
  delay(2000);  
}

void loop2() {
   int r, g, b;
 
  // fade from blue to violet
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
}
/code]

Add an input pin in"setup", make sure you use the built-in pullup, then read the switch in "loop" and select between "loop1" and "loop2" in "loop" with a simple "if"

i put the BUTTON but I still dont know what to do in loop

#define REDPIN 3	// LED connected to digital pin 3
#define GREENPIN 5      //LED connected to digital pin 5
#define BLUEPIN 6       //LED connected to digital pin 6
#define FADESPEED 5  
#define BUTTON 7
 int val= 0;            // val will be used to store the state of the input pin
  

void setup()

{ pinMode(3, OUTPUT);     // sets the digital // pin as output
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  
  pinMode(BUTTON, INPUT);
 
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  }
  
void loop(){
 val = digitalRead(BUTTON);   // read input value and store it
   if(val==HIGH)
   {
 loop1();
 }else{
 loop2(); }

  
  

 void loop1()
 
{ digitalWrite(3, HIGH);   // turns the LED on
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);
  delay(1000);              // waits for (x)ms
  digitalWrite(3, LOW);     // turns the LED off
  digitalWrite(5, HIGH);    // turns the LED on
  digitalWrite(6, LOW);
  delay(1000);             // waits for (x)ms
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, HIGH);   // turns the LED on
  delay(1000);  
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);    // turns the LED off
  delay(2000);  
}

void loop2() {
   int r, g, b;
 
  // fade from blue to violet
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
}
/code]

Set up a variable for the button to see when it is pushed, then use an if statement...

int ButtonState;
           if(ButtonState == LOW)
              {
                 // light up your leds or whatever you need loop1()
              }
            else if(ButtonState == HIGH)
              {
                 //  turn on other lights or whatever... loop2()

}

njs707:
Set up a variable for the button to see when it is pushed, then use an if statement...

int ButtonState;

if(ButtonState == LOW)
              {
                 // light up your leds or whatever you need loop1()
              }
            else if(ButtonState == HIGH)
              {
                 //  turn on other lights or whatever... loop2()



}

Shouldn't you be actually reading the switch state?

@martin_80x
You need a digitalRead() statement, to turn on the pullup resistor.

@martin_80x
You need a digitalRead() statement, to turn on the pullup resistor.

Though a digitalWrite works better.

If you want to switch loops while the button is pressed or not, njs707 has given you the code.

If you want to toggle on each button press, then it's a little different:

int loop=0;
void loop()
{
  if (digitalRead(BUTTON)==HIGH)
    loop=1-loop;
  if (loop)
    loop2();
  else
    loop1();
}

You may wish to debounce the button, depending on your switch/needs.

this is what I done and is not compiling

#define REDPIN 3	// LED connected to digital pin 3
#define GREENPIN 5      //LED connected to digital pin 5
#define BLUEPIN 6       //LED connected to digital pin 6
#define FADESPEED 5  
int loop=0;

void setup()
{ pinMode(3, OUTPUT);     // sets the digital // pin as output
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  }
  
void loop() {
 loop1();
 loop2(); }
  if (digitalRead(BUTTON)==HIGH)
    loop=1-loop;
  if (loop)
  
    loop2();
 
{ digitalWrite(3, HIGH);   // turns the LED on
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);
  delay(1000);              // waits for (x)ms
  digitalWrite(3, LOW);     // turns the LED off
  digitalWrite(5, HIGH);    // turns the LED on
  digitalWrite(6, LOW);
  delay(1000);             // waits for (x)ms
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, HIGH);   // turns the LED on
  delay(1000);  
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);    // turns the LED off
  delay(2000);  
}

else
   loop1; 
   
   int r, g, b;
 
  // fade from blue to violet
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
}
/code]

Yes - you just set up the number range, and use a slightly different counter (and a case/switch for simplicity):

int loop=0;
void loop()
{
  if (digitalRead(BUTTON)==HIGH)
  {
    ++loop;
    if (loop>=3) // 3 - number of loops: 0,1,2
      loop=0;
  }
  switch (loop)
  {
    case 0:
      loop1();
      break;
    case 1:
      loop2();
      break;
    case 2:
      loop3();
      break;
  }
}

You can get a lot fancier (and smaller) with this code, but this should work.

this is what I done and is not compiling

if (digitalRead(BUTTON)==HIGH)
    loop=1-loop;
  if (loop)
  
    loop2();

"loop" is a pointer to a function, and it is a constant, so you can't assign anything to it.
You certainly can't subtract anything from it.

martin_80x:
this is what I done and is not compiling

#define REDPIN 3	// LED connected to digital pin 3

#define GREENPIN 5      //LED connected to digital pin 5
#define BLUEPIN 6       //LED connected to digital pin 6
#define FADESPEED 5 
int loop=0;

void setup()
{ pinMode(3, OUTPUT);     // sets the digital // pin as output
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
 
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  }
 
void loop() {
loop1();
loop2(); }
  if (digitalRead(BUTTON)==HIGH)
    loop=1-loop;
  if (loop)
 
    loop2();

{ digitalWrite(3, HIGH);   // turns the LED on
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);
  delay(1000);              // waits for (x)ms
  digitalWrite(3, LOW);     // turns the LED off
  digitalWrite(5, HIGH);    // turns the LED on
  digitalWrite(6, LOW);
  delay(1000);             // waits for (x)ms
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, HIGH);   // turns the LED on
  delay(1000); 
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);    // turns the LED off
  delay(2000); 
}

else
   loop1;
   
   int r, g, b;

// fade from blue to violet
  for (r = 0; r < 256; r++) {
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  }
  // fade from violet to red
  for (b = 255; b > 0; b--) {
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
  // fade from red to yellow
  for (g = 0; g < 256; g++) {
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  }
  // fade from yellow to green
  for (r = 255; r > 0; r--) {
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  }
  // fade from green to teal
  for (b = 0; b < 256; b++) {
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
  // fade from teal to blue
  for (g = 255; g > 0; g--) {
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  }
}

From the looks of your code, you're including these alternate loops directly, so you don't need loop1/loop2. However, you wanted to have more than two loops in a post of yours (can't find it now), so try this code:

#define REDPIN 3	// LED connected to digital pin 3
#define GREENPIN 5      //LED connected to digital pin 5
#define BLUEPIN 6       //LED connected to digital pin 6

// not in code - where set????
#define BUTTON  (0)
#define FADESPEED (0)

int currLoop=0;
void setup()
{ 
  pinMode(3, OUTPUT);     // sets the digital // pin as output
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}
void loop2() 
{ 
  digitalWrite(3, HIGH);   // turns the LED on
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);
  delay(1000);              // waits for (x)ms
  digitalWrite(3, LOW);     // turns the LED off
  digitalWrite(5, HIGH);    // turns the LED on
  digitalWrite(6, LOW);
  delay(1000);             // waits for (x)ms
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, HIGH);   // turns the LED on
  delay(1000);  
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);    // turns the LED off
  delay(2000);  
}
void loop1() 
{
  int r, g, b;
  // fade from blue to violet
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
}
void loop() 
{
  if (digitalRead(BUTTON)==HIGH)
    currLoop=1-currLoop;
  if (currLoop)
    loop2();
  else
    loop1(); 
}

A couple of items: BUTTON and FADESPEED are not set - I added #defines at the top so it would compile, but you'll have to put the proper values in them.

Right now these two programs are working with two loops non stop, but I want to add a pushBUTTON to select one or the other. tnx

As you progress on this project and learn from the advice given, I would just add a correction to your word useage for future reference. A arduino can only have one active program, it's what's you uploaded from the IDE to the board. However a program (or sketch if you like) can have multiple programmer defined functions, that with proper program structure and control flow can be made to 'switch' as often and for as long as you the programmer desire.

So one board has one running program. Having two programs would require having two boards running different programs.

Make sense?

Lefty

I suspect later you will want to debounce the button, here's the code modified with the debounce example from the Arduino examples folder:

#define REDPIN 3	// LED connected to digital pin 3
#define GREENPIN 5      //LED connected to digital pin 5
#define BLUEPIN 6       //LED connected to digital pin 6

// not in code - where set????
#define BUTTON  (0)
#define FADESPEED (0)

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

int currLoop=0;
void setup()
{ 
  pinMode(3, OUTPUT);     // sets the digital // pin as output
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}
void loop2() 
{ 
  digitalWrite(3, HIGH);   // turns the LED on
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);
  delay(1000);              // waits for (x)ms
  digitalWrite(3, LOW);     // turns the LED off
  digitalWrite(5, HIGH);    // turns the LED on
  digitalWrite(6, LOW);
  delay(1000);             // waits for (x)ms
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, HIGH);   // turns the LED on
  delay(1000);  
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);    // turns the LED off
  delay(2000);  
}
void loop1() 
{
  int r, g, b;
  // fade from blue to violet
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
}
void loop() 
{
  int reading = digitalRead(BUTTON);
  if (reading != lastButtonState) 
    lastDebounceTime = millis();
  if ((millis() - lastDebounceTime) > debounceDelay) 
  {
    buttonState = reading;
    if (buttonState)
      currLoop=1-currLoop;
  }
  if (currLoop)
    loop2();
  else
    loop1(); 
  lastButtonState = reading;  
}

Thanks, yes that's what I'm thinking, changing the functions with different switches for example on/off, fading, I was able to put those two together; now once I finish with this one I will try to combine it with the other one.

Funny pinMode(BUTTON, INPUT) wasn't in the setup()
But is not changing right away, I have to hold the button for a long time to make it change from one function to another.

#define REDPIN 3	// LED connected to digital pin 3
#define GREENPIN 5      //LED connected to digital pin 5
#define BLUEPIN 6       //LED connected to digital pin 6

// not in code - where set????
#define BUTTON  (7)
#define FADESPEED (5)

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

int currLoop=0;
void setup()
{ 
  pinMode(3, OUTPUT);     // sets the digital // pin as output
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  
  pinMode(BUTTON,INPUT);

}
void loop2() 
{ 
  digitalWrite(3, HIGH);   // turns the LED on
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);
  delay(1000);              // waits for (x)ms
  digitalWrite(3, LOW);     // turns the LED off
  digitalWrite(5, HIGH);    // turns the LED on
  digitalWrite(6, LOW);
  delay(1000);             // waits for (x)ms
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, HIGH);   // turns the LED on
  delay(1000);  
  digitalWrite(3, LOW);    // turns the LED off
  digitalWrite(5, LOW);    // turns the LED off
  digitalWrite(6, LOW);    // turns the LED off
  delay(2000);  
}
void loop1() 
{
  int r, g, b;
  // fade from blue to violet
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
}
void loop() 
{
  int reading = digitalRead(BUTTON);
  if (reading != lastButtonState) 
    lastDebounceTime = millis();
  if ((millis() - lastDebounceTime) > debounceDelay) 
  {
    buttonState = reading;
    if (buttonState)
      currLoop=1-currLoop;
  }
  if (currLoop)
    loop2();
  else
    loop1(); 
  lastButtonState = reading;  
}
/code]

The use of delay() in your loop functions is why the program does not respond promptly to a button push. You're going to have to get rid of them, which means you'll need to take a look at the blink without delay example. It wouldn't hurt to search the forums for 'state machine' either.