Multiple code selection

I was wondering if anyone had like an example code that i could get. It will select between 4 different loops useing a single button switch on pin two.

That should not be too hard to come up with once you have learned the basic programming skills needed to program an Arduino. Not trying to be terse with your request, but as the saying goes, give a man a fish and feed him for a day, teach him to fish and.....

You will find lots of help around here if you at least give it a shot and then post your code if having problems as you learn.

Lefty

i can select between two codes useing the state change detection example sketch, i was able to take the global variables, and the void setups and the void loops and make it work for two.. is it even possible to modify that sketch to work with 4?

Yes.
Look at "switch/case"

would this be easier to do with a rotary switch? like have it hooked up to pins 2,3,4,5 and just have it read something like "if pin2 is high then doop loop1, if pin 3 is high then do loop2..." because the main problem i am haveing is making the sketch read the button state on the second sketch.. i cant get out of it once it starts i have to push the reset button.. i found the sketch on this site and he was haveing the same problem but it never got resolved.

It is very hard to comment on code we can't see (hint)

Here is the code i found on another thread, at the very least i would like to be able to make it come out of loop2 when i push the button, but i would really like to be able to add two more loops to it and be able to select between them with a single button.

int  buttonPin = 2;    
int ledPin = 13;
int ledPin2 = 12;
int rpin = 9;
int gpin = 10;
int bpin = 11;
int potpin = 0;
int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;
float h;                    
int h_int;                   
int r = 0, g = 0, b = 0;         
int val = 0;                   
void h2rgb(float h, int& R, int& G, int& B);  
void fadeUp(int pin, int d = 10)
{
  int i;
  for (i = 255; i >= 0; i--)
  {
    analogWrite(pin, i);
    delay(d);
  }
}
void fadeDown(int pin, int d = 20)
{
  int i;
  for (i = 0; i <= 255; i++)
  {
    analogWrite(pin, i);
    delay(d);
  }
}

void h2rgb(float h, int& R, int& G, int& B) {
  int var_i;
  float S=1, V=1, var_1, var_2, var_3, var_h, var_r, var_g, var_b;
  if ( S == 0 )                       
  {
    R = V * 255;
    G = V * 255;
    B = V * 255;
  }
  else
  {
    var_h = h * 6;
    if ( var_h == 6 ) var_h = 0;      
    var_i = int( var_h ) ;            
    var_1 = V * ( 1 - S );
    var_2 = V * ( 1 - S * ( var_h - var_i ) );
    var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );

    if      ( var_i == 0 ) {
      var_r = V     ;
      var_g = var_3 ;
      var_b = var_1 ;
    }
    else if ( var_i == 1 ) {
      var_r = var_2 ;
      var_g = V     ;
      var_b = var_1 ;
    }
    else if ( var_i == 2 ) {
      var_r = var_1 ;
      var_g = V     ;
      var_b = var_3 ;
    }
    else if ( var_i == 3 ) {
      var_r = var_1 ;
      var_g = var_2 ;
      var_b = V     ;
    }
    else if ( var_i == 4 ) {
      var_r = var_3 ;
      var_g = var_1 ;
      var_b = V     ;
    }
    else                   {
      var_r = V     ;
      var_g = var_1 ;
      var_b = var_2 ;
    }

    R = (1-var_r) * 255;
    G = (1-var_g) * 255;
    B = (1-var_b) * 255;
  }
} 
void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  pinMode(rpin, OUTPUT);
  pinMode(gpin, OUTPUT);
  pinMode(bpin, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  analogWrite(rpin, 255);
  analogWrite(gpin, 255);
  analogWrite(bpin, 255);
}
void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
    } 
    else {
       Serial.println("off"); 
    }
    lastButtonState = buttonState;
  }
  if (buttonPushCounter % 2 == 0) {
    loop1();
  } else {
    loop2();
  }  
}
void loop1(){
  val = analogRead(potpin);   
  h = ((float)val)/1024;       
  h_int = (int) 360*h;         

  h2rgb(h,r,g,b);              
  Serial.print("POT value: ");
  Serial.print(val);           
  Serial.print(" = Hue of ");
  Serial.print(h_int);         
  Serial.print(" degrees. RGB values: ");
  Serial.print(r);             
  Serial.print(" ");
  Serial.print(g);             
  Serial.print(" ");
  Serial.println(b);           
  analogWrite(rpin, r);       
  analogWrite(gpin, g);       
  analogWrite(bpin, b);        
}

void loop2(){
  fadeUp(bpin);
  fadeUp(gpin);
  fadeDown(bpin);
  fadeUp(rpin);
  fadeDown(gpin);
  fadeUp(bpin);
  fadeDown(rpin);
  
}

I suggest getting rid of all those "delay"s before you go any further, otherwise responding to a button-press is going to be tough.

remove these delays.. (they are the only two i saw)

{
  int i;
  for (i = 255; i >= 0; i--)
  {
    analogWrite(pin, i);
    [glow]delay(d);[/glow]
  }
}
void fadeDown(int pin, int d = 20)
{
  int i;
  for (i = 0; i <= 255; i++)
  {
    analogWrite(pin, i);
   [glow] delay(d);[/glow]  }
}

they are the only two i saw

Yes, but look how often they're executed!

If we are to teach NickLee1983 to "fish", I believe that it is best to start at pseudo code.

This is where you write out what you want your program to do step by step. When you do this, try to avoid loops (except for the main one with the Arduino). This helps fault finding later on.

When I post pseudo, I always start it with (pseudo) and end it with (/pseudo), so that everyone knows where it starts and finishes.


To get you started, I believe that this is the way to go:

(pseudo)

Start

If button is being pushed
wait 40mSec

if button is still being pushed
set flag high
wait for button to be released
if flag is high
increment button counter
if counter is more than the maximum value (4)reset counter
clear flag

if counter is 1
(what would be in loop1...)

if counter is 2
(what would be in loop2...)

if counter is 3
(what would be in loop3...)

if counter is 4
(what would be in loop4...)
Back to start

(/pseudo)


This goes through and updates the counter every "loop" (from "start" to "back to start".

The parts "(what would be in loopx...)" only get run if the counter is on the right number.

Notice that I do not take what the button is saying on face value (to avoid Impulse noise and bouncing)

The "flag" is just a Boolean variable.


Does this make sense?


Teach a man to pseudo, and he can code for life. Don't teach him pseudo, and he can't catch fish... or something like that...

:slight_smile:

yes, that makes perfect sense, i will research and find out what to do those thing and get back with you if i have more problems.. THANX!
i would assume that make a counter count to 4 with a button and then reset would be step 1, then add the boolean variables and so on

ok here is what i came up with... it may not be the "right" way but hey... it works it turns on and leds are off push one you select color with pot, push two it auto cycles, push three it goes red, push four it goes green, push five it goes blue, push six it turns off.. YAY

int potpin = 0;
int buttonPin = 2; 
int rpin = 9;       
int gpin = 10;
int bpin = 11;
int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;
float h;                     
int h_int;                   
int r = 0, g = 0, b = 0;          
int val = 0;
int redLEDValue   = 255; 
int greenLEDValue = 1;   
int blueLEDValue = 1;  
int i = 0;                   
void h2rgb(float h, int& R, int& G, int& B);

void h2rgb(float h, int& R, int& G, int& B) {

 
  int var_i;
  float S=1, V=1, var_1, var_2, var_3, var_h, var_r, var_g, var_b;

  if ( S == 0 )                       
  {
    R = V * 255;
    G = V * 255;
    B = V * 255;
  }
  else
  {
    var_h = h * 6;
    if ( var_h == 6 ) var_h = 0;      
    var_i = int( var_h ) ;            
    var_1 = V * ( 1 - S );
    var_2 = V * ( 1 - S * ( var_h - var_i ) );
    var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );

    if      ( var_i == 0 ) {
      var_r = V     ;
      var_g = var_3 ;
      var_b = var_1 ;
    }
    else if ( var_i == 1 ) {
      var_r = var_2 ;
      var_g = V     ;
      var_b = var_1 ;
    }
    else if ( var_i == 2 ) {
      var_r = var_1 ;
      var_g = V     ;
      var_b = var_3 ;
    }
    else if ( var_i == 3 ) {
      var_r = var_1 ;
      var_g = var_2 ;
      var_b = V     ;
    }
    else if ( var_i == 4 ) {
      var_r = var_3 ;
      var_g = var_1 ;
      var_b = V     ;
    }
    else                   {
      var_r = V     ;
      var_g = var_1 ;
      var_b = var_2 ;
    }

    R = (1-var_r) * 255;                  
    G = (1-var_g) * 255;
    B = (1-var_b) * 255;
  }
} 



void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(rpin, OUTPUT);
  pinMode(gpin, OUTPUT);
  pinMode(bpin, OUTPUT);
  Serial.begin(9600);
  
}


void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
    } 
    else {
       Serial.println("off"); 
    }
    lastButtonState = buttonState;
  }
  
  
  if (buttonPushCounter == 1) {
    
    loop4();
    
  }
  if (buttonPushCounter == 2) {
    
    loop5();
    
  }
  if (buttonPushCounter == 3) {
    
    loop1();
    
  }  
  if (buttonPushCounter == 4) {
    
    loop2();
  }
  if (buttonPushCounter == 5) {
    
    loop3();
    
  }
  
  if (buttonPushCounter == 6) {
    
    loop6();
     Serial.println(buttonPushCounter = 0);
  }
}


void loop1() {
  digitalWrite(rpin, HIGH);
  digitalWrite(gpin, LOW);
  digitalWrite(bpin, LOW);
}

void loop2() {
  digitalWrite(gpin, HIGH);
  digitalWrite(rpin, LOW);
  digitalWrite(bpin, LOW);
}

void loop3() {
  digitalWrite(bpin, HIGH);
  digitalWrite(gpin, LOW);
  digitalWrite(rpin, LOW);
}

void loop4() {
  val = analogRead(potpin);   
  h = ((float)val)/1024;      
  h_int = (int) 360*h;         

  h2rgb(h,r,g,b);              

  Serial.print("POT value: ");
  Serial.print(val);           
  Serial.print(" = Hue of ");
  Serial.print(h_int);         
  Serial.print(" degrees. RGB values: ");
  Serial.print(r);             
  Serial.print(" ");
  Serial.print(g);             
  Serial.print(" ");
  Serial.println(b);           

  analogWrite(rpin, r);        
  analogWrite(gpin, g);        
  analogWrite(bpin, b);        
  
}

void loop6(){
  digitalWrite(bpin, LOW);
  digitalWrite(gpin, LOW);
  digitalWrite(rpin, LOW);
}

void loop5()
{
i += 1;      
if (i < 255) 
{
redLEDValue -= 1;
greenLEDValue += 1;
blueLEDValue = 1; 
}
else if (i < 509) 
{
redLEDValue = 1; 
greenLEDValue -= 1; 
blueLEDValue += 1; 
}
else if (i < 763) 
{
redLEDValue += 1; 
greenLEDValue = 1; 
blueLEDValue -= 1; 
}
else 
{
i = 1;
}
analogWrite(rpin,   redLEDValue);   
analogWrite(gpin, greenLEDValue);
analogWrite(bpin,  blueLEDValue);

delay(50); 
}