Unexpected behavior in my RGB-strip driver code

62 views Asked by At

I'm getting wrong output on the pins 9, 10 and 11. They are meant to be inputs for my RGB strip driver circuit. Which is basically an array of NPN transistors pulling current from the strip.

The basic idea is to get only 2 controls to set R, G, B and brightness. I'm using a button and a potenciometer. Potenciometer is used to set the value and the button to skip to next value setting. There is one setting state which is like the default one. It is the one to set the brightness and I will be using this most of the time. The othe ones are for setting the colors and when in one of those setting the strip will be blinking in between only the color I'm currently setting and the result with alle three colors set. The whole code was working just fine until I added the brightness setting and I think that I am kinda lost in my own code.

I even added a serial to read the outputs but I don't understand why are the numbers the way they are :(

  int pinR = 9;
  int pinG = 10;
  int pinB = 11;
  int potPin = A0;
  const int buttonPin = 2;

  int brightR = 0;
  int brightG = 0;
  int brightB = 0;
  int brightness = 50; //

  int R;
  int G;
  int B;

  int potValue = 0;

  int blinky = 0;
  boolean blinking = false;

  int buttonState;
  int lastButtonState = LOW;
  long lastDebounceTime = 0;
  long debounceDelay = 50;

  int setting = 0; //0=R 1=G 2=B 3=Brightness

  void setup() {
    // put your setup code here, to run once:
    pinMode(pinR, OUTPUT);
    pinMode(pinG, OUTPUT);
    pinMode(pinB, OUTPUT);

    Serial.begin(9600);
  }

  void RGBset(int r, int g, int b){
      analogWrite(pinR, r);
      analogWrite(pinG, g);
      analogWrite(pinB, b);
    }

  void loop() {
    // put your main code here, to run repeatedly:
    potValue = analogRead(potPin);
    potValue = map(potValue, 0, 1023, 0, 255); //read pot --> map to values from 0 - 255

    int reading = digitalRead(buttonPin);

    if (reading != lastButtonState) {

      lastDebounceTime = millis();
    }

    if ((millis() - lastDebounceTime) > debounceDelay) {

      if (reading != buttonState) {
        buttonState = reading;

        if (buttonState == HIGH) {
          setting++;
        }
      }
    }

    lastButtonState = reading;

    if(setting > 3){  // 0=R 1=G 2=B 3=Brightness
      setting = 0;    // cant get 4 cause there is no 4
    }

    if(setting == 0){
      brightR = potValue;
      if(blinking){
        RGBset(brightR, brightG, brightB);
      }else{
        RGBset(brightR, 0, 0);
      }
    }

    if(setting == 1){
      brightG = potValue;
      if(blinking){
        RGBset(brightR, brightG, brightB);
      }else{
        RGBset(0, brightG, 0);
      }
    }

    if(setting == 2){
      brightB = potValue;
      if(blinking){
        RGBset(brightR, brightG, brightB);
      }else{
        RGBset(0, 0, brightB);
      }
    }

    if(setting == 3){
      brightness = potValue;
      brightness = map(brightness, 0, 255, 1, 100);  //mapping brightness to values from 1 - 100
      R = brightR * brightness / 100;                //set value * brightness / 100 
      G = brightG * brightness / 100;                //that leads to get % of set value
      B = brightB * brightness / 100;                //255 * 50 / 100 = 127,5 ==> 128
      RGBset(R, G, B);                               //it wont blink in thiss setting
    }

    if(setting != 3){
      blinky++;
      if(blinky > 1000){
        blinking = !blinking;
        blinky = 0;
      }
    }

    String output = (String(brightR) + " " + String(R) + " " + String(brightG) + " " + String(G) + " " + String(brightB) + " " + String(B) + " " + String(brightness) + " " + String(potValue) + " " + String(blinking)); 
    Serial.println(output);

    delay(1);
  }
1

There are 1 answers

0
jabujavi On BEST ANSWER

First, in setup:

pinMode(buttonPin , INPUT);

Second, what are you expected when setting==3? You aren't reloading/updating the variables for brightR brightG brightB. So, when you change setting, you will lost the change of brightness