Making a button switch codes?

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.

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.

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?

Agent_47:
How do I do that?

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

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
}

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

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) {

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

You need to write h2rgb - it doesn't exist in your code.

how do I write h2rgb

and where do I put it I cant figure it out

What does h2rgb do?

lol i don"t even know, but when I deleted it, the code works. thanks for pointing that out.

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

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.

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
}}

AWOL:
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))

Sorry about that I was trying to figure that out but couldn't find it. thanks for the tip.

some of my files were deleted and tried to figure out my sketch but got lost so I'm starting fresh. with the original codes I used.

Here is my 3 codes, 1 is for basic button setup, the second is the controlling rgb with potentiometer, and third is just automatic rainbow fade.

I tried last time mixing them together and switching some of the words to match like GREEN_LED_PIN to gpin, or is doing that wrong.

Yes I am new and a complete noob to writing sketches, but I want to learn really bad and I really appreciate the help and patience you all give me.

What do I do exactly to get started here?

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;
}

second

int potpin = 2;              // Switch connected to digital pin 2

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);

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
}


void loop()                     // run over and over again
{
  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;
  }
}

Third

// LED leads connected to PWM pins
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;

// Used to store the current intensity level of the individual LEDs
int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

// Length of time we spend showing each color
const int DISPLAY_TIME = 100; // In milliseconds


void setup() {
  // No setup required.
}

void loop() {
  // Cycle color from red through to green
  // (In this loop we move from 100% red, 0% green to 0% red, 100% green)
  for (greenIntensity = 0; greenIntensity <= 255; greenIntensity+=5) {
        redIntensity = 255-greenIntensity;
        analogWrite(GREEN_LED_PIN, greenIntensity);
        analogWrite(RED_LED_PIN, redIntensity);
        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 (blueIntensity = 0; blueIntensity <= 255; blueIntensity+=5) {
        greenIntensity = 255-blueIntensity;
        analogWrite(BLUE_LED_PIN, blueIntensity);
        analogWrite(GREEN_LED_PIN, greenIntensity);
        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 (redIntensity = 0; redIntensity <= 255; redIntensity+=5) {
        blueIntensity = 255-redIntensity;
        analogWrite(RED_LED_PIN, redIntensity);
        analogWrite(BLUE_LED_PIN, blueIntensity);
        delay(DISPLAY_TIME);
  }
}