Go Down

### Topic: Making a button switch codes? (Read 2369 times)previous topic - next topic

#### Agent_47

##### May 18, 2011, 08:45 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.

#### PaulS

#1
##### May 18, 2011, 09:26 pm
Quote
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.

#### Agent_47

#2
##### May 18, 2011, 10:01 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?

#### Jack Christensen

#3
##### May 18, 2011, 10:10 pm

How do I do that?

I might check out some of the tutorials like this one and this one.

#### AWOL

#4
##### May 18, 2011, 10:11 pm
Quote
When the number is even, do one thing. When it is odd, do the other thing

Code: [Select]
if (myVariable & 1){
// do the odd thing
} else {
//do the even thing
}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### Agent_47

#5
##### May 18, 2011, 11:29 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() {

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 {
{
//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;
}
}
}

#### Aeturnalus

#6
##### May 18, 2011, 11:58 pm
You have:
Code: [Select]

void h2rgb(float H, int &R, int &G, int &B);

It should be:
Code: [Select]

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

#### Agent_47

#7
##### May 19, 2011, 12:55 am
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() {

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 {
{
//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;
}
}
}

#### Aeturnalus

#8
##### May 19, 2011, 01:01 am
You need to write h2rgb - it doesn't exist in your code.

#### Agent_47

#9
##### May 19, 2011, 01:05 amLast Edit: May 19, 2011, 01:38 am by Agent_47 Reason: 1
how do I write h2rgb

and where do I put it I cant figure it out

#### Aeturnalus

#10
##### May 19, 2011, 01:46 am
What does h2rgb do?

#### Agent_47

#11
##### May 19, 2011, 01:50 am
lol i don"t even know, but when I deleted it, the code works. thanks for pointing that out.

#### Agent_47

#12
##### May 19, 2011, 04:45 am
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);
}

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);
{
//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;
}
}}}

#### AWOL

#13
##### May 19, 2011, 08:17 amLast Edit: May 19, 2011, 03:12 pm by AWOL Reason: 1
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### PaulS

#14
##### May 19, 2011, 02:41 pm
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:
Code: [Select]
if(someCondition)
{
if(anotherCondition)
{
// indented code
}
}

is a lot easier to follow than
Code: [Select]
if(someCondition){
if(anotherCondition) {
// indented code
}}

Go Up

Please enter a valid email to subscribe