New to C++ and Arduino. I cannot get these two codes to work together.

I have tried to fix this issue but i have yet to come to a logical conclusion. For some reason when i push the button the RGB’s blink. They do come on and off, but do not stay off. I don’t know if it is because of the delay feature or if it is the analog output. Please help me if i can’t figure this out i won’t have hair. what i am trying to accomplish is to simply turn on and off two rgb’s that go through a color spectrum. simple enough but i believe that i am going about it all wrong.

thank you,
Paul H.

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int redPin = 3;       // the pin that the LED is attached to
const int greenPin = 5;
const int bluePin = 6;
int counter = 0;

int numColors = 255;

int animationDelay = 400;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
 // This part takes care of displaying the
 // color changing in reverse by counting backwards if counter
 // is above the number of available colors  
 float colorNumber = counter > numColors ? counter - numColors: counter;
 
 // Play with the saturation and brightness values
 // to see what they do
 float saturation = .05; // Between 0 and 1 (0 = gray, 1 = full color)
 float brightness = .06; // Between 0 and 1 (0 = dark, 1 is full brightness)
 float hue = (colorNumber / float(numColors)) * 360; // Number between 0 and 360
 long color = HSBtoRGB(hue, saturation, brightness); 
 
 // Get the red, blue and green parts from generated color
 int red = color >> 16 & 255;
 int green = color >> 8 & 255;
 int blue = color & 255;

 setColor(red, green, blue);
   // Counter can never be greater then 2 times the number of available colors
 // the colorNumber = line above takes care of counting backwards (nicely looping animation)
 // when counter is larger then the number of available colors
 counter = (counter + 1) % (numColors * 2);
 
 // If you uncomment this line the color changing starts from the
 // beginning when it reaches the end (animation only plays forward)
 // counter = (counter + 1) % (numColors);

 delay(animationDelay);
}

void setColor (unsigned char red, unsigned char green, unsigned char blue) 
{        
   analogWrite(redPin, red);
   analogWrite(greenPin, green);
   analogWrite(bluePin, blue);
} 

long HSBtoRGB(float _hue, float _sat, float _brightness) {
   float red = 0.0;
   float green = 0.0;
   float blue = 0.0;
   
   if (_sat == 0.0) {
       red = _brightness;
       green = _brightness;
       blue = _brightness;
   } else {
       if (_hue == 360.0) {
           _hue = 0;
       }

       int slice = _hue / 60.0;
       float hue_frac = (_hue / 60.0) - slice;

       float aa = _brightness * (1.0 - _sat);
       float bb = _brightness * (1.0 - _sat * hue_frac);
       float cc = _brightness * (1.0 - _sat * (1.0 - hue_frac));
       
       switch(slice) {
           case 0:
               red = _brightness;
               green = cc;
               blue = aa;
               break;
           case 1:
               red = bb;
               green = _brightness;
               blue = aa;
               break;
           case 2:
               red = aa;
               green = _brightness;
               blue = cc;
               break;
           case 3:
               red = aa;
               green = bb;
               blue = _brightness;
               break;
           case 4:
               red = cc;
               green = aa;
               blue = _brightness;
               break;
           case 5:
               red = _brightness;
               green = aa;
               blue = bb;
               break;
           default:
               red = 0.0;
               green = 0.0;
               blue = 0.0;
               break;
       }
   }

   long ired = red * 255.0;
   long igreen = green * 255.0;
   long iblue = blue * 255.0;
   
   return long((ired << 16) | (igreen << 8) | (iblue));
  
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
  
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, HIGH);
  } else {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, HIGH);
  }

}

Code_One.ino (798 Bytes)

I only see one code there. What's the problem you're having with it?

sorry I was trying to upload the two codes but messed that up. this is my first post on here. the rgbs do not stay completely off. i don't think it is the wiring for the simple fact that i can use the code posted as an attachment and the example code from the arduino program(statechangedetection) works. The whole code that i frankenstined was the one that i posted not as an attachment. That code will go through a color spectrum and turn off but blink in the off mode.

 float colorNumber = counter > numColors ? counter - numColors: counter;

Compare and int to an int, and select the appropriate int to return. Store that int in a float. Why?

Are you expecting somehow, to select color number 3.7?

How IS your switch wired? You are not using the internal pullup resistor, so you MUST have an external resistor.

I do have a external 10kohm resistor going from the micro switch to the negative lead.

I do have a external 10kohm resistor going from the micro switch to the negative lead.

The negative lead of what? We need pictures, not vague hand-waving.

Wiring the switch using the internal pullup resistor CAN be described without a picture. Ground one leg. Connect the other leg to a digital pin. Why are you NOT using the internal pullup resistor? Why are you using more hardware than needed, and more complicated wiring than needed?

One thing - why on earth is code to read a pushbutton inside a function named HSBtoRGB ? I think we must be missing a couple of lines.

sorry I was trying to upload the two codes but messed that up.

Ok. So where's the actual code that you are uploading to the board, which is doing this?