Pages: 1 2 [3]   Go Down
Author Topic: How switch between 2 different codes?  (Read 3593 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I went to past the code back in and noticed an extra set of these "{}" i took those out pasted the codes back in and hooray it works when i turn it on i can control the led with the potentiometer, then i push the button and it auto cycles then i can change it back but i have to push the reset button...
Thanks for all of the help!!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and it auto cycles then i can change it back but i have to push the reset button...
This means that you are not reading the button state in the function that loops. You can either read the button state in that function, or stop the looping. Since the function will be called again when the loop() function is called again, this is probably the better approach. But, only you can decide which is appropriate for you.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I may give that a shot, I dunno, Thanks again for showing infinite patience and helping me though this!!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you mentioned that i could make loop1(), and loop()2 read the button state to make them switch back and forth without pushing the reset button.. what would i need to put in those loops to make them read the button state??

EDIT: I just noticed something kind of interesting...if i replace loop2() with anything else i can switch between them just fine(i tested this by replaceing the code in loop2() with the code that is in loop1() but i changed the potpin to have a different input, and when i did that i was able to push the button and control color with one pot and when i pushed the button again it was controlled with a photoresistor). so apparently there is  aproblem somewhere some how with the code for the color cycle i just have no clue where to even start looking

Code:
const int  buttonPin = 2;    
const 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(){
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin, LOW);
  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);
}
« Last Edit: August 17, 2010, 02:48:55 am by xXAxHillbillyXx » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your loop2 function contains 4 calls to fadeUp and 3 calls to fadeDown, with the default value for delay between steps.

fadeUp has 256 steps, with 10 milliseconds between steps. That will require 2,560 milliseconds to complete.

fadeDown has 256 steps, with 20 milliseconds between steps. That will require 5,120 milliseconds to complete.

Therefore, loop2 will require 25.6 seconds to complete. You are, therefore, only able to read the button press when loop2 completes - once every 25+ seconds.

Have you tried holding the button down that long?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

no i dont think i have held it down that long
EDIT: i just held it down for that long and it still didnt chang back... is this going to be easier to just push the reset button or is there something fairly simple that can be done
« Last Edit: August 17, 2010, 12:00:43 pm by xXAxHillbillyXx » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Since you "need" to use delay, you will probable need to look into using an interrupt to monitor the switch. When the interrupt occurs, simply increment buttonPressCounter and set a global boolean variable (stop) to true. In fadeUp and fadeDown, change the conditional part of the loop to include " && !stop":
Code:
for(i = 255; i >= 0[glow] && !stop[/glow]; i--)

After the interrupt, code resumes executing right where it left off, so, on each pass through the for loops, you need to see if the code should stop.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am not sure how to do what your saying, I dont know what the conditional part of the loop is, i dont know how to set a global boolean variable,..
Logged

Pages: 1 2 [3]   Go Up
Jump to: