Send output only when input changes.

Hello, I am new to Arduino and this is just a learning project I have been working on.

I have a DMX shield (digital stage lighting controller) using the Conceptinetics library. The shield is controlling a fiber optics driver that uses 4 DMX channels and I have 4 linear potentiometers sending input to the Arduino.

The problem is, the code constantly sends DMX values on all 4 channels, every time the loop repeats. I need to find a way to send a DMX value only when the input for that channel has changed. So, I need to be able to send output for channel 1 only when the channel 1 pot is moving and have no output sent on the other 3 channels. i.e. channel 1 pot moves, channel 1 sends new DMX output.

I have tried a few things that didn't work out. Can someone show me an example that may work or point me the right direction. Thank you.

#include <Conceptinetics.h>

#define DMX_MASTER_CHANNELS   100 

// Pin number to change read or write mode on the shield
#define RXEN_PIN                2

// 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 );

int color = 1; // DMX chan 1, 0 and 255 = no color, 32 = purple, 64 = yellow, 96 = teal,
               // 128 = orange, 160 = blue, 192 = pink, 224 = green 
int cycle = 2; // DMX chan 2, 0 = off, forward 1 = slow, 127 = fast, 128 to 255?
int fade = 3; // DMX chan 3, 0 = full, 127 = off, 255 = full
int strobe = 4; // DMX chan 4, 0 = off, 1 = slow, 255 = fast
int sensorPin0 = A0;
int sensorPin1 = A1;
int sensorPin2 = A2;
int sensorPin3 = A3;
int dmxval0 = 0;
int dmxval1 = 1;
int dmxval2 = 2;
int dmxval3 = 3;

void setup() {             
   dmx_master.enable ();  
}

void loop() 
{
 fibercolor(); 
 fibercycle(); 
 fiberfade(); 
 fiberstrobe();
}

void fibercolor()
  {
  dmxval0 = analogRead(sensorPin0); 
  dmxval0 = dmxval0 / 4;  
  dmx_master.setChannelValue ( color, dmxval0 ); 
  }

void fibercycle()
  {
  dmxval1 = analogRead(sensorPin1); 
  dmxval1 = dmxval1 / 4;  
  dmx_master.setChannelValue ( cycle, dmxval1 ); 
  }

void fiberfade()
  {
  dmxval2 = analogRead(sensorPin2); 
  dmxval2 = dmxval2 / 4;  
  dmx_master.setChannelValue ( fade, dmxval2 ); 
  }

void fiberstrobe()
  {
  dmxval3 = analogRead(sensorPin3); 
  dmxval3 = dmxval3 / 4;  
  dmx_master.setChannelValue ( strobe, dmxval3 ); 
  }

When you are tempted to suffix variable names with letters or numbers, it is time to learn about arrays.

Things like this:

  dmxval0 = analogRead(sensorPin0);

probably change all of the time. I hope that you have a good definition for "pot is moving".

The most important question to answer is: What is YOUR definition of "pot is moving".

In fact, there are four nearly identical functions and I don't understand the need, although it does look pretty.

What you are asking is easy to do with an Arduino but I don't want to write all of the code, especially if it has to be done without arrays.

And never mind. I just figured out, by trying this code with a LED light, that loose connections on the DMX shield are causing phantom signals. I sometimes get flashing of all three colors when the corresponding pots are at 0 but wiggling the wires fixes it... until they get moved again.

@Delta_G, saving a comparing the last setting to the current one is what I was attempting and thinking it wasn't working. I do now believe it is working when all the connections are secure.

@vaj4088, I'll read about arrays and see if I can learn something new.

Thanks for the help.

vaj4088:
When you are tempted to suffix variable names with letters or numbers, it is time to learn about arrays.

And when you have clusters of different variable names all suffixed with litters or numbers, like this

int sensorPin0 = A0;
int sensorPin1 = A1;
int sensorPin2 = A2;
int sensorPin3 = A3;
int dmxval0 = 0;
int dmxval1 = 1;
int dmxval2 = 2;
int dmxval3 = 3;

It's time to learn about structs and classes.

struct DmxFaderFollower {
  byte sensorPin;
  byte dmxChannel;
  byte currentValue = 0;

  DmxFaderFollower(byte attachSensor, byte attachChannel) :
    sensorPin(attachSensor),
    dmxChannel(attachChannel)
  {
  }

  void setup() {}

  void loop() {
    byte newValue = analogRead(sensorPin) / 4;
    if(newValue != currentValue) {
      dmx_master.setChannelValue (newValue, dmxChannel);
      currentValue = newValue;
    }
  }
};

DmxFaderFollower
 color(A0, 0),
 cycle(A1, 1),
 fade(A2, 2),
 strobe(A3, 3);

void setup() {
  color.setup();
  cycle.setup();
  fade.setup();
  strobe.setup();
}

void loop() {
  color.loop();
  cycle.loop();
  fade.loop();
  strobe.loop();
}