Author Topic: led strip light controller  (Read 9354 times)

plexer

  • Newbie
  • *
  • Posts: 38
led strip light controller
« on: November 22, 2013, 11:15:06 am »
Right so I've just built a quadcopter and want to put some pretty leds on it.

I've bought some 12v waterproof led strips from ebay to stick on the arms, I bought 12v strips as the main lipo battery is 11.6v.

I want to use the digispark to control the strips as individuals of which there will be 5 and to respond to a high signal from the flight board to change pattern.

I'm assuming I'll need to use an NPN transistor to switch the led strips as they are 12v's?

On the quadcopter flight controller there is an output called cam_trigger which is used to trigger a camera shutter in response to toggling a switch on the transmitter and everytime I toggle this switch causing a high on a digispark input pin I want to switch to a new pattern of lights.

Does this sound possible?

Any links to sample code would be great.

Thanks

Ben

plexer

  • Newbie
  • *
  • Posts: 38
Re: led strip light controller
« Reply #1 on: November 22, 2013, 11:37:56 am »
Also to add these a single colour led strips not rgb ones.

Ben

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: led strip light controller
« Reply #2 on: November 22, 2013, 03:21:17 pm »
I would attach the cam_trigger to P5, put a transistor on each of the other pins to control the 5 strips. Check P5 for high and either increment a value whenever it is high, and then have a bunch of statements that picks a pattern based on that value (resetting once it exceeds the number of patterns) or whenever P5 goes high do random(X) where X is the max number of patterns, and then pick one based on that value.

plexer

  • Newbie
  • *
  • Posts: 38
Re: led strip light controller
« Reply #3 on: November 23, 2013, 05:45:11 am »
Thanks for that it's how I thought I would do it just wasn't 100%.

Do I need anything between the pin and the transistors?

As I'm pulling the pin high as the signal to change pattern does it need a resistor to ground to hold it low otherwise?

Thanks

Ben

plexer

  • Newbie
  • *
  • Posts: 38
Re: led strip light controller
« Reply #4 on: November 23, 2013, 06:04:09 am »
The other question was should I use pwm for turning them on and off does it offer any benefits? although I'd have to s/w pwm on some pins?

Ben

plexer

  • Newbie
  • *
  • Posts: 38
Re: led strip light controller
« Reply #5 on: November 23, 2013, 03:19:20 pm »
Ok I've written this which compiles and uploads fine but it doesn't do anything.

I've got 2 leds one on each of pins 0 & 1 and a couple of bits of wire to simulate a button on pin 5.

Ben

Code: [Select]
int val = 0; // stores value of input put i.e has it been pressed, 0 = no 1 = yes
 int pattern = 0; // tracks which led pattern we display 0 = off

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(0, OUTPUT); //1st LED strip
  pinMode(1, OUTPUT); //2nd LED strip
  pinMode(2, OUTPUT); //3rd LED strip
  pinMode(3, OUTPUT); //4nd LED strip
  pinMode(4, OUTPUT); //5th LED strip
  pinMode(5, INPUT); //Connect to trigger
}

// the loop routine runs over and over again forever:
void loop() {
 
  val = digitalRead(5);  // read input value
  if (val == HIGH && pattern < 3) {
    pattern++;
  } else {
    pattern = 0;
  }
 
 while (digitalRead(5) == LOW) {
  switch(pattern){
  case '0': 
    // turn all LED's off
    digitalWrite(0, LOW);   
    digitalWrite(1, LOW);
    break;
  case '1':
    digitalWrite(0, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(500); // wait half a second and turn LED off
    digitalWrite(0, LOW);
    break;
  case '2':
    digitalWrite(1, HIGH);    // turn the LED off by making the voltage LOW
    delay(500);
    digitalWrite(1, LOW);
    break;
  case '3':
    digitalWrite(0, HIGH);    // turn the LED off by making the voltage LOW
    delay(500);
    digitalWrite(1, HIGH);
    delay(500);
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    break;
  default:
     // turn all LED's off
    digitalWrite(0, LOW);   
    digitalWrite(1, LOW);
  }
 }
}
 

plexer

  • Newbie
  • *
  • Posts: 38
Re: led strip light controller
« Reply #6 on: November 23, 2013, 03:50:54 pm »
Ok found some issues with the code and this seems to work on the breadboard:

Code: [Select]
int val = 0; // stores value of input put i.e has it been pressed, 0 = no 1 = yes
 int pattern = 0; // tracks which led pattern we display 0 = off

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(0, OUTPUT); //1st LED strip
  pinMode(1, OUTPUT); //2nd LED strip
  pinMode(2, OUTPUT); //3rd LED strip
  pinMode(3, OUTPUT); //4nd LED strip
  pinMode(4, OUTPUT); //5th LED strip
  pinMode(5, INPUT); //Connect to trigger
}

// the loop routine runs over and over again forever:
void loop() {
 
  // read input value
  if (digitalRead(5) == HIGH && pattern < 3) {
    pattern++;
    digitalWrite(0, HIGH);
  } else {
    pattern = 0;
  }
 
 while (digitalRead(5) == LOW) {
  switch(pattern){
  case 0: 
    // turn all LED's on
    digitalWrite(0, HIGH);   
    digitalWrite(1, HIGH);
    break;
  case 1:
    digitalWrite(0, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(500); // wait half a second and turn LED off
    digitalWrite(0, LOW);
    break;
  case 2:
    digitalWrite(1, HIGH);    // turn the LED off by making the voltage LOW
    delay(500);
    digitalWrite(1, LOW);
    break;
  case 3:
    digitalWrite(0, HIGH);    // turn the LED off by making the voltage LOW
    delay(500);
    digitalWrite(1, HIGH);
    delay(500);
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    break;
  default:
     // turn all LED's off
    digitalWrite(0, LOW);   
    digitalWrite(1, HIGH);
  }
 }
}

Any comments or improvements welcome.

Ben

plexer

  • Newbie
  • *
  • Posts: 38
Re: led strip light controller
« Reply #7 on: November 24, 2013, 03:25:49 am »
I tried to use the debounce example in order to prevent false triggers but perhaps I can just use a delay and check the pin is high after that?

Ben

plexer

  • Newbie
  • *
  • Posts: 38
Re: led strip light controller
« Reply #8 on: November 24, 2013, 04:54:09 am »
This is the code I've tried using debounce on the switch input.

Trouble with this is do I have issues with using delay() in the led lighting routines as I gather all other processing stops while this is processed.

Also I'm stuck with the bit for resetting the count to 0 if it exceeds the number of light patterns I've made?

Code: [Select]
/*
  Debounce
  This example code is in the public domain.
 
  http://www.arduino.cc/en/Tutorial/Debounce
  */

// constants won't change. They're used here to
// set pin numbers:
 const int buttonPin = 5;    // the number of the pushbutton pin
 const int flPin = 0;      // Front left strip
 const int frPin = 1;      // Front right strip
 const int rlPin = 2;      // Rear left strip
 const int rrPin = 3;      // Rear right strip
 const int tailPin = 4;    // Tail strip
 
// Variables will change:
//int FL = LOW;         // the current state of the output pin
//int FR = LOW;         // the current state of the output pin
//int RL = LOW;         // the current state of the output pin
//int RR = LOW;         // the current state of the output pin
//int TAIL = LOW;       // the current state of the output pin
int ledState = LOW;   //initial state of led's

int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
int pattern = 0; // tracks which pattern we're on

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
   pinMode(buttonPin, INPUT);
   pinMode(flPin, OUTPUT);
   pinMode(frPin, OUTPUT);
   pinMode(rlPin, OUTPUT);
   pinMode(rrPin, OUTPUT);
   pinMode(tailPin, OUTPUT);

   // set initial LED state
   digitalWrite(flPin, ledState);
   digitalWrite(frPin, ledState);
   digitalWrite(rlPin, ledState);
   digitalWrite(rrPin, ledState);
   digitalWrite(tailPin, ledState);
}

void loop() {
   // read the state of the switch into a local variable:
   int reading = digitalRead(buttonPin);

   // check to see if you just pressed the button
   // (i.e. the input went from LOW to HIGH),  and you've waited
   // long enough since the last press to ignore any noise: 

   // If the switch changed, due to noise or pressing:
   if (reading != lastButtonState) {
     // reset the debouncing timer
     lastDebounceTime = millis();
   }
   
   if ((millis() - lastDebounceTime) > debounceDelay) {
     // whatever the reading is at, it's been there for longer
     // than the debounce delay, so take it as the actual current state:

     // if the button state has changed:
     if (reading != buttonState) {
       buttonState = reading;

       // only increment the pattern counter if the new button state is HIGH and the pattern number is less than the number of patterns
       if (buttonState == HIGH && pattern < 3) {
         pattern++;
       } else {
       pattern = 0;
       }
       }
      }
   
   // save the reading.  Next time through the loop,
   // it'll be the lastButtonState:
   lastButtonState = reading;
   
switch(pattern){
  case 0: 
    // turn all LED's OFF
    digitalWrite(flPin, LOW);   
    digitalWrite(frPin, LOW);
    digitalWrite(rlPin, LOW);
    break;
  case 1:
    digitalWrite(flPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(frPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(rlPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(500); // wait half a second and turn LED off
    digitalWrite(flPin, LOW);
    digitalWrite(frPin, LOW);
    digitalWrite(rlPin, LOW);
    break;
  case 2:
    digitalWrite(flPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(flPin, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(frPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(frPin, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(rlPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(rlPin, LOW);
    break;
  case 3:
    digitalWrite(rlPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(rlPin, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(frPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(frPin, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(flPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(flPin, LOW);
    break;
  default:
    digitalWrite(0, LOW);   
    digitalWrite(1, HIGH);  }

}


plexer

  • Newbie
  • *
  • Posts: 38
Re: led strip light controller
« Reply #9 on: November 24, 2013, 07:52:51 am »
Here's my current code that appears to work as I expect and loops round all the patterns.

Just need to work out the most efficient way of showing the light patterns.

Ben

Code: [Select]

/*
  Debounce
  This example code is in the public domain.
 
  http://www.arduino.cc/en/Tutorial/Debounce
  */

// constants won't change. They're used here to
// set pin numbers:
 const int buttonPin = 5;    // the number of the pushbutton pin
 const int flPin = 0;      // Front left strip
 const int frPin = 1;      // Front right strip
 const int rlPin = 2;      // Rear left strip
 const int rrPin = 3;      // Rear right strip
 const int tailPin = 4;    // Tail strip
 const int pattern_num = 3; // Number of light patterns we have defined
 
// Variables will change:
//int FL = LOW;         // the current state of the output pin
//int FR = LOW;         // the current state of the output pin
//int RL = LOW;         // the current state of the output pin
//int RR = LOW;         // the current state of the output pin
//int TAIL = LOW;       // the current state of the output pin
int ledState = LOW;   //initial state of led's

int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
int pattern = 0; // tracks which pattern we're on

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
   pinMode(buttonPin, INPUT);
   pinMode(flPin, OUTPUT);
   pinMode(frPin, OUTPUT);
   pinMode(rlPin, OUTPUT);
   pinMode(rrPin, OUTPUT);
   pinMode(tailPin, OUTPUT);

   // set initial LED state
   digitalWrite(flPin, ledState);
   digitalWrite(frPin, ledState);
   digitalWrite(rlPin, ledState);
   digitalWrite(rrPin, ledState);
   digitalWrite(tailPin, ledState);
}

void loop() {
   // read the state of the switch into a local variable:
   int reading = digitalRead(buttonPin);

   // check to see if you just pressed the button
   // (i.e. the input went from LOW to HIGH),  and you've waited
   // long enough since the last press to ignore any noise: 

   // If the switch changed, due to noise or pressing:
   if (reading != lastButtonState) {
     // reset the debouncing timer
     lastDebounceTime = millis();
   }
   
   if ((millis() - lastDebounceTime) > debounceDelay) {
     // whatever the reading is at, it's been there for longer
     // than the debounce delay, so take it as the actual current state:

     // if the button state has changed:
     if (reading != buttonState) {
       buttonState = reading;

       // only increment the pattern counter if the new button state is HIGH and the pattern number is less than the number of patterns
       if (buttonState == HIGH && pattern < (pattern_num + 1)) {
         pattern++;
       if (pattern == 4) {
         pattern = 0;
       } 
     }
       }
   }   
   
   // save the reading.  Next time through the loop,
   // it'll be the lastButtonState:
   lastButtonState = reading;
   
   // now run some light shows
switch(pattern){
  case 0: 
    // turn all LED's OFF
    digitalWrite(flPin, LOW);   
    digitalWrite(frPin, LOW);
    digitalWrite(rlPin, LOW);
    break;
  case 1:
    digitalWrite(flPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(frPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(rlPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(500); // wait half a second and turn LED off
    digitalWrite(flPin, LOW);
    digitalWrite(frPin, LOW);
    digitalWrite(rlPin, LOW);
    break;
  case 2:
    digitalWrite(flPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(flPin, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(frPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(frPin, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(rlPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(rlPin, LOW);
    break;
  case 3:
    digitalWrite(rlPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(rlPin, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(frPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(frPin, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(flPin, HIGH);    // turn the LED on by making the voltage HIGH
    delay(200);
    digitalWrite(flPin, LOW);
    break;
  default:
    digitalWrite(flPin, LOW);   
    digitalWrite(frPin, HIGH); 
    digitalWrite(rlPin, LOW);     
  }

}

gogol

  • Sr. Member
  • ****
  • Posts: 398
Re: led strip light controller
« Reply #10 on: November 24, 2013, 12:47:04 pm »
Hello plexer,

some thoughts:

  • does the cam_trigger really need debouncing?  Normally push-button like stuff or relays need that. 
    That can be done easily with a small cap and an resistor.
  • your code will lose many button-events, as you have a lot of delays in your code.  I would write a function  boolean wait_for_trigger(  waitTime), which will do the following:
    • reading the trigger-button state in an loop
    • return with false, when the button-state has not changed within waitTime
    • return with true in that moment, the button-state has changed

Before the switch-statement I would set all LEDs to off, so I have each loop an defined state for my "case"s.
(pull the content of case 0: in front of the switch-statement)

Instead of delay(waitTime), I would insert if (boolean wait_for_trigger(  waitTime)){ break};

That way, "pattern"  would be set through the wait_for_trigger() function, which will really watch your trigger state most of the time.

wait_for_trigger() will calculate a stop time before looping like:  stopTime = millis()+waitTime; and than loop till millis() > stopTime.

That way you don't waste time with delay, where the cpu just does nothing, and no button state is detected.

Hope that helps

  gogol

 

plexer

  • Newbie
  • *
  • Posts: 38
Re: led strip light controller
« Reply #11 on: November 24, 2013, 02:22:27 pm »
Hey gogol,

Thanks for your input I'll see what I can do.

At the moment it works and does what I want it to do, missing some key presses isn't too much of an issue really as you would just keep flicking the switch until you get the pattern you want but I do want a more effective way of being able to write patterns.

I'm not sure if the cam trigger really needs debouncing and I may be looking at the incoming PPM signal from the receiver anyway so may have to change how I detect that.

Thanks

Ben