Controlling multiple light strips with pot and momentary switch

Hello all,

I am a beginner arduino programmer (but definitely not a noob). I have been tasked with a project to be able to turn on or off 4 12v LED light strips and also independently fade each of the light strips. I have the code I have posted below working for one individual strip and have scaled the code four all four strips. I have a couple questions. Maybe someone can point me in the right direction.

  1. Have I scaled this code to all four light strips in an efficient manner (can the code be reduced or written to be more effective)

  2. How would I go about coding another momentary switch that will put each of the four light strips into a blink action (I know how to code the blinking). I'm thinking that I need to do another "state change detection". Am I on the right path?

anyway here is my current code:

const int buttonPin1  = 2; // the number of the button pin
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 7;

const int transistorPin1 = 5; // the number of digital transistor pin
const int transistorPin2 = 6;
const int transistorPin3 = 9;
const int transistorPin4 = 10;

// variables will change:
int buttonPushCounter = 0;
int buttonState = 0; // variable for reading the pushbutton status
int lastButtonState = 0;
int potPin1 = A0; // select the input pin for the potentiometer
int potPin2 = A1;
int potPin3 = A2;
int potPin4 = A3;
int val = 0; // variable to store the value coming from the sensor

void setup() {
pinMode(transistorPin1, OUTPUT); // declare the ledPin as an OUTPUT
pinMode (potPin1, OUTPUT);
pinMode (buttonPin1, INPUT);

pinMode(transistorPin2, OUTPUT); // declare the ledPin as an OUTPUT
pinMode (potPin2, OUTPUT);
pinMode (buttonPin3, INPUT);

pinMode(transistorPin3, OUTPUT); // declare the ledPin as an OUTPUT
pinMode (potPin3, OUTPUT);
pinMode (buttonPin3, INPUT);

pinMode(transistorPin4, OUTPUT); // declare the ledPin as an OUTPUT
pinMode (potPin4, OUTPUT);
pinMode (buttonPin4, INPUT);

Serial.begin(9600);
}

void loop(){
buttonState = digitalRead (buttonPin1);

if (buttonState != lastButtonState){
if (buttonState == HIGH){
buttonPushCounter++;
Serial.println ("on");
Serial.println ("number of button pushes: ");
Serial.println (buttonPushCounter);
}
else {
Serial.println ("off");
}
}

lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0) {
digitalWrite(transistorPin1, HIGH);
}
else
{
   // read the potentiometer:
   int sensorValue = analogRead(A0);
   // map the sensor value to a range from 0 - 255:
   int outputValue = map(sensorValue, 0, 1023, 0, 255);
   // use that to control the transistor:
   analogWrite(transistorPin1, outputValue);
 }

buttonState = digitalRead (buttonPin2);

if (buttonState != lastButtonState){
if (buttonState == HIGH){
buttonPushCounter++;
Serial.println ("on");
Serial.println ("number of button pushes: ");
Serial.println (buttonPushCounter);
}
else {
Serial.println ("off");
}
}

lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0) {
digitalWrite(transistorPin2, HIGH);
}
else
{
   // read the potentiometer:
   int sensorValue = analogRead(A1);
   // map the sensor value to a range from 0 - 255:
   int outputValue = map(sensorValue, 0, 1023, 0, 255);
   // use that to control the transistor:
   analogWrite(transistorPin2, outputValue);
 } 
 buttonState = digitalRead (buttonPin3);

if (buttonState != lastButtonState){
if (buttonState == HIGH){
buttonPushCounter++;
Serial.println ("on");
Serial.println ("number of button pushes: ");
Serial.println (buttonPushCounter);
}
else {
Serial.println ("off");
}
}

lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0) {
digitalWrite(transistorPin3, HIGH);
}
else
{
   // read the potentiometer:
   int sensorValue = analogRead(A2);
   // map the sensor value to a range from 0 - 255:
   int outputValue = map(sensorValue, 0, 1023, 0, 255);
   // use that to control the transistor:
   analogWrite(transistorPin3, outputValue);
 }
 buttonState = digitalRead (buttonPin4);

if (buttonState != lastButtonState){
if (buttonState == HIGH){
buttonPushCounter++;
Serial.println ("on");
Serial.println ("number of button pushes: ");
Serial.println (buttonPushCounter);
}
else {
Serial.println ("off");
}
}

lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0) {
digitalWrite(transistorPin4, HIGH);
}
else
{
   // read the potentiometer:
   int sensorValue = analogRead(A3);
   // map the sensor value to a range from 0 - 255:
   int outputValue = map(sensorValue, 0, 1023, 0, 255);
   // use that to control the transistor:
   analogWrite(transistorPin4, outputValue);
 }
}

Have I scaled this code to all four light strips in an efficient manner

No not at all. The whole thing could be 10 times shorter if you use arrays to define pins.
See:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Thanks for the tip Grumpy_Mike

Hello all,

Rather than start a new forum question I have made changes to the above project. I have printed a board to hold all of my transistors, pushbuttons and pots for each led strip designated by the code I will post shortly. When I have this code for one led strip it works fine, however when I have all 4 led strips (5th combo currently not used) hooked up using this code, when I hit the pushbutton the led’s flash a little bit and seem to miss the on off function every now and then. I figured that I had an issue that could be solved with debouncing, but this isn’t working.

My first question is: Have I properly debounced the pushbuttons with the bounce library? Second, if the debouncing code is correct, do I have to play with the timing of the debounce function? Third, is the issue something other than what could be solved by debouncing the pushbuttons?

Here is the current version of the code that I have:

#include <Bounce.h>

const int buttonPin1  = 2; // the number of the button pin
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 7;
const int buttonPin5 = 8;

const int transistorPin1 = 5; // the number of digital transistor pin
const int transistorPin2 = 6;
const int transistorPin3 = 9;
const int transistorPin4 = 10;
const int transistorPin5 = 11;

// variables will change:

int buttonPushCounter = 0;

int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; 
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;

int lastButtonState = 0;

int potPin1 = A0; // select the input pin for the potentiometer
int potPin2 = A1;
int potPin3 = A2;
int potPin4 = A3;
int potPin5 = A4;

int val = 0; // variable to store the value coming from the sensor

Bounce bouncer1 = Bounce(buttonPin1, 20);
Bounce bouncer2 = Bounce(buttonPin2, 20); // When bouncing another pin change initiator before =
Bounce bouncer3 = Bounce(buttonPin3, 20);
Bounce bouncer4 = Bounce(buttonPin4, 20);
Bounce bouncer5 = Bounce(buttonPin5, 20);


void setup() {
  pinMode(transistorPin1, OUTPUT); // declare the ledPin as an OUTPUT
  pinMode (potPin1, OUTPUT);
  pinMode (buttonPin1, INPUT);

  pinMode(transistorPin2, OUTPUT); // declare the ledPin as an OUTPUT
  pinMode (potPin2, OUTPUT);
  pinMode (buttonPin3, INPUT);

  pinMode(transistorPin3, OUTPUT); // declare the ledPin as an OUTPUT
  pinMode (potPin3, OUTPUT);
  pinMode (buttonPin3, INPUT);

  pinMode(transistorPin4, OUTPUT); // declare the ledPin as an OUTPUT
  pinMode (potPin4, OUTPUT);
  pinMode (buttonPin4, INPUT);
  
   pinMode(transistorPin5, OUTPUT); // declare the ledPin as an OUTPUT
  pinMode (potPin5, OUTPUT);
  pinMode (buttonPin5, INPUT);

  Serial.begin(9600);
}

void loop(){

  bouncer1.update();
  bouncer2.update();
  bouncer3.update();
  bouncer4.update();
  bouncer5.update();

  buttonState1 =  bouncer1.read(); //read the bouncing momentary switches- each associated with the separate switch conditions below.
  buttonState2 =  bouncer2.read();
  buttonState3 =  bouncer3.read();
  buttonState4 =  bouncer4.read();
  buttonState5 =  bouncer5.read();

  if (buttonState1 != lastButtonState){
    if (buttonState1 == HIGH){
      buttonPushCounter++;
      Serial.println ("on");
      Serial.println ("number of button pushes: ");
      Serial.println (buttonPushCounter);
    }
    else {
      Serial.println ("off");
    }
  }

  lastButtonState = buttonState1;
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(transistorPin1, HIGH);
      }
  else
  {
    // read the potentiometer:
    int sensorValue = analogRead(A0);
    // map the sensor value to a range from 0 - 255:
    int outputValue = map(sensorValue, 0, 1023, 0, 255);
    // use that to control the transistor:
    analogWrite(transistorPin1, outputValue);
      }
  if (buttonState2 != lastButtonState){
    if (buttonState2 == HIGH){
      buttonPushCounter++;
      Serial.println ("on");
      Serial.println ("number of button pushes: ");
      Serial.println (buttonPushCounter);
    }
    else {
      Serial.println ("off");
    }
  }

  lastButtonState = buttonState2;
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(transistorPin2, HIGH);
      }
  else
  {
    // read the potentiometer:
    int sensorValue = analogRead(A1);
    // map the sensor value to a range from 0 - 255:
    int outputValue = map(sensorValue, 0, 1023, 0, 255);
    // use that to control the transistor:
    analogWrite(transistorPin2, outputValue);
      } 
  if (buttonState3 != lastButtonState){
    if (buttonState3 == HIGH){
      buttonPushCounter++;
      Serial.println ("on");
      Serial.println ("number of button pushes: ");
      Serial.println (buttonPushCounter);
    }
    else {
      Serial.println ("off");
    }
  }

  lastButtonState = buttonState3;
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(transistorPin3, HIGH);
      }
  else
  {
    // read the potentiometer:
    int sensorValue = analogRead(A2);
    // map the sensor value to a range from 0 - 255:
    int outputValue = map(sensorValue, 0, 1023, 0, 255);
    // use that to control the transistor:
    analogWrite(transistorPin3, outputValue);
      }
 
  if (buttonState4 != lastButtonState){
    if (buttonState4 == HIGH){
      buttonPushCounter++;
      Serial.println ("on");
      Serial.println ("number of button pushes: ");
      Serial.println (buttonPushCounter);
    }
    else {
      Serial.println ("off");
    }
  }

  lastButtonState = buttonState4;
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(transistorPin4, HIGH);
      }
  else
  {
    // read the potentiometer:
    int sensorValue = analogRead(A3);
    // map the sensor value to a range from 0 - 255:
    int outputValue = map(sensorValue, 0, 1023, 0, 255);
    // use that to control the transistor:
    analogWrite(transistorPin4, outputValue);
      }
     }

/*
 Need to add an extra command for the fifth TransistorPin, switch, and pot. My current thinking is that for the superstitious and/or training conditions I would copy the same as above,
 except under the else there would be transistor 1-3 on the superstitious blinking pattern associated with the second click of the momentary buttonPin5.
/*

Have I properly debounced the pushbuttons with the bounce library?

I don't know I have never used the debounce library mainly it is unnecessary.

Are you getting problems with bouncing? If not there is no need to debounce, if so a small delay after a change is detected is all that is needed.

The fact that it misses the pressing of the switch tells me that it is bouncing. I will play with the delay to determine if this will help. Thinking about this further I am wondering why when I push the momentary switch the light twitches or flashes quickly. Can anyone think of a reason that the twitching would occur while pressing the momentary switch?

The fact that it misses the pressing of the switch tells me that it is bouncing.

Well it tells me that bouncing is not your problem. If it were then it would appear the button is being pressed very rapidly.

Sounds it might be a power supply capacity problem or lack of supply decoupling.