HULK SMASH!!!

Hey!!

im building an incredible hulk project for a friend. i have an adafruit sound FX board to handle the audio. im using an uno to power the fx board, control the triggering, fading in and out an LED (like a pulse), and the button to trigger the sound. what i want to do is have the LED fading and out slowly. when i press the button, i want the uno to trigger the sound board (100-120ms pulse), increase the speed of the fade on the LED, and hold the faster pulse for a certain time ie. 5 seconds. ill need to be able to adjust the time so i can time it to the sound effect. i can get the pulse speed control to work on a momentary press but i have to hold the button in for it to stay fast. as soon as i let go it reverts to the slower fade. i can also get the trigger to work but i cant get them to work together and in the correct sequence. any help would be appreciated!!

heres the code so far…im using pin 13 to represent the sound trigger.

Smash.ino (2.1 KB)

Welcome to the Forum. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

It is important to provide as much of the information that is needed to solve your problem as you can, in your first post. The forum has guidelines for posting in a standard way that makes it easiest for knowledgeable people to help you. Making an effort to do this will greatly increase the number and quality of helpful responses that you get.

In this case, the problem is that you have added your sketch as an attachment. Unless the sketch is too large, it's better if you post your code (using code tags!), rather than attach it. When it's attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the "Temp" folder and manually remove it. It's much easier to just view the code in your post.

The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing. Code tags can also be inserted manually in the forum text using the code and /code metatags.

You need to do your timing and detect button state changes

const int Blink_Button = 2;
const int Smash_Trigger_Button = 3;
const int Button_LED = 9;
const int Smash_Trigger = 13;
int Button_LED_State;
int Previous_Button_LED_State;
unsigned long Time, startTime;
const unsigned long fastDuration = 5000;
int Count;
const int Fade_Amount = 5;
int Brightness = 0;

void setup() {
  pinMode (Blink_Button, INPUT_PULLUP);
  pinMode (Smash_Trigger_Button, INPUT_PULLUP);
  pinMode (Smash_Trigger, OUTPUT);
  pinMode (Button_LED, OUTPUT);
  Previous_Button_LED_State = digitalRead(Blink_Button);
  startTime = 0;
}

void loop() {

  Button_LED_State = digitalRead(Blink_Button);

  if ( Button_LED_State != Previous_Button_LED_State ) {
    // we have a state change
    if ( Button_LED_State == HIGH ) {
      // button was just pressed, increase fade speed and trigger sound board
      Time = 5;
      Trigger();
      startTime = millis();
    }
    else {
      // button was released, do nothing
    }
    delay(50);  // debounce
    Previous_Button_LED_State = Button_LED_State;
  }

  Fade();

  // check to see if we are doing a fast fade and if so,
  // turn it off if enough time has elapsed
  // startTime == 0 means we are not doing fast fade
  if ( startTime && millis() - startTime >= fastDuration ) {
    startTime = 0;  // turn it off
    Time = 30;  // slow fade
  }
}

void Fade() {

  static unsigned long Previous_Millis = 0;

  unsigned long Current_Millis = millis();

  if (Current_Millis - Previous_Millis >= Time) {
    Previous_Millis = Current_Millis;
    analogWrite(Button_LED, Brightness);

    // change the brightness for next time through the loop:
    Brightness += Fade_Amount;

    // reverse the direction of the fading at the ends of the fade:
    if (Brightness <= 0 || Brightness >= 255) {
      Fade_Amount = - Fade_Amount;
    }
  }
}

void Trigger() {

  digitalWrite (Smash_Trigger, HIGH);
  delay (100);
  digitalWrite (Smash_Trigger, LOW);

}

my Fault!!....i didnt see where i could do that when i posted

it works perfectly except when i upload or restart the UNO....the button LED blinks Really fast.....like almost on solid......once i press the button, it triggers and fades fast, and then slow after the time is up.....any thoughts??

it works perfectly except when i upload or restart the UNO…the button LED blinks Really fast…like almost on solid…once i press the button, it triggers and fades fast, and then slow after the time is up…any thoughts??

blh64:
You need to do your timing and detect button state changes

const int Blink_Button = 2;

const int Smash_Trigger_Button = 3;
const int Button_LED = 9;
const int Smash_Trigger = 13;
int Button_LED_State;
int Previous_Button_LED_State;
unsigned long Time, startTime;
const unsigned long fastDuration = 5000;
int Count;
const int Fade_Amount = 5;
int Brightness = 0;

void setup() {
  pinMode (Blink_Button, INPUT_PULLUP);
  pinMode (Smash_Trigger_Button, INPUT_PULLUP);
  pinMode (Smash_Trigger, OUTPUT);
  pinMode (Button_LED, OUTPUT);
  Previous_Button_LED_State = digitalRead(Blink_Button);
  startTime = 0;
}

void loop() {

Button_LED_State = digitalRead(Blink_Button);

if ( Button_LED_State != Previous_Button_LED_State ) {
    // we have a state change
    if ( Button_LED_State == HIGH ) {
      // button was just pressed, increase fade speed and trigger sound board
      Time = 5;
      Trigger();
      startTime = millis();
    }
    else {
      // button was released, do nothing
    }
    delay(50);  // debounce
    Previous_Button_LED_State = Button_LED_State;
  }

Fade();

// check to see if we are doing a fast fade and if so,
  // turn it off if enough time has elapsed
  // startTime == 0 means we are not doing fast fade
  if ( startTime && millis() - startTime >= fastDuration ) {
    startTime = 0;  // turn it off
    Time = 30;  // slow fade
  }
}

void Fade() {

static unsigned long Previous_Millis = 0;

unsigned long Current_Millis = millis();

if (Current_Millis - Previous_Millis >= Time) {
    Previous_Millis = Current_Millis;
    analogWrite(Button_LED, Brightness);

// change the brightness for next time through the loop:
    Brightness += Fade_Amount;

// reverse the direction of the fading at the ends of the fade:
    if (Brightness <= 0 || Brightness >= 255) {
      Fade_Amount = - Fade_Amount;
    }
  }
}

void Trigger() {

digitalWrite (Smash_Trigger, HIGH);
  delay (100);
  digitalWrite (Smash_Trigger, LOW);

}

Your ‘Time’ variable is initialized to 0 so that is your blink delay. If you want it to start with a default blink rate, initialize it to 30.

That seems like something you could figure out?