Unexpected (for me) lighting problem

Hi all - I have a project I am working on - at the flick of a switch, roughly 30 LEDs are switched between fading through the colours or 1 set colour which is set by 3 pots. I have had a bit of trouble and posted before and so now have checked all the pots and they work fine (when 12v applied, they all go from 0 - 12 as they should)

My query is that when I flick the switch to go to 1 colour, even with the pots disconnected, a colour appears...I am a bit colour-blind, so i couldnt tell you what the colour is :D

Now, to me when the pots are disconnected from the board (power & the signal wires...everything) there is no voltage to the pins (A0-3) and therefore there should be no colour (as if the pots are turned all the way down)

Is my thinking correct? and if so, what could be the problem?

I am working off THIS schematic

No, you are not ;) Same you use a pull up for the switch (or the internal pull up) the input impedance of a analog pin is very high. So a disconnect pin will just pick up all the crap that's in the air and read that as a voltage. Aka, it's kind of a random signal generator (but not really). And because of that a trick most do when using the random() is to seed it with a reading of a unconnected ADC because it's kind of random.

Ah, I see…I have not used the internal pullup on the analog pins…just the switch pin :smiley:

Hopefully that will be the reason…will test it now…in the meantime, this is my code I am using in case it is needed…

int red = 9, grn = 10, blu = 11, sw1 = 2, val = 0, mode = 0;
int inRGB[] = { A0,A1,A2 };
int outRGB[] = { 9,10,11 };
int tempValue = 0;
int brightness = 0;
int fadeAmount = 5;
int state;

const int inMinVal = 0, inMaxVal = 1023;

void setup()
{
  Serial.begin(9600);
  pinMode(red, OUTPUT);
  pinMode(grn, OUTPUT);
  pinMode(blu, OUTPUT);
  pinMode(sw1, INPUT_PULLUP);
  for (int i = 0; i < 3; i++)
  {
    pinMode(inRGB[i], INPUT);
    pinMode(outRGB[i], OUTPUT);
  }
}

void loop() 
{
  state = digitalRead(sw1);
  for (int i = 0; i < 3; i++)
  {
    tempValue = analogRead(inRGB[i]);
    tempValue = map(tempValue, inMinVal, inMaxVal, 0, 255);
  if(state == LOW) analogWrite(outRGB[i], tempValue);
  else Fade();
  }
}

void Fade()
{
  unsigned int rgbColour[3];

  rgbColour[0] = 255;
  rgbColour[1] = 0;
  rgbColour[2] = 0;  

  for (int decColour = 0; decColour < 3; decColour += 1) 
  {
    int incColour = decColour == 2 ? 0 : decColour + 1;

    for(int i = 0; i < 255; i += 1)
    {
      rgbColour[decColour] -= 1;
      rgbColour[incColour] += 1;
      
      setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
      delay(5);
    }
  }
}

void setColourRgb(unsigned int Red, unsigned int green, unsigned int blue)
{
  analogWrite(red, Red);
  analogWrite(grn, green);
  analogWrite(blu, blue);
}

EDIT
Ok, I have chnaged

pinMode(sw1, INPUT_PULLUP);
  for (int i = 0; i < 3; i++)
  {
    pinMode(inRGB[i], INPUT);
    pinMode(outRGB[i], OUTPUT);
  }

to

pinMode(sw1, INPUT_PULLUP);
  for (int i = 0; i < 3; i++)
  {
    pinMode(inRGB[i], INPUT_PULLUP);
    pinMode(outRGB[i], OUTPUT);
  }

and I still get the colour…Im assuming it is whatever colour appears when all LEDs are turned up to the max.
I have now swapped to my nano as I just connected the usb and ext 12v to it by mistake, so Im assuming the effect would be even more prevalent as they are legs, rather than “holes”.

Anyways, I have also discovered that when I put the pots back into the circuit, the green and blue are fine, but the red is VERY minimal…you can see it changing but only very slightly…remove the pots and we are back to full brightness again :smiley:

That's because a pull up/down will mess with the reading so you should not use it ;) How much they mess with the reading depends on the order of magnitude between the pot and the pull resistor but that's why they are not used. And I even believe the IDE turns them off with a analogRead() but I'm not 100% sure...

Simply don't disconnect the pots :) And if you really want them to be off when disconnected put a resistor between the input and GND of at least two orders of magnitude bigger then the pot value. Because that resistor IS in parallel with the pot so makes the curve non-linear. And if that resistor is in the same order of magnitude that non-linearity will be very visible.

PS Why do you use an array for the RGB pins but do you have sw1. If you number it it deserves an array PPS sw is a terrible variable name. Short variable names are easy to mess up at global level. Just call it SwitchPin PPPS It usuals a good thing to remind us of a variable being an array by calling it something in plural. So InRgbPin*s, OutRgbPins* and SwitchPin*s*.

Thanks for the info so far... I am only disconnecting the pots as a troubleshooting thing for the moment...they will be permenantly connected when this gets sorted lol :D I suppose I could have made another array for colour pins, but tbh, there is only 1 switch, val, mode, etc variable so just did them seperately. I know what they all stand for :D cant remember why i put sw1, instead of just switch or something like that

As i say, im leaving pots connected, but when i do, even with them turned down, instead of there being no light, theres still a colour and its confusing me lol