loading

Arduino 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;
    }
  }
}

oldmicah4 years ago
Not seeing anything major. If I wanted to be persnickety though, if you ever change your interval to anything greater than 1, it could cause numbers less than 0 or greater than 255. I.e. you may want to set your light value with

lightx = min( lightx + interval, 255);
lightx = max( lightx - interval, 0); // may have to use fmax & fmin depending on compiler

Make sense?
bmrodgers3365 (author)  oldmicah4 years ago
You make a good point and thanks for the tip. I figured that I can change the interval to either 3 or 5 so that it would still be between 0 and 255, thus making it easier to control without changing much of the code.
I see no glaring issues so you'll just have to wait till your Arduino comes in and then you can test it.
bmrodgers3365 (author)  mpilchfamily4 years ago
Thanks. I don't think there are any problems either, I just wanted to have a second opinion.