0

I'm pretty new to Arduino and this is my first project I'm working on and I'm really struggling with why my code isn't working properly.

The LED strip should be able to adjust the colour with one potentiometer that I can take out to so I don't use 3 potentiometers and just use that can adjust all colours. Then once it's on mode 4 it should keep the colour and blink repeatedly.

I'm using an Arduino Uno but once my project if finished I'll use the Arduino Nano. I don't know if I'll need to change the code but if I do I think I could do that on my own.

I just don't know why my LED strip is always stuck on off (255).

// set pin numbers:
int ledPinR = 11; // the number of the LED R pin
int ledPinG = 10; // the number of the LED G pin
int ledPinB = 9; // the number of the LED B pin
int buttonPin = 7; // the number of the push button pin
int mode = 0; //to change the mode
int bluelev = 0; //save the blue led value
int greenlev = 0; //save thegreen led value
int redlev = 0; // save the red led value

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);

  //make the led strip white at boot
  digitalWrite(ledPinR, 0);
  digitalWrite(ledPinG, 0);
  digitalWrite(ledPinB, 0);
}

void loop() {
  // when the button is pressed changes the mode value by one
  if (digitalRead(buttonPin) == LOW) {
    ++mode;
    delay(500);
  }

  if (mode == 1) { //red config
    int adcValue; // Define a variable to save the ADC value
    // Convert analog of A0 port into digital, and work as PWM duty cycle of ledPinR port
    adcValue = analogRead(A0);
    analogWrite(ledPinR, map(adcValue, 0, 1023, 0, 255));
    (redlev = ledPinR); // saves the red led level
  }

  if (mode == 2) { //green config
    int adcValue; // Define a variable to save the ADC value
    // Convert analog of A1 port into digital, and work as PWM duty cycle of ledPinG port
    adcValue = analogRead(A1);
    analogWrite(ledPinG, map(adcValue, 0, 1023, 0, 255));
    (greenlev = ledPinG); // saves the green led level
  }

  if (mode == 3) { //Blue config
    int adcValue; // Define a variable to save the ADC value
    // Convert analog of A2 port into digital, and work as PWM duty cycle of ledPinB port
    adcValue = analogRead(A2);
    analogWrite(ledPinB, map(adcValue, 0, 1023, 0, 255));
    (bluelev = ledPinB); // saves the blue led level
  }

  if (mode == 4) {
    digitalWrite(ledPinR, 255); // switch off LED
    digitalWrite(ledPinG, 255); // switch off LED
    digitalWrite(ledPinB, 255); // switch off LED
    digitalWrite(8, 0); // switch on LED
    delay(1000); //wait 1 second
    digitalWrite(ledPinR, redlev); // switch on LED
    digitalWrite(ledPinG, greenlev); // switch on LED
    digitalWrite(ledPinB, bluelev); // switch on LED
    digitalWrite(8, 225); // switch on LED
    delay(1000); //wait 1 second
  }

  if (mode == 5) {
    mode -= 5; //sets the mode value back to 0
  }
}

my circuit

Here is the setup.

Pretend the RGB LED is a LED strip and the potentiometers are just slots for one to be put in to change the colour when I want to. So when I'm not changing the colours I can use the potentiometer for other projects and not waste 3.

2 Answers 2

1

There are at least these errors in your code:

1. You cannot read back the analog value set to a pin.

You try this:

    analogWrite(ledPinR, map(adcValue, 0, 1023, 0, 255));
    (redlev = ledPinR); // saves the red led level

You correctly set the analog value at the pin for the red LED. But then you try to read it back. Look at the definition of ledPinR: It is an int with the fixed value of 11. So redlev is always 11.

Note 1: The output is not really analog, but pulse width modulated.

Note 2: You don't need the parentheses in the second statement.

Instead, store the mapped value in redlev:

    redlev = map(adcValue, 0, 1023, 0, 255);
    analogWrite(ledPinR, redlev);

Note 3: Use good variable names, like redLevel. Some time or later you will confuse things because of abbreviated names.

2. You are not repeating the analog write in mode4.

You wrote this, which is apparently wrong:

  if (mode == 4) {
    digitalWrite(ledPinR, 255); // switch off LED
    // other statements...
    delay(1000); //wait 1 second
    digitalWrite(ledPinR, redlev); // switch on LED
    // other statements...
    delay(1000); //wait 1 second
  }

Instead you want this (if 255 is really "off"):

  if (mode == 4) {
    analogWrite(ledPinR, 255); // switch off LED
    // other statements...
    delay(1000); //wait 1 second
    analogWrite(ledPinR, redlev); // switch on LED
    // other statements...
    delay(1000); //wait 1 second
  }

There are more issues, but this will take you a step forward. Most important, keep an indentation style, I cleaned up your source in the question to make it readable.

2
  • thank you so much
    – user92341
    Sep 11 at 15:23
  • You're welcome! BTW, you are supposed to mark the answer that fits you best as "accepted" instead of commenting a "thanks"... Sep 11 at 15:25
-1

My first project was also the same, to set the strip color with one potentiometer. I used the function getColorValue, that returns you a Struct. This struct contains the value for red, green, and blue. Here is the example code:

#define PIN_POTENTIOMETER A0

struct RGB
{
    uint8_t red;
    uint8_t green;
    uint8_t blue;
} color;

RGB getColorValue(uint16_t inputValue);

void setup()
{
    Serial.begin(9600);
    pinMode(PIN_POTENTIOMETER, INPUT);
}

void loop()
{
    uint16_t inputValue = analogRead(PIN_POTENTIOMETER);
    color = getColorValue(inputValue);

    Serial.print("R: ");
    Serial.print(color.red);
    Serial.print(" G: ");
    Serial.print(color.green);
    Serial.print(" B: ");
    Serial.println(color.blue);
    delay(100);
}

RGB getColorValue(uint16_t inputValue)
{
    RGB color;
    color.red = 0;
    color.green = 0;
    color.blue = 0;

    map(inputValue, 0, 1023, 0, 255);

    if (inputValue <= 42.5)
    {
        color.red = 255;
        color.green = inputValue * 6;
        color.blue = 0;
    }
    else if (inputValue <= 85)
    {
        color.red = 255 - (inputValue - 43) * 6;
        color.green = 255;
        color.blue = 0;
    }
    else if (inputValue <= 127.5)
    {
        color.red = 0;
        color.green = 255;
        color.blue = (inputValue - 85) * 6;
    }
    else if (inputValue <= 170)
    {
        color.red = 0;
        color.green = 255 - (inputValue - 127.5) * 6;
        color.blue = 255;
    }
    else if (inputValue <= 212.5)
    {
        color.red = (inputValue - 170) * 6;
        color.green = 0;
        color.blue = 255;
    }
    else if (inputValue <= 255)
    {
        color.red = 255;
        color.green = 0;
        color.blue = 255 - (inputValue - 212.5) * 6;
    }

    return color;
}

If you need any further help, you can comment here or contact me directly with my mail in the bio.

Regards Dario

3
  • analogWrite and analogRead doesn't require pinMode.
    – Juraj
    Sep 11 at 13:08
  • @Juraj You are right, my fault. Sep 11 at 13:21
  • thank you so much
    – user92341
    Sep 11 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.