Offline
Newbie
Karma: 0
Posts: 20
|
 |
« on: May 18, 2011, 01:45:27 pm » |
I was wondering if there is a way to make a button switch from one code to the other. I'm trying to make a lamp that switches from a potentiometer controlling an RGB LED to a rainbow mode that repeats itself when I press the button. Any Ideas that may get me jump started on doing this would be great.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #1 on: May 18, 2011, 02:26:07 pm » |
I was wondering if there is a way to make a button switch from one code to the other. Sure. You know how to connect the switch, right? You know how to read when the switch is pressed, right? Create a variable to count the number of presses. When the number is even, do one thing. When it is odd, do the other thing.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #2 on: May 18, 2011, 03:01:18 pm » |
Create a variable to count the number of presses. When the number is even, do one thing. When it is odd, do the other thing. [/quote]
How do I do that?
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2478
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #3 on: May 18, 2011, 03:10:42 pm » |
How do I do that?
I might check out some of the tutorials like this one and this one.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: May 18, 2011, 03:11:54 pm » |
When the number is even, do one thing. When it is odd, do the other thing if (myVariable & 1){ // do the odd thing } else { //do the even thing }
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #5 on: May 18, 2011, 04:29:46 pm » |
I made this sketch I don't know if its right but i get an error expected '{' at the end of input and highlights "void h2rgb(float H, int &R, int &G, int &B)" - int buttonPin = 2;
int buttonState = 0; int potpin = 3;
int rpin = 9; int gpin = 10; int bpin = 11; 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); // Length of time we spend showing each color const int DISPLAY_TIME = 100; // In milliseconds
void setup() { pinMode(rpin, OUTPUT); pinMode(gpin, OUTPUT); pinMode(bpin, OUTPUT); pinMode(buttonPin, INPUT); Serial.begin(9600); }
void loop() { buttonState = digitalRead(buttonPin); if (buttonState ==HIGH) { // Cycle color from red through to green // (In this loop we move from 100% red, 0% green to 0% red, 100% green) for (g = 0; g <= 255; g+=5) { r = 255-g; analogWrite(gpin, g); analogWrite(rpin, r); delay(DISPLAY_TIME); }
// Cycle color from green through to blue // (In this loop we move from 100% green, 0% blue to 0% green, 100% blue) for (b = 0; b <= 255; b+=5) { g = 255-b; analogWrite(bpin, b); analogWrite(gpin, g); delay(DISPLAY_TIME); }
// Cycle cycle from blue through to red // (In this loop we move from 100% blue, 0% red to 0% blue, 100% red) for (r = 0; r <= 255; r+=5) { b = 255-r; analogWrite(rpin, r); analogWrite(bpin, b); delay(DISPLAY_TIME); } } else { { val=analogRead(potpin); // Read the pin and display the value //Serial.println(val); h = ((float)val)/1024; h_int = (int) 360*h; h2rgb(h,r,g,b); Serial.print("Potentiometer value: "); Serial.print(val); Serial.print(" = Hue of "); Serial.print(h_int); Serial.print("degrees. In RGB this is: "); Serial.print(r); Serial.print(" "); Serial.print(g); Serial.print(" "); Serial.println(b);
analogWrite(rpin, r); analogWrite(gpin, g); analogWrite(bpin, 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 ) //HSV values = 0 ÷ 1 { R = V * 255; G = V * 255; B = V * 255; } else { var_h = H * 6; if ( var_h == 6 ) var_h = 0; //H must be < 1 var_i = int( var_h ) ; //Or ... var_i = floor( 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; //RGB results = 0 ÷ 255 G = (1-var_g) * 255; B = (1-var_b) * 255; } } }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 0
Posts: 360
Arduino rocks
|
 |
« Reply #6 on: May 18, 2011, 04:58:47 pm » |
You have: void h2rgb(float H, int &R, int &G, int &B);
It should be: void h2rgb(float H, int &R, int &G, int &B) {
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #7 on: May 18, 2011, 05:55:39 pm » |
alright I got this but all it says is error compiling undefined reference to h2rgb(float, int&, int&, int&)'
int buttonPin = 2; int buttonState = 0; int potpin = 3;
int rpin = 9; int gpin = 10; int bpin = 11; 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); // Length of time we spend showing each color const int DISPLAY_TIME = 100; // In milliseconds
void setup() { pinMode(rpin, OUTPUT); pinMode(gpin, OUTPUT); pinMode(bpin, OUTPUT); pinMode(buttonPin, INPUT); Serial.begin(9600); }
void loop() { buttonState = digitalRead(buttonPin); if (buttonState ==HIGH) { // Cycle color from red through to green // (In this loop we move from 100% red, 0% green to 0% red, 100% green) for (g = 0; g <= 255; g+=5) { r = 255-g; analogWrite(gpin, g); analogWrite(rpin, r); delay(DISPLAY_TIME); }
// Cycle color from green through to blue // (In this loop we move from 100% green, 0% blue to 0% green, 100% blue) for (b = 0; b <= 255; b+=5) { g = 255-b; analogWrite(bpin, b); analogWrite(gpin, g); delay(DISPLAY_TIME); }
// Cycle cycle from blue through to red // (In this loop we move from 100% blue, 0% red to 0% blue, 100% red) for (r = 0; r <= 255; r+=5) { b = 255-r; analogWrite(rpin, r); analogWrite(bpin, b); delay(DISPLAY_TIME); } } else { { val=analogRead(potpin); // Read the pin and display the value //Serial.println(val); h = ((float)val)/1024; h_int = (int) 360*h; h2rgb(h,r,g,b); Serial.print("Potentiometer value: "); Serial.print(val); Serial.print(" = Hue of "); Serial.print(h_int); Serial.print("degrees. In RGB this is: "); Serial.print(r); Serial.print(" "); Serial.print(g); Serial.print(" "); Serial.println(b);
analogWrite(rpin, r); analogWrite(gpin, g); analogWrite(bpin, 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 ) //HSV values = 0 ÷ 1 { r = V * 255; g = V * 255; b = V * 255; } else { var_h = h * 6; if ( var_h == 6 ) var_h = 0; //H must be < 1 var_i = int( var_h ) ; //Or ... var_i = floor( 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; //RGB results = 0 ÷ 255 g = (1-var_g) * 255; b = (1-var_b) * 255; } } }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 0
Posts: 360
Arduino rocks
|
 |
« Reply #8 on: May 18, 2011, 06:01:38 pm » |
You need to write h2rgb - it doesn't exist in your code.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #9 on: May 18, 2011, 06:05:35 pm » |
how do I write h2rgb
and where do I put it I cant figure it out
|
|
|
|
« Last Edit: May 18, 2011, 06:38:45 pm by Agent_47 »
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 0
Posts: 360
Arduino rocks
|
 |
« Reply #10 on: May 18, 2011, 06:46:09 pm » |
What does h2rgb do?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #11 on: May 18, 2011, 06:50:05 pm » |
lol i don"t even know, but when I deleted it, the code works. thanks for pointing that out.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #12 on: May 18, 2011, 09:45:28 pm » |
I made this sketch it loads fine but I'm not getting a response at all from the chip. What did I do wrong in it.
int buttonPin = 2;
int potpin = 3;
int rpin = 9; int gpin = 10; int bpin = 11; float h; int h_int; int r=0, g=0, b=0;
int val; int val2; int buttonState;
int lightMode = 0;
const int DISPLAY_TIME = 100; // In milliseconds
void setup() { pinMode(rpin, OUTPUT); pinMode(gpin, OUTPUT); pinMode(bpin, OUTPUT); pinMode(buttonPin, INPUT); Serial.begin(9600); buttonState = digitalRead(buttonPin); }
void loop() { val = digitalRead(buttonPin); // read input value and store it in val delay(10); // 10 milliseconds is a good amount of time val2 = digitalRead(buttonPin); // read the input again to check for bounces if (val == val2) { // make sure we got 2 consistant readings! if (val != buttonState) { // the button state has changed! if (val == LOW) { // check if the button is pressed if (lightMode == 0) { // is the light off? lightMode = 1; digitalWrite(rpin, HIGH); digitalWrite(gpin, HIGH); digitalWrite(bpin, HIGH); // Cycle color from red through to green // (In this loop we move from 100% red, 0% green to 0% red, 100% green) for (g = 0; g <= 255; g+=5) { r = 255-g; analogWrite(gpin, g); analogWrite(rpin, r); delay(DISPLAY_TIME); }
// Cycle color from green through to blue // (In this loop we move from 100% green, 0% blue to 0% green, 100% blue) for (b = 0; b <= 255; b+=5) { g = 255-b; analogWrite(bpin, b); analogWrite(gpin, g); delay(DISPLAY_TIME); }
// Cycle cycle from blue through to red // (In this loop we move from 100% blue, 0% red to 0% blue, 100% red) for (r = 0; r <= 255; r+=5) { b = 255-r; analogWrite(rpin, r); analogWrite(bpin, b); delay(DISPLAY_TIME); } }else { lightMode = 0; digitalWrite(rpin, LOW); digitalWrite(gpin, LOW); digitalWrite(bpin, LOW); { val=analogRead(potpin); // Read the pin and display the value //Serial.println(val); h = ((float)val)/1024; h_int = (int) 360*h;
Serial.print("Potentiometer value: "); Serial.print(val); Serial.print(" = Hue of "); Serial.print(h_int); Serial.print("degrees. In RGB this is: "); Serial.print(r); Serial.print(" "); Serial.print(g); Serial.print(" "); Serial.println(b);
analogWrite(rpin, r); analogWrite(gpin, g); analogWrite(bpin, 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 ) //HSV values = 0 ÷ 1 { r = V * 255; g = V * 255; b = V * 255; } else { var_h = h * 6; if ( var_h == 6 ) var_h = 0; //H must be < 1 var_i = int( var_h ) ; //Or ... var_i = floor( 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; //RGB results = 0 ÷ 255 g = (1-var_g) * 255; b = (1-var_b) * 255; } } } buttonState = val; } }}}
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #13 on: May 19, 2011, 01:17:13 am » |
OK, time to learn how to post code so that you don't hospitalise other forum members with sprained scrolling fingers.
When you post some code, open a code box using the # icon on the editor's toolbar, then copy your code into the space between the tags. (you can do this retrospectively, by clicking on "modify" on each of your posts, highlighting the code, then clicking the # icon. (HINT))
Now, back to the problem - what does the serial monitor show you your sketch is doing? We can't see the output.
|
|
|
|
« Last Edit: May 19, 2011, 08:12:32 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #14 on: May 19, 2011, 07:41:00 am » |
You have way too much code for now. You are not enabling the internal pull-up resistors. This implies that you have an external resistor connected with the switch. Is this true? Exactly how is the switch connected? You need to learn WHEN to use { and } and when not to. You have a bunch of useless { and }. For a beginner, which you clearly are, it is a LOT easier to see blocks of code if you put each { and } on a line by themselves, and properly indent the code in between: if(someCondition) { if(anotherCondition) { // indented code } } is a lot easier to follow than if(someCondition){ if(anotherCondition) { // indented code }}
|
|
|
|
|
Logged
|
|
|
|
|
|