Fade out DMX on button press

Hi all I'm looking for some advice on how to get a DMX signal to fade out when a button is released. I'm very close having taken code from fading LEDs and modifying it to use with the Conceptinetics library but it's not quite what I'm after.

#include <Conceptinetics.h>

int buttonPin = 7;
int soundPin = 10; 
//const byte ledPin = 9;
int buttonState = LOW;
boolean OKtoFade = false;
int brightness = 0;
int fadeAmount = 5;

#define DMX_MASTER_CHANNELS   3 

#define RXEN_PIN                2

DMX_Master        dmx_master ( DMX_MASTER_CHANNELS, RXEN_PIN );    


void setup() 
{
  dmx_master.enable ();
    pinMode(soundPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  digitalWrite(soundPin, HIGH);
  //Serial.begin(9600);
}

void loop() 
{
    
  if (digitalRead(buttonPin) == LOW) 
  {
    digitalWrite(soundPin, LOW);
    dmx_master.setChannelValue ( 1, 0);
    brightness = 255;
    OKtoFade = true;
    
  }
  else if (OKtoFade)
  {    
    digitalWrite(soundPin, HIGH);
    brightness = brightness - fadeAmount;
    if (brightness <= 0)
    {
      brightness = 0;
      OKtoFade = false; 
    }
  dmx_master.setChannelValue ( 1, brightness);
  
  }
  delay(40);
}

This is what I'm currently running and the only issue is that the light connected doesnt come on until the button is RELEASED and then fades out. Ideally I'd like it to come on as soon as the button is pushed and then start to fade when it's released, does anyone have any ideas on how this would be achieved?

Thanks in advance

Use the transision of the button, not the state of it.

PCINT - Pin Change Interrupt - set to interrupt on falling and rising edge.

https://dronebotworkshop.com/interrupts/

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.