Toggling LEDs to potentiometer and holding the values

This is my first time working with Arduino, I don't have much programming knowledge beyond basic html. My overall goal for this project is to build a light table with 6 high power LEDs witch can all be controlled individually after being toggled with a push button. The setup would have seven buttons, the first six correspond to their respective LEDs the seventh toggles all of them. Once an LED is toggled its brightness is manipulated by the potentiometer. The LED signals will go through a transistor then to the LEDs themselves. The wiring I understand perfectly its just the coding that I'm having issues with, I'm trying to store the values that the potentiometer gives on the LED when another one is toggled so that it keeps the last value its given. Here is the code that I've come up with, and after two days of searching I can't seem to find a way to store the values through the loop. I'm assuming that I just don't know how to use the code properly. Any advise would be appreciated. I'm using and Arduino Micro for this.

int button1 = 0;
int button2 = 1;
int button3 = 2;
int button4 = 4;
int button5 = 7;
int button6 = 8;
int button7 = 12;
int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 9;
int led5 = 10;
int led6 = 11;
int dimmer = A0;
//Constants that will be modified.
int led1value = 0;
int led2value = 0;
int led3value = 0;
int led4value = 0;
int led5value = 0;
int led6value = 0;
int x = 0;
int pwmvalue =0;
void setup()
{
  Serial.begin(9600);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(button5, INPUT);
  pinMode(button6, INPUT);
  pinMode(button7, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
}
void loop() {
  pwmvalue = map(analogRead(dimmer), 0, 1023, 0, 255);
  //Declaring button press in the form of x=?
  if (digitalRead(button1) == HIGH)
    x=1;
  if (digitalRead(button2) == HIGH)
    x=2;
  if (digitalRead(button3) == HIGH)
    x=3;
  if (digitalRead(button4) == HIGH)
    x=4;
  if (digitalRead(button5) == HIGH)
    x=5;
  if (digitalRead(button6) == HIGH)
    x=6;
  if (digitalRead(button7) == HIGH)
    x=7;
  //Declaring what LED is connected to the PoT
if (x == 1) led1value = pwmvalue;
if (x == 2) led2value = pwmvalue;
if (x == 3) led3value = pwmvalue;
if (x == 4) led4value = pwmvalue;
if (x == 5) led5value = pwmvalue;
if (x == 6) led6value = pwmvalue;
if (x == 7) led1value = pwmvalue,
          led2value = pwmvalue,
          led3value = pwmvalue,
          led4value = pwmvalue,
          led5value = pwmvalue,
          led6value = pwmvalue;
  //Writing the saved ledvalues to LED pins
analogWrite (led1, led1value);
analogWrite (led2, led2value);
analogWrite (led3, led3value);
analogWrite (led4, led4value);
analogWrite (led5, led5value);
analogWrite (led6, led6value);
delay(1);
}

HI, there are a couple of issues.
The first is you are only testing to see if the button is pushed, when you press the next button you still have the previous button set as HIGH, you need to scan the buttons and not only test for buttons HIGH but BUTTONS LOW.

With this bit;

[code]if (x == 1) led1value = pwmvalue;
if (x == 2) led2value = pwmvalue;
if (x == 3) led3value = pwmvalue;
if (x == 4) led4value = pwmvalue;
if (x == 5) led5value = pwmvalue;
if (x == 6) led6value = pwmvalue;
if (x == 7) led1value = pwmvalue,
          led2value = pwmvalue,
          led3value = pwmvalue,
          led4value = pwmvalue,
          led5value = pwmvalue,
          led6value = pwmvalue;

[/code]
You are saving led1value to pwmvalue,
shouldn't you be saving pwmvalue to led1value?
And so on for the other values.
That way you will be storing the pwm value for each channel.

if (x == 7) led1value = pwmvalue,
          led2value = pwmvalue,
          led3value = pwmvalue,
          led4value = pwmvalue,
          led5value = pwmvalue,
          led6value = pwmvalue;

I would comment out these lines to start with, lets just get the individual channel control.

Hope this helps

Tom...... :slight_smile:

The wiring I understand perfectly

So just to check you are using constant current drivers then?

For the constant current source I am using a switching mode regulator, from what I've read I think it should work.
As for the ledvalue integers my thought is that after the press of a button it changes x to the corresponding number staying the same as the code loops until another button is pressed changing x once more. The if statements then check x and set ledvalue to the current pwmvalue (potentiometer value mapped to 0, 255) if it is toggled. You mentioned scanning the buttons, does this not scan on every loop?

if (digitalRead(button1) == HIGH)
    x=1;

Or am I missing something important?
I tried a Switch Case statement but I figured that it might have been best to start with something simple just to make sure it works.

For the constant current source I am using a switching mode regulator, from what I've read I think it should work.

Not too sure what you mean, a switch mode regulator normally delivers a fixed voltage where as a switching LED driver will deliver the constant current. You need one for each LED that is not in series. That is for each LED you need to control interdependently.

am I missing something important?

That code will only light one LED as the higher buttons override the lower ones. Is that what you want?

Shouldn't the ledvalue constants keep the value that was given to them since the last time they were toggled, as they are not in the loop. Say i toggle button 1 witch tells sets x=1, x being equal to 1 then sets led1value to be equal to the map(dimmer, 0, 1023, 0, 255). Shouldn't that value stay equal to the potentiometer until another button is toggled, and when another button is toggled shouldn't it keep the last value it received from the PoT?

Each time through the loop you are setting all the LEDs to the same value irrespective of what button is pressed, so any values stored are wiped out.
Only do an analogue write for the current LED as set by the variable x and not all of them.

So I shouldn't use x as the only variable? Should I have one variable per button?

Should I have one variable per button

No that is fine you are using that variable to indicate what yu are currently using with your pot.
What you want to do is only change the analogue output on the pin that the variable x tells you to.
You can use a case statement for this, or can be clever and use an array.

Here is an updated version of the code.

int button1 = 0;
int button2 = 1;
int button3 = 2;
int button4 = 4;
int button5 = 7;
int button6 = 8;
int button7 = 12;
int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 9;
int led5 = 10;
int led6 = 11;
int dimmer = A0;
//Constants that will be modified.
int led1value = 0;
int led2value = 0;
int led3value = 0;
int led4value = 0;
int led5value = 0;
int led6value = 0;
int x = 0;
int pwmvalue =0;
void setup()
{
  Serial.begin(9600);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(button5, INPUT);
  pinMode(button6, INPUT);
  pinMode(button7, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
}
void loop() {
  pwmvalue = map(analogRead(dimmer), 0, 1023, 0, 255);
  //Declaring button press in the form of x=?
  if (digitalRead(button1) == HIGH)
    x=1;
  if (digitalRead(button2) == HIGH)
    x=2;
  if (digitalRead(button3) == HIGH)
    x=3;
  if (digitalRead(button4) == HIGH)
    x=4;
  if (digitalRead(button5) == HIGH)
    x=5;
  if (digitalRead(button6) == HIGH)
    x=6;
  if (digitalRead(button7) == HIGH)
    x=7;
  //Declaring what LED is connected to the PoT
switch (x) {
  case 1: led1value = pwmvalue;
  break;
  case 2: led2value = pwmvalue;
  break;
  case 3: led3value = pwmvalue;
  break;
  case 4: led4value = pwmvalue;
  break;
  case 5: led5value = pwmvalue;
  break;
  case 6: led6value = pwmvalue;
  break;
  case 7: led1value = pwmvalue;
          led2value = pwmvalue;
          led3value = pwmvalue;
          led4value = pwmvalue;
          led5value = pwmvalue;
          led6value = pwmvalue;
  break;
}
/*if (x == 1) led1value = pwmvalue;
if (x == 2) led2value = pwmvalue;
if (x == 3) led3value = pwmvalue;
if (x == 4) led4value = pwmvalue;
if (x == 5) led5value = pwmvalue;
if (x == 6) led6value = pwmvalue;
if (x == 7) led1value = pwmvalue,
         led2value = pwmvalue,
         led3value = pwmvalue,
         led4value = pwmvalue,
         led5value = pwmvalue,
         led6value = pwmvalue;*/
  //Writing the saved ledvalues to LED pins
analogWrite (led1, led1value);
analogWrite (led2, led2value);
analogWrite (led3, led3value);
analogWrite (led4, led4value);
analogWrite (led5, led5value);
analogWrite (led6, led6value);
delay(1);
}

I've replaced the If statements with a switch case. I have the whole thing on a bread board, right now I'm just using 5mm LEDs to test that I have the code right. Right now I don't have the pulldown resistors for the buttons so they have no connection to ground to avoid a short. So far it seems to be working though every now and then some of the buttons register without being pushed, I'm assuming that it's due to the lack of being pulled down.

Where I think you were going wrong was forgetting that without braces an if statement only performs the next instruction. Unlike python indentation is only cosmetic in C. So in the original code you should have put braces in here:-

if (x == 7) {
          led1value = pwmvalue,
         led2value = pwmvalue,
         led3value = pwmvalue,
         led4value = pwmvalue,
         led5value = pwmvalue,
         led6value = pwmvalue;
  }

Right now I don't have the pulldown resistors for the buttons

You must ALWAYS have a pull up or down resistor on any button input. Pull ups are best because they are free, please read this:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Thank you so much for the help, I figured the random switching was due to the lack of a resistor if I had one I would have used it to test. On the other hand I had no clue that it had built in pullup resistor, a lack of research on my part I guess. Thanks again for the help, it's working perfectly with the pullups. Here is the final code that I'm using, there may still be room for improvement but for now I'm content that it works.

int button1 = 0;
int button2 = 1;
int button3 = 2;
int button4 = 4;
int button5 = 7;
int button6 = 8;
int button7 = 12;
int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 9;
int led5 = 10;
int led6 = 11;
int dimmer = A0;
//Constants that will be modified.
int led1value = 0;
int led2value = 0;
int led3value = 0;
int led4value = 0;
int led5value = 0;
int led6value = 0;
int x = 0;
int pwmvalue =0;
void setup()
{
  Serial.begin(9600);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(button5, INPUT_PULLUP);
  pinMode(button6, INPUT_PULLUP);
  pinMode(button7, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
}
void loop() {
  pwmvalue = map(analogRead(dimmer), 0, 1023, 0, 255);
  //Declaring button press in the form of x=?
  if (digitalRead(button1) == LOW)
    x=1;
  if (digitalRead(button2) == LOW)
    x=2;
  if (digitalRead(button3) == LOW)
    x=3;
  if (digitalRead(button4) == LOW)
    x=4;
  if (digitalRead(button5) == LOW)
    x=5;
  if (digitalRead(button6) == LOW)
    x=6;
  if (digitalRead(button7) == LOW)
    x=7;
  //Declaring what LED is connected to the PoT
switch (x) {
  case 1: led1value = pwmvalue;
  break;
  case 2: led2value = pwmvalue;
  break;
  case 3: led3value = pwmvalue;
  break;
  case 4: led4value = pwmvalue;
  break;
  case 5: led5value = pwmvalue;
  break;
  case 6: led6value = pwmvalue;
  break;
  case 7: led1value = pwmvalue;
          led2value = pwmvalue;
          led3value = pwmvalue;
          led4value = pwmvalue;
          led5value = pwmvalue;
          led6value = pwmvalue;
  break;
}
  //Writing the saved ledvalues to LED pins
analogWrite (led1, led1value);
analogWrite (led2, led2value);
analogWrite (led3, led3value);
analogWrite (led4, led4value);
analogWrite (led5, led5value);
analogWrite (led6, led6value);
delay(1);
}

Once I learn more I'll have to come back and see what I can improve!