loading

Arduino light dimmer code?

I am creating a 6 channel light dimmer system using an arduino duemilanove. I want to control it using push buttons to  dim and brighten. I want to setup 6 buttons to corresponding to the 6 channels. Then I want an up, down, and on/off button. When you hold down a channel button, it activates to up and down button to control that channel. I want the on/off button to turn all channels to full brightness or completely off. I am basing the dimming part of the circuit off of this instructable: https://www.instructables.com/id/Arduino-Powered-Musical-Christmas-Lights/. Anyway, I have ordered an arduino but want someone to check out my code before I build this. I have made many things but am new to writing my own code so I would appreciate any improvements and friendly advice. 

Here is the code:

int light1pin=3;
int light2pin=5;
int light3pin=6;
int light4pin=9;
int light5pin=10;
int light6pin=11;
int button1pin=0;
int button2pin=1;
int button3pin=2;
int button4pin=4;
int button5pin=7;
int button6pin=8;
int uppin=12;
int downpin=13;
int onoffpin=14;
int onoff=0;
int light1=255;
int light2=255;
int light3=255;
int light4=255;
int light5=255;
int light6=255;
int interval=1;

void setup() {
  pinMode(light1pin, OUTPUT);
  pinMode(light2pin, OUTPUT);
  pinMode(light3pin, OUTPUT);
  pinMode(light4pin, OUTPUT);
  pinMode(light5pin, OUTPUT);
  pinMode(light6pin, OUTPUT);
  pinMode(button1pin, INPUT);
  pinMode(button2pin, INPUT);
  pinMode(button3pin, INPUT);
  pinMode(button4pin, INPUT);
  pinMode(button5pin, INPUT);
  pinMode(button6pin, INPUT);
  pinMode(uppin, INPUT);
  pinMode(downpin, INPUT);
  pinMode(onoffpin, INPUT);
}

void loop() {
  analogWrite(light1pin, light1);
  analogWrite(light2pin, light2);
  analogWrite(light3pin, light3);
  analogWrite(light4pin, light4);
  analogWrite(light5pin, light5);
  analogWrite(light6pin, light6);
  if (digitalRead(onoffpin) == HIGH && onoff == 0){
    light1=255;
    light2=255;
    light3=255;
    light4=255;
    light5=255;
    light6=255;
    onoff=1;
  }
  if (digitalRead(onoffpin) ==  HIGH && onoff == 1) {
    light1=0;
    light2=0;
    light3=0;
    light4=0;
    light5=0;
    light6=0;
    onoff=0;
  }
  if (digitalRead(button1pin) == HIGH) {
    if (digitalRead(uppin) == HIGH && light1 < 255){
      light1 = light1 + interval;
    }
    if (digitalRead(downpin) == HIGH && light1 > 0){
      light1 = light1 - interval;
    }
  }
  if (digitalRead(button2pin) == HIGH) {
    if (digitalRead(uppin) == HIGH && light2 < 255){
      light2 = light2 + interval;
    }
    if (digitalRead(downpin) == HIGH && light2 > 0){
      light2 = light2 - interval;
    }
  }
  if (digitalRead(button3pin) == HIGH) {
    if (digitalRead(uppin) == HIGH && light3 < 255){
      light3 = light3 + interval;
    }
    if (digitalRead(downpin) == HIGH && light3 > 0){
      light3 = light3 - interval;
    }
  }
  if (digitalRead(button4pin) == HIGH) {
    if (digitalRead(uppin) == HIGH && light4 < 255){
      light4 = light4 + interval;
    }
    if (digitalRead(downpin) == HIGH && light4 > 0){
      light4 = light4 - interval;
    }
  }
  if (digitalRead(button5pin) == HIGH) {
    if (digitalRead(uppin) == HIGH && light5 < 255){
      light5 = light5 + interval;
    }
    if (digitalRead(downpin) == HIGH && light5 > 0){
      light5 = light5 - interval;
    }
  }
  if (digitalRead(button6pin) == HIGH) {
    if (digitalRead(uppin) == HIGH && light6 < 255){
      light6 = light6 + interval;
    }
    if (digitalRead(downpin) == HIGH && light6 > 0){
      light6 = light6 - interval;
    }
  }
}

Its OK, as far as I can tell, from a completely uncommented program.

You need to think about what is happening WHEN the button is pressed down. What happens to the counters ?

Also, you'll probably find 255 levels is too many for practical use. Drop it to 64 or fewer, and shift the control word like this

Brightness= control_word << 2;
bmrodgers3365 (author)  steveastrouk4 years ago
Thanks for the help. I realize that having 255 levels is a lot. I am going to change the interval at which the levels move to either 3 or 5 giving me either 85 or 51 levels. I am also planning on publishing an instructable on this project so I will add many comments to my program.