LED Potentiometer Controlled w/ Push Button

Hello, I am having some trouble with some code for a project I am working on. I am going to connect an LED RGD analog strip w/3 potentiometers controlling the brightness of each R G B color. In addition to controlling the brighting, I want to also put in a push button that will have several presets. I am having problems integrating the code of the push button with the potentiometer controlled LED light.

Button pushed (1)Red. Pushed again (2) Green (3)Blue (4)Purple (5) Yellow (6) White. (7) off

The code is below - and I keep getting errors.

Please help me finish this.

THANK YOU

.

const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;

const int redPotPin = 0;
const int greenPotPin = 1;
const int bluePotPin = 2;

int currentColorValueRed;
int currentColorValueGreen;
int currentColorValueBlue;

int buttonState = 0;
int buttonPin = 8;// must be declared
int counter = 0;

void setup()
{
pinMode ( buttonPin, INPUT); // switch pin input
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

}

void loop()
{
// Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
}
{
if (buttonState == 1);
counter = counter+1;
if (counter == 4)
counter = 0;
}
do while (buttonState == 1){
buttonState = digitalRead (buttonPin);

analogWrite(redPin, currentColorValueRed);
analogWrite(bluePin, currentColorValueBlue);
analogWrite(greenPin, currentColorValueGreen);

do while (buttonState == 1);
buttonState == analogRead (buttonPin);

if (counter == 1) {
analogWrite REDPIN, HIGH);
digitalWrite GREENPIN, LOW);
digitalWrite BLUEPIN = LOW);

if (counter == 1) {
digitalWrite REDPIN, LOW);
digitalWrite GREENPIN, HIGH);
digitalWrite BLUEPIN = LOW);

if (counter == 1) {
digitalWrite REDPIN, LOW);
digitalWrite GREENPIN, LOW);
analogWrite BLUEPIN, HIGH);

if (counter == 1) {
analogWrite REDPIN, HIGH);
digitalWrite GREENPIN, LOW);
analogWrite BLUEPIN = HIGH); // purple

if (counter == 1) {
analogWrite REDPIN, HIGH);
analogWrite GREENPIN, HIGH);
digitalWrite BLUEPIN = LOW); // yellow

if counter == 1) {
analogWrite REDPIN, HIGH);
analogWrite GREENPIN, HIGH);
analogWrite BLUEPIN = HIGH); // white

if (counter == 0 )
digitalWrite REDPIN, LOW);
digitalWrite GREENPIN, LOW);
digitalWrite BLUEPIN = LOW);

}

Why do all your counters look to be equaling 1 and what is missing here?

if counter == 1)

Look into CASE statements, they are better.

When trying to debug a sketch you should concentrate on the first error reported. Often a single mistake will create a cascade of errors and fixing the first one will fix many others as well.

   do while (buttonState == 1);

This is quite novel. I hope, for you sake, that buttonState never is 1. This do-nothing loop will never end.

I thought counter only could be 1 or 0… should I put counter 1 counter 2 counter 3 etc?..

I just get errors and cant figure out how to fix them, I am pretty sure my code is close to what I need.

Just don’t know how to make this code right.

Any thought as to why you closed this off to the rest of the code?

void loop()
{ // start of loop
// Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
  currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) ); 
  currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
  currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
} // end of loop

The rest of your code is never reached, so it will never do anything. Put it in the loop() {…}!

There is a lot wrong with both your code and the logic that it is trying to implement. Take note of the comments already made and fix the obvious problems.

I find it helpful to Auto Format the code as it warns when it can't do it because off too many brackets of one kind or another so I know to look for that kind of typing error.

Once you can Auto Format the code with errors try verifying it, note the errors and start looking for problems. The line highlighted may not have an error but those before may, so concentrate on them.

Once you can verify the code try compiling and uploading it. Does it do what you expect when it runs ? If not then insert some Serial.print statements to output variables at various points. Do the values match what you expect ? Does the program get to all the parts that you expect ?

const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;

const int redPotPin = 0;
const int greenPotPin = 1;
const int bluePotPin = 2;

int currentColorValueRed;
int currentColorValueGreen;
int currentColorValueBlue;

int PedalPress = 0;
int buttonPin = 8;
int Counter = 0;

void setup()

{
pinMode(redPin,OUTPUT);
digitalWrite(redPin,LOW);

pinMode(greenPin,OUTPUT);
digitalWrite(greenPin,LOW);

pinMode(bluePin,OUTPUT);
digitalWrite(bluePin,LOW);

attachInterrupt(0, PedalPress, RISING);
}

void loop()
{
currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
}
{
if (PedalPress == 1);
counter = counter+1;
if (counter == 4)
counter = 0;

do while (PedalPress == 1){
PedalPress = digitalRead (buttonPin);

analogWrite(redPin, currentColorValueRed);
analogWrite(bluePin, currentColorValueBlue);
analogWrite(greenPin, currentColorValueGreen);

do while (buttonState == 1);
buttonState == analogRead (buttonPin);

{

if(Counter == 1)
{
analogWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,LOW);

}

else if(Counter == 2)
{
digitalWrite(redPin,LOW);
digitalWrite(greenPin,HIGH);
digitalWrite(bluePin,LOW);

}

else if(Counter == 3)
{
digitalWrite(redPin,LOW);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,HIGH);

}

else if(Counter == 4)
{
digitalWrite(redPin,LOW);
digitalWrite(greenPin,HIGH);
digitalWrite(bluePin,HIGH);

}

else if(Counter == 5)
{
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,HIGH);
digitalWrite(bluePin,LOW);

}

else
{
digitalWrite(redPin,LOW);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,LOW);

}

if (Counter > 5)
{
Counter = 0;
}

}

void PedalPress()

{
Counter++;
}

Thank you everyone for the help, I am fairly new at code and it can be frustration. I appreciate the feedback and advice

.... am I on the right track now? I just have problems with the push button, integrating it with the code.

  currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
buttonState = digitalRead (buttonPin);

// insert debounce code here for button

  if (buttonState == 1) counter++;
  if (counter == 5) counter = 0;
  
  analogWrite(redPin, currentColorValueRed);
  analogWrite(bluePin, currentColorValueBlue);
  analogWrite(greenPin, currentColorValueGreen);
   

  if(Counter == 1)
  {
    analogWrite(redPin,HIGH);
    digitalWrite(greenPin,LOW);
    digitalWrite(bluePin,LOW);

  }

  else if(Counter == 2)
  {
    digitalWrite(redPin,LOW);
    digitalWrite(greenPin,HIGH);

THIS IS NOT THE FULL CODE

I know your new to programming, but there were just way too many mistakes in there. You can fix the rest.

Did you read and take note of previous comments ?

Fundamentally your whole program consists of this at the moment

void loop()
{
  currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) ); 
  currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
  currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
}

and an interrupt routine that increments a counter that is not referenced in loop()

Does the code Auto Format ? If not, then why ?

This is how I would do it if I wanted to do such a thing:

const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;

const int redPotPin = A0;
const int greenPotPin = A1;
const int bluePotPin = A2;

int buttonState = 0;
int buttonPin = 8;
int Pattern = 0;
const int MaxPattern = 6;

void setup() {
  pinMode(redPin,OUTPUT);
  digitalWrite(redPin,LOW);

  pinMode(greenPin,OUTPUT);
  digitalWrite(greenPin,LOW);

  pinMode(bluePin,OUTPUT);
  digitalWrite(bluePin,LOW);
}

void loop() {
  // Get the crrent button state
  int currentButtonState = digitalRead(buttonPin);
  static int lastButtonState = LOW;
  static unsigned long lastDebounceTime = 0;

  // Note the last time the button changed state
  if (currentButtonState != lastButtonState) {
    lastDebounceTime = millis();
  }

  // Remember the last state
  lastButtonState = currentButtonState; 

  // Has the state been stable for 50 milliseconds?
  if ((millis() - lastDebounceTime) > 50) {
    // Act on the new stable state if it is a change from the last stable state
    if (currentButtonState != buttonState) {
      // We have a new and different stable button state   
      buttonState = currentButtonState;
      if (buttonState == HIGH) {
        // The new state is "Pressed"
        Pattern++;
        if (Pattern > MaxPattern)
          Pattern = 0;
      }
    }
  }


  switch (Pattern) {
  case 0: // Use the pot setting
    analogWrite(redPin, map( analogRead(redPotPin), 0, 1024, 255, 0));
    analogWrite(bluePin, map( analogRead(bluePotPin), 0, 1024, 255, 0));
    analogWrite(greenPin, map( analogRead(greenPotPin), 0, 1024, 255, 0));
    break;

  case 1:  // Red
    digitalWrite(redPin,HIGH);
    digitalWrite(greenPin,LOW);
    digitalWrite(bluePin,LOW);
    break;

  case 2: // Green
    digitalWrite(redPin,LOW);
    digitalWrite(greenPin,HIGH);
    digitalWrite(bluePin,LOW);
    break;

  case 3: // Blue
    digitalWrite(redPin,LOW);
    digitalWrite(greenPin,LOW);
    digitalWrite(bluePin,HIGH);
    break;

  case 4:  // Cyan (Green+Blue)
    digitalWrite(redPin,LOW);
    digitalWrite(greenPin,HIGH);
    digitalWrite(bluePin,HIGH);
    break;

  case 5: // Yellow (Red+Green)
    digitalWrite(redPin,HIGH);
    digitalWrite(greenPin,HIGH);
    digitalWrite(bluePin,LOW);
    break;

  case 6: // OFF
    digitalWrite(redPin,LOW);
    digitalWrite(greenPin,LOW);
    digitalWrite(bluePin,LOW);
    break;

  }
}