Go Down

Topic: Getting Multiple Buttons To Trigger DMX signal  (Read 2943 times) previous topic - next topic

DMXbuddy

Hey Guys

This is my first attempt at Arduino so I guess its all a learning curve but I've been stuck on this for a while now and I would greatly appreciate some help. Long story short, I have an Arduino Uno and a DMX shield attached to it and I want to trigger different DMX signals by different buttons mapped to the Digital Pins.

I can send desired DMX signals from my Arduino and receive them on my light but I am having trouble triggering them individually with a momentary button.

At the current moment I have two buttons attached to the unit (one to Digital Pins 5 and the other to pin 6). I want each button to play a different colour sequence. At the moment the button attached to Pin 5 I want it to go through, Green, Blue, Red and the one on pin 6 I want it to play a wash of Yellow, then Red, then Blue.

However when I push the button it will randomly play either a sequence. Sometimes it will play the correct sequence but other times it won't. I'm fairly certain the problem is within my code so I've attached it below and would appreciate it if someone could tell me where I'm going wrong.

Thanks for your time

Code: [Select]
#include <Conceptinetics.h> //To talk to my DMX Sheild
#define DMX_MASTER_CHANNELS   8
#define RXEN_PIN              2

const int buttononePin = 5;     //Button imput one (Pins 2,3,4 are used to transmit DMX signals)
const int buttontwoPin = 6;     // Button imput two
int buttonState = 0;         // variable for reading the pushbutton status


// Configure a DMX master controller, the master controller
// will use the RXEN_PIN to control its write operation
// on the bus
DMX_Master        dmx_master ( DMX_MASTER_CHANNELS, RXEN_PIN );


void setup()

{             
 
  // Enable DMX master interface and start transmitting
  dmx_master.enable (); 
  dmx_master.setChannelRange ( 2, 25, 127 );

  pinMode(buttononePin, INPUT);
  pinMode(buttontwoPin, INPUT);

}

void loop()
// At the moment these numbers are just place holders. 

// Button 1 - Triggers: Green, Blue, Red
 
{
  {
    buttonState = digitalRead(buttononePin);
    if (buttonState == HIGH) 
      {
      // FYI Channel 1 is brightness, 2 is reds, 3 Greens, 4 Blues, 5 Whites...(I dont want to use channels 6-8 but they are) 6 Strobe, 7 Mode, 8 Hold.
      dmx_master.setChannelValue ( 1, 100 ); 
      dmx_master.setChannelValue ( 2, 0); 
      dmx_master.setChannelValue ( 3, 250 ); 
      dmx_master.setChannelValue ( 4, 10 ); 
      dmx_master.setChannelValue ( 5, 10); 
      dmx_master.setChannelValue ( 6, 0 );
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 );   
      delay ( 500 );
     
      dmx_master.setChannelValue ( 1, 100 ); 
      dmx_master.setChannelValue ( 2, 0); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 250 ); 
      dmx_master.setChannelValue ( 5, 10); 
      dmx_master.setChannelValue ( 6, 0 );
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 );   
      delay ( 500 );
   
      dmx_master.setChannelValue ( 1, 100 ); 
      dmx_master.setChannelValue ( 2, 250); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 10 ); 
      dmx_master.setChannelValue ( 5, 10); 
      dmx_master.setChannelValue ( 6, 0 );
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 );   
      delay ( 1000 );
     
      }
 
    else
      {
      // turn DMX off:
      dmx_master.setChannelValue ( 1, 0 ); 
      dmx_master.setChannelValue ( 2, 0 ); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 0 ); 
      dmx_master.setChannelValue ( 5, 0 ); 
      dmx_master.setChannelValue ( 6, 0 );
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 );   
     
      }
  }
// Button 2 - Triggers: Yellow, Red, Blue.

  {
    buttonState = digitalRead(buttontwoPin);
    if (buttonState == HIGH) 
      {
     
      dmx_master.setChannelValue ( 1, 100 ); 
      dmx_master.setChannelValue ( 2, 250 ); 
      dmx_master.setChannelValue ( 3, 125 ); 
      dmx_master.setChannelValue ( 4, 0 ); 
      dmx_master.setChannelValue ( 5, 0); 
      dmx_master.setChannelValue ( 6, 0 ); 
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 ); 
      delay ( 1000 );
     
      dmx_master.setChannelValue ( 1, 100 ); 
      dmx_master.setChannelValue ( 2, 250 ); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 0 ); 
      dmx_master.setChannelValue ( 5, 0 ); 
      dmx_master.setChannelValue ( 6, 0 ); 
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 ); 
      delay ( 500 );
     
      dmx_master.setChannelValue ( 1, 0 ); 
      dmx_master.setChannelValue ( 2, 0 ); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 250 ); 
      dmx_master.setChannelValue ( 5, 0 ); 
      dmx_master.setChannelValue ( 6, 0 ); 
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 ); 
      delay ( 1000 );
   
      }
     
    else
      {
      // turn DMX off:
      dmx_master.setChannelValue ( 1, 0 ); 
      dmx_master.setChannelValue ( 2, 0); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 0 ); 
      dmx_master.setChannelValue ( 5, 0); 
      dmx_master.setChannelValue ( 6, 0 );
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 );   
     
      }
  }
}

marco_c

How are your buttons connected? Sounds like they may be floating.

Please post a drawing of the circuit or a good description.

DMXbuddy

Sorry for the shoddy picture, but in short I power the buttons through the 5 volt pin then i trigger the button sending a signal to pin5 or 6 and through a resistor before common ground.

Whandall

If you are using a breadboard check the connections on both sides of the resistors.

I would suggest having the buttons close to GND and to use INPUT_PULLUP,
that way you don't need the external resistors anyway (but pressed will be represented by LOW then).

You don't care about the bouncing of the keys, but the delays
in your code shield you from seeing the effect, you should better take care of that.

Bounce2 is a nice library that makes it very easy to handle buttons (and their bouncing).

While using delays 'works' for simple sequences, it blocks the program flow
and renders you blind for events or unable to react to them.

Your dmx patterns are perfect candidates to be driven by a state-machine,
each state fires dmx commands, remembers the time and advances to the next state,
if the programmed time has passed.
Only disjunct patterns can share one state-machine, but you can run more than one.

That way you only check the time, and don't waste cpu-time by waiting.

One more note: your sketch is cluttered with redundant braces {}, you should remove them.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Paul__B



So if I can believe that diagram (frankly, I don't :smiley-roll: ), you have both buttons wired together to the same pull-down resistor.

Anyway, bad practice - get into the habit of connecting buttons and input switches to ground, whether or not you use the internal pull-up.

One more note: your sketch is cluttered with redundant braces {}, you should remove them.
Well, one such pair. :smiley-lol:

DMXbuddy

Thanks guys for your help! The Bounce2 library helped and so did the input pull ups. But now I've got another issue. The code always loops twice for every single button press. I thought it might be the debouncing time so I've tried different lengths: 1ms, 5ms, 10ms, 15ms, 20ms, 100ms and the code still looped twice. Any chance someone could spot the flaw in my code?


Code: [Select]
#include <Bounce2.h>              // Bounce Buttons
#include <Conceptinetics.h>       // Pack to talk to my DMX Sheild

#define DMX_MASTER_CHANNELS   8   // The Number Of Channels going int my DMX Light
#define RXEN_PIN              2   // The Return Pin on Arduino

const int buttononePin = 5;       // Pin input one (Pins 2,3,4 are used to transmit DMX signals)
const int buttontwoPin = 6;       // Pin input two
const int buttonthreePin = 7;     // Pin input three
int buttonState = 0;              // variable for reading the pushbutton status

// Configure a DMX master controller, the master controller
// will use the RXEN_PIN to control its write operation
// on the bus
DMX_Master        dmx_master ( DMX_MASTER_CHANNELS, RXEN_PIN );

// Instantiate a Bounce object
Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();

void setup()

{             
   
  pinMode(buttononePin, INPUT_PULLUP);
  debouncer1.attach(buttononePin);
  debouncer1.interval(5); // interval in ms
 
  pinMode(buttontwoPin, INPUT_PULLUP);
  debouncer2.attach(buttontwoPin);
  debouncer2.interval(5); // interval in ms

  pinMode(buttonthreePin, INPUT_PULLUP);
  debouncer3.attach(buttonthreePin);
  debouncer3.interval(5); // interval in ms
 
  // Enable DMX master interface and start transmitting
  dmx_master.enable (); 
  dmx_master.setChannelRange ( 2, 25, 127 );

}

void loop()

{
  debouncer1.update();
  debouncer2.update();
  debouncer3.update();
 
  int one = debouncer1.read();
  int two = debouncer2.read();
  int three = debouncer3.read();

// Button 1
{
    if (one == LOW) 
      {
      // FYI Channel 1 is brightness, 2 is reds, 3 Greens, 4 Blues, 5 Whites...(I dont want to use channels 6-8 but they are) 6 Strobe, 7 Mode, 8 Hold.
      dmx_master.setChannelValue ( 1, 200 );  // A sequence of Yellow, Green, the Blue over 1.5 Seconds
      dmx_master.setChannelValue ( 2, 254); 
      dmx_master.setChannelValue ( 3, 206 ); 
      dmx_master.setChannelValue ( 4, 0 ); 
      dmx_master.setChannelValue ( 6, 0 );
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 );   
      delay ( 500 );
      dmx_master.setChannelValue ( 1, 200 ); 
      dmx_master.setChannelValue ( 2, 0); 
      dmx_master.setChannelValue ( 3, 206 ); 
      dmx_master.setChannelValue ( 4, 0 ); 
      dmx_master.setChannelValue ( 6, 0 );
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 );   
      delay ( 500 );
      dmx_master.setChannelValue ( 1, 200 ); 
      dmx_master.setChannelValue ( 2, 0 ); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 200 ); 
      dmx_master.setChannelValue ( 6, 0 );
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 );   
      delay ( 500 );

       

     
      }
}
// Button 2
  {   
    if (two == LOW) 
      {
      dmx_master.setChannelValue ( 1, 200 ); // A sequence of Red, Blue over 1.5 Seconds
      dmx_master.setChannelValue ( 2,  200 ); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 0 ); 
      dmx_master.setChannelValue ( 5, 0); 
      dmx_master.setChannelValue ( 6, 0 ); 
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 ); 
      delay ( 1000 );
      dmx_master.setChannelValue ( 1, 200 ); 
      dmx_master.setChannelValue ( 2,  0 ); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 200 ); 
      dmx_master.setChannelValue ( 5, 0); 
      dmx_master.setChannelValue ( 6, 0 ); 
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 ); 
      delay ( 500 );
      }
     
    else
      {
      // turn DMX off:
      dmx_master.setChannelValue ( 1, 0 ); 
      dmx_master.setChannelValue ( 2, 0); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 0 ); 
      dmx_master.setChannelValue ( 5, 0 ); 
      dmx_master.setChannelValue ( 6, 0 );
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 );   
     
      }
  }
// Button 3
  {
   
    if (three == LOW) 
      {
      dmx_master.setChannelValue ( 1, 100 );  // White for 2 Seconds
      dmx_master.setChannelValue ( 2, 0 ); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 0 ); 
      dmx_master.setChannelValue ( 5, 200 ); 
      dmx_master.setChannelValue ( 6, 0 ); 
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 ); 
      delay ( 2000 );
      }
     
    else
      {
      // turn DMX off:
      dmx_master.setChannelValue ( 1, 0 ); 
      dmx_master.setChannelValue ( 2, 0 ); 
      dmx_master.setChannelValue ( 3, 0 ); 
      dmx_master.setChannelValue ( 4, 0 ); 
      dmx_master.setChannelValue ( 5, 0 ); 
      dmx_master.setChannelValue ( 6, 0 );
      dmx_master.setChannelValue ( 7, 0 ); 
      dmx_master.setChannelValue ( 8, 0 );       
      }
  }


}

Whandall

You are using the read function, which return true on each statechange,
use fell() for the keypress (on INPUT_PULLUP buttons).

Code: [Select]
void loop() {
  debouncer1.update();
  debouncer2.update();
  debouncer3.update();

  int one = debouncer1.fell();
  int two = debouncer2.fell();
  int three = debouncer3.fell();
 //......
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

DMXbuddy

Thanks for your help Whandall, this fixed my problem and it worked perfectly!

TagCentralen

Maybe it is wrong to open such old question, but I am looking to do the same thing but with millis?
What DMX shield do you use?
/Johan Billow, member of non-profit model railway association "TÃ¥gCentralen Ljungby, Sweden"

noiasca

you can use this Arduino DMX Shield,

by the way it's a bad idea to reopen an old thread.
Open your own one,
link to the old thread, at least it proofs you have done your research before asking.
how to react on postings:
- post helped: provide your final sketch, say thank you & give karma.
- post not understood: Ask as long as you understand the post
- post is off topic (or you think it is): Stay to your topic. Ask again.
- else: Ask again.

jubukraa

by the way it's a bad idea to reopen an old thread.
Open your own one,
Not always a bad idea, and this is one of those times.

The new poster specifically asked the OP and by extension previous respondents "What DMX shield do you use?" and it makes sense to ask that here.

Of course, those folk may not be around to see the new post, but that in itself doesn't make posting in this old thread a bad idea.


jubukraa

I have absolutely no idea what that last post means.

MY post was already a negropost
I especially wonder what a "negropost" is?


Paul__B

Not always a bad idea, and this is one of those times.
Quite so!

Here's an old thread to which people just keep adding.  :smiley-lol:

Paul__B


Go Up