Help with electric guitar lamp project

Hello everyone. I’m a complete Newbie.

I want to make a lamp with an old electric guitar and RGBW led strip. I want the 4 potentiometers of the guitar to control the colors and the switch (push button) to control the modes as so:
pushButtonCounter=0 only the white pot works as a dimmer
pushButtonCounter=1 only the 3 RGB pot works and able to mix the color
pushButtonCounter=2 Red color
pushButtonCounter=3 Green color
pushButtonCounter=4 Blue color
pushButtonCounter=5 RGB color fade cycle mood
pushButtonCounter=6 Return to mode 1

So far everything works except when I put the code in the pushButtonCounter=2 section, the program bugs. below is my code (I blocked part of the coding so the program runs). I’m stuck on this and don’t know what to do…Please help!

int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int whitePin = 6;
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to

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

long int fade_goTime;
int fade_break = 100;

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

fade_goTime = millis();
}

void loop()
{
// 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 went 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 went from on to off:
Serial.println(“off”);
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;

if (buttonPushCounter % 2 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
if (buttonPushCounter == 0){ // Only white potentiometer work
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
whitePin=analogRead(A3);
analogWrite(6, whitePin /4);}

else if (buttonPushCounter == 1){ // Only the 3 RGB potentiometers should work
analogWrite(6, 0); // WORK IF I ONLY PUT THIS LINE
// redPin=analogRead(A0); // WHEN I PUT THIS CODE IN, IT BLOCKS THE PROGRAM
// greenPin=analogRead(A1); // WHEN I PUT THIS CODE IN, IT BLOCKS THE PROGRAM
// bluePin=analogRead(A2); // WHEN I PUT THIS CODE IN, IT BLOCKS THE PROGRAM
// analogWrite(9, redPin /4); // WHEN I PUT THIS CODE IN, IT BLOCKS THE PROGRAM
// analogWrite(10, greenPin /4); // WHEN I PUT THIS CODE IN, IT BLOCKS THE PROGRAM
// analogWrite(11, bluePin /4); // WHEN I PUT THIS CODE IN, IT BLOCKS THE PROGRAM
}
else if (buttonPushCounter == 2){ // RED
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
analogWrite(whitePin, 0 );}
else if (buttonPushCounter == 3){ // GREEN
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
analogWrite(whitePin, 0 );}
else if (buttonPushCounter == 4){ // BLUE
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
analogWrite(whitePin, 0 );}
else if (buttonPushCounter == 6)
buttonPushCounter=0;
else if (buttonPushCounter == 5)

if( millis() >= fade_goTime) fade();
}

void fade()
{

static unsigned int rgbColour[3] = {255,0,0}; //Start on red
static int incColour = 1;
static int decColour = 0;
static int i = -1;

// cross-fade the two colours.
i++;
if(i > 254) {
i=0;
decColour ++;
if(decColour >2) decColour = 0;
if (decColour == 2) incColour = 0;
else incColour = decColour +1;
}

rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;

setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
fade_goTime = millis() + fade_break;
}

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
analogWrite(6, 0);
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

GuitarLamp.ino (3.98 KB)

the program bugs.

Not helpful.

redPin=analogRead(A0)Not sensible.
Think. Do you have a pin 1023? If so,what pinMode did you set for it?

Please remember to use code tags when posting code

Post your code using code blocks otherwise it can't be copied properly.

I really don’t understand why you are reading analog inputs into your pin variables. ’

    whitePin = analogRead(A3);

You should make these constant:

int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int whitePin = 6;

Whenever your counter rolls over back to 0 then you have corrupted your pin variables with analog readings which in all likelihood is confusing the Arduino API when you try to write to some invalid pin # as TheMemberFormerlyKnownAsAWOL has alluded to.

First, I’m sorry if I did not put my code at the right place. I’m not used to Forums…
Second, Thanks for the quick response.

I see that I messed up my code pretty bad. That was my first attempt at it.

I guess I also posted this in the wrong section of the forum.

From your responses, I see where I need to correct it. I will try to modify it tonight and see if it work. and post my corrections.

I did repost the same topic in the Software section instead (where it should be I guess) with the code in the code box. I also inserted a picture of what the final project should look like (put the IRL from my Google photo).

Thanks again :slight_smile:

As to what is the exact bug description:
When I get to the section pushButtonCounter = 2, the pots works fine and I am able to mix the color but when I click again, it does not move to the next section.

From your responses, I guess the wrong pin assign would cause this problem.

It worked!!
Thanks!
below is my corrected code...

const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
const int whitePin = 6;
const int buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// 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
int whitePot;
int redPot;
int greenPot;
int bluePot;
long int fade_goTime;
int fade_break = 100;

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

  fade_goTime = millis();
}

void loop()
{
  // 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 went 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 went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

    if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
if (buttonPushCounter == 0){     // Only white potentiometer work
    analogWrite(redPin, 0);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 0);
    whitePot=analogRead(A3);
    analogWrite(whitePin, whitePot /4);}

else if (buttonPushCounter == 1){   // Only the 3 RGB potentiometers work
    analogWrite(whitePin, 0);       
    redPot=analogRead(A0);        
    greenPot=analogRead(A1);       
    bluePot=analogRead(A2);        
    analogWrite(redPin, redPot /4);     
    analogWrite(greenPin, greenPot /4);   
    analogWrite(bluePin, bluePot /4);   
}
else if (buttonPushCounter == 2){  // RED  
    analogWrite(redPin, 255);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 0);
    analogWrite(whitePin, 0 );}
else if (buttonPushCounter == 3){  // GREEN  
    analogWrite(redPin, 0);
    analogWrite(greenPin, 255);
    analogWrite(bluePin, 0);
    analogWrite(whitePin, 0 );}  
else if (buttonPushCounter == 4){  // BLUE  
    analogWrite(redPin, 0);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 255);
    analogWrite(whitePin, 0 );}  
else if (buttonPushCounter == 6)
    buttonPushCounter=0;
else if (buttonPushCounter == 5) 

  if(  millis() >= fade_goTime) fade();
}

void fade()
{
 
  static unsigned int rgbColour[3] = {255,0,0}; //Start on red
  static int incColour = 1;
  static int decColour = 0;
  static int i = -1;
     
      // cross-fade the two colours.
      i++;
      if(i > 254) {
        i=0;
        decColour ++;
        if(decColour >2) decColour = 0;     
        if (decColour == 2)  incColour = 0;
        else incColour = decColour +1;
      }

       
        rgbColour[decColour] -= 1;
        rgbColour[incColour] += 1;
       
        setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);     
       fade_goTime = millis() + fade_break;       
   }

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
  analogWrite(whitePin, 0);
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}