Implementing 'fade' and 'fire effect' into sketch

I have this code (2nd of the 2 codes at the bottom) finally working perfect with the help of forum members
especially cyclegadget and AWOL.
These are two interactive proximity sensing machines
2 Arduino Lilypad Simple Boards http://www.sparkfun.com/products/10274are connected via Xbee Series 2 radios Digi XBee Ecosystem | Everything You Need to Explore and Create Wireless Connectivity | Digi International via shieldhttp://www.sparkfun.com/products/8937.
The Simple Board lacks Rx/Tx, therefore the use of SoftwareSerial.
If the 2 wearers are within reach (30 feet approx) one ProximityIndicatorLED
comes on on the other wearers piece, if a button is pressed, another ButtonLED
lights up. This is the working combined sketch so that both machines can
do the same to each other.
I have now my LED's longer on without delay, but on the opposing
machine I want the first (ProximityLED) to imitate a heartbeat pulsing-
so using 'fade' instead of 'blink'. I was already able to implement 'blink'.
Yet I'm struggling to adjust the blinking frequency with 'long interval'.
And the ButtonLed should trigger a 'fire effect' of a couple of LEDs instead of just coming on.
First: Here is the code for 'fire effect'. Ideally it should do 3 independent LEDs. I would be happy,
if I could get 1 to flicker like it does as a simple sketch.

// LED Fire Effect for one LED

int ButtonLed = 9;

void setup()
{
  pinMode(ButtonLed, OUTPUT);

}

void loop() {
  analogWrite(ButtonLed, random(120)+135);

  delay(random(100));
}
/*
  By Alexandra da Fonseca-Klein, Digital Jewellery project,
 For interactive Jewellery 'proximity sensing hearts'
 www.ADFK.co.uk; 2012
 2 Arduino Lilypad Simple Boards are connected via Xbee Series 2 radios.
 The Simple Board lacks Rx/Tx, therefore the use of SoftwareSerial.
 If the 2 wearers are within reach (30 feet approx) one ProximityIndicatorLED
 comes on on the other wearers piece, if a button is pressed, another ButtonLED
 lights up. This is the working combined sketch so that both machines can
 do the same to each other. S before the comment stands for 'from server code'.
 
 */
#include <SoftwareSerial.h>        // use these specified set of commands
//////////////////SERIAL PINS//////////////////
const int RXPin = A4;              // setting the pin to RX
const int TXPin = A5;              // setting the pin to TX
SoftwareSerial xbee (RXPin, TXPin);// defining the new serial name
/////////////////LED PINS//////////////////////
const int ButtonLed = 9;     // the number of the PushLed pin
const int ProxInd =  11;      // the number of the ProximityIndicatorLED pin
const int ButtInd =  13;      //S  the number of the indicator LED pin
////////////////BUTTON PINS/////////////////////
const int buttonPin = 5;     // S the number of the pushbutton pin
////////////////STATE VARIABLES////////////////
int buttonState = 0;         // S variable for reading the pushbutton status
int ledState = LOW;
int ledState2 = LOW;
////////////////TIMER VARIABLES///////////////
unsigned long TimerProxInd;
unsigned long TimerButtonLed;
unsigned long TimerprintH = 0;    //timer for printing H
unsigned long TimerprintL = 0;    //timer for printing L
///////////////SERIAL VARIABLES//////////////
char incomingByte;    // a variable to read incoming serial data into

///////////////SET-UP////////////////////////
void setup()                // definitions that are always true
{
  Serial.begin(9600);
  xbee.begin(9600);// set the data rate for the SoftwareSerial port
  pinMode(ProxInd, OUTPUT);   // the number of the ProximityIndicatorLED pin (that comes on when there's a steady 'H' or Serial input)
  pinMode(ButtonLed, OUTPUT); // the number of the PushLed pin ( that comes on when there's an 'L' read on Serial)
  pinMode(ButtInd, OUTPUT);	//S  declare LED as output the number of the indicator LED pin 
  //(that indicates on the same board that the button has been pressed)
  pinMode(buttonPin, INPUT);     //S  declare pushbutton as input
  digitalWrite(buttonPin, HIGH);  //S default pin state to HIGH or 'on'
}

void loop() // run over and over
{
  unsigned long newtime = millis();

  if (newtime - TimerprintH >= 900UL)    //S wait 900ms
  { 
    xbee.print('H');                  //S send the letter H out
    TimerprintH = newtime;            //update timer
  }
/////////////////////////CHECK THE BUTTON//////////////////////
  buttonState = digitalRead(buttonPin);//S read the state of the pushbutton value:

  if (buttonState == HIGH)    //S is the button not being pressed
  {
    digitalWrite(ButtInd, LOW);//S leave the ButtonIndicatorLED off: 
  }
  else                      //S otherwise
  {
    if (newtime - TimerprintL >= 2000UL)    //S wait 2 seconds between sending L
    {
    xbee.print('L');         //S send the letter 'L' over the wireless connection
    digitalWrite(ButtInd, HIGH);//S and turn ButtonIndicatorLED on:
    TimerprintL = newtime;            //update timer
    }
  }

//////////////////////////CHECK FOR INCOMING SERIAL///////////
  // see if there's incoming serial data:
  if (xbee.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = (char)xbee.read();
    
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') 
    {
      TimerProxInd = newtime,
      digitalWrite(ProxInd, HIGH);
      ledState = HIGH;
      }

    // if it's a capital L, turn on the LED:
    if (incomingByte == 'L') 
    {
      TimerButtonLed = newtime; 
      digitalWrite(ButtonLed, HIGH);
      ledState2 = HIGH;
      }
  }

  if (newtime - TimerProxInd >= 2000UL && ledState == HIGH)
  {
    digitalWrite(ProxInd,LOW);
    ledState = LOW;
    Serial.println("time is up for TimerProxInd");
  }

  if (newtime - TimerButtonLed >= 1000UL && ledState2 == HIGH)
  {
    digitalWrite(ButtonLed, LOW);
    ledState2 = LOW;
    Serial.println("time is up for ButtonLed");
  }
}

AnalogWrite is capable of 0 to 255, 135 is about 50% power level. Try removing the + 135 and changing the first number to 255. See if you like the result and try different numbers.

analogWrite(ButtonLed, random(120)+135);

Try analogWrite(ButtonLed, random(255);

// LED Fire Effect for one LED

int ButtonLed = 9;

void setup()
{
  pinMode(ButtonLed, OUTPUT);

}

void loop() {
  analogWrite(ButtonLed, random(120)+135);

  delay(random(100));
}

I forgot to turn on the notify function.
Yes awesome. That's the effect I want.
There had to be a second ) after the 255.

  analogWrite(ButtonLed, random(255));

I put that into my 'L' portion for the Button press at the bottom,
but the Led only comes on normally.

I think the best approach is to create a function with a meaningful name like ledfire().

The function ledfire() will need to contain the random led action and blink without delay style. The function will later be called with an "if" test.

If you could make a your random LED code work without delay, so that you get a hand in the process. I will help you create the function and help you put it in with the rest of your proximity code.

Some tips to help you write the code: it will be handy to use the variable newtime like we did with the other timed actions. You can use the other timed events as examples.

Thanks.

  1. So I took the standard blink without delay and implemented it, taking the two variables from the top:
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 500;           // interval at which to blink (milliseconds)

and put the heartbeat function into the "if 'H'" area.

    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') 

      /////// BLINK WITHOUT DELAY /////////
      if(currentMillis - previousMillis > interval) {
        // save the last time you blinked the LED 
        previousMillis = currentMillis;   

        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
          ledState = HIGH;
        else
          ledState = LOW;

        // set the LED with the ledState of the variable:
        digitalWrite(ProxInd, ledState);
      }

It does blink, but I can't make it blink faster than approx. 1 second,
no matter, what I put for 'long interval' at the top. I would
like 500, as I put it to.
Plus, I would like it to fade without delay. More like a heart pulse.
Tried various ideas in the old forum but no luck. I also tried the
traditional 'fade' with delay- no luck.
At least I got the blink without delay working- albeit with a set
1000 frequency.

  1. Now the 'LED fire effect' sketch seems pretty simple and with your
    idea (255) even better, the delay(30) doesn't bother me.
    What I don't get, after I replaced the regular ButtonLed function
    in the if 'L' with this simple 'fire' function:
    // if it's a capital L, turn on the LED:
    if (incomingByte == 'L') 
    {
      ///////LED FIRE SKETCH///////

      analogWrite(ButtonLed, random(255)); // FIRE, WORKS ON ITS OWN- NOT HERE
      delay(random(30));

      digitalWrite(ProxInd, LOW)  //////DURING FIRE HEARTBEAT STOPS//////

        delay(1000);                ///DREADED DELAY
    }

The LED stays simply on- without fire effect. Now I tried to at least have some effect,
that I could write in my text about, so I made the heartbeat stop when the ButtonLED (fire)
is on, and then have it on for a second.

  1. What I also observed with this configuration, is the following:
    When the two machines increase their distance so that 'H'
    is not transmitted anymore or the other machine is turned off,
    the ProximityLed turns off, right?
    That does work perfectly in the first machine.
    In this 'heartbeat and fire' machine it does stay off only if the blink was on LOW
    at the moment of losing signal.
    When the LED was HIGH at that moment, it stays on, which is a little awkward.
    I tried different approaches to end the ProximityLED like i.e.
else
digitalWrite(ProxInd, LOW);

but it might stay on in this stage of the programming.

  1. Here's the code for 'proximity_heart_beat_fire) so far
    that's working to make at least a prototype of a slightly
    different counterpart to the first machine that I've called "proximity_heart_colour_vibe"
    as I said before. (It has a colour changing LED on ProxInd and a vibe board on ButtonLed)
/*
  By Alexandra da Fonseca-Klein, Digital Jewellery project,
 For interactive Jewellery 'proximity sensing hearts'
 www.ADFK.co.uk; 2012
 2 Arduino Lilypad Simple Boards are connected via Xbee Series 2 radios.
 The Simple Board lacks Rx/Tx, therefore the use of SoftwareSerial.
 If the 2 wearers are within reach (30 feet approx) one ProximityIndicatorLED
 comes on on the other wearers piece, if a button is pressed, another ButtonLED
 lights up. This is supposed to be the combined sketch so that both machines can
 do the same to each other. S before the comment stands for 'from server code'.
 This is the 'heartbeat' and 'fire' side.
 
 */
#include <SoftwareSerial.h>        // use these specified set of commands
//////////////////SERIAL PINS//////////////////
const int RXPin = A4;              // setting the pin to RX
const int TXPin = A5;              // setting the pin to TX
SoftwareSerial xbee (RXPin, TXPin);// defining the new serial name
/////////////////LED PINS//////////////////////
const int ButtonLed = 9;     // the number of the PushLed pin (FIRE)
const int ProxInd =  11;      // the number of the ProximityIndicatorLED pin (HEARTBEAT)
const int ButtInd =  13;      //S  the number of the indicator LED pin
////////////////BUTTON PINS/////////////////////
const int buttonPin = 5;     // S the number of the pushbutton pin
////////////////STATE VARIABLES////////////////
int buttonState = 0;         // S variable for reading the pushbutton status
int ledState = LOW;
int ledState2 = LOW;
////////////////TIMER VARIABLES///////////////
unsigned long TimerProxInd;
unsigned long TimerButtonLed;
unsigned long TimerprintH = 0;    //timer for printing H
unsigned long TimerprintL = 0;    //timer for printing L
///////////////SERIAL VARIABLES//////////////
char incomingByte;    // a variable to read incoming serial data into

// Variables will change:

long previousMillis = 0;        // will store last time LED was updated

/////// BLINK WITHOUT DELAY FREQUENCY REGULATOR WORKS ON ITS OWN- NOT HERE

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 500;           // interval at which to blink (milliseconds)

///////////////SET-UP////////////////////////
void setup()                // definitions that are always true
{
  Serial.begin(9600);
  xbee.begin(9600);// set the data rate for the SoftwareSerial port
  pinMode(ProxInd, OUTPUT);   // the number of the ProximityIndicatorLED pin (that comes on when there's a steady 'H' or Serial input)
  pinMode(ButtonLed, OUTPUT); // the number of the PushLed pin ( that comes on when there's an 'L' read on Serial)
  pinMode(ButtInd, OUTPUT);	//S  declare LED as output the number of the indicator LED pin 
  //(that indicates on the same board that the button has been pressed)
  pinMode(buttonPin, INPUT);     //S  declare pushbutton as input
  digitalWrite(buttonPin, HIGH);  //S default pin state to HIGH or 'on'
}

void loop() // run over and over
{
  unsigned long currentMillis = millis();
  unsigned long newtime = millis();

  if (newtime - TimerprintH >= 900UL)    //S wait 900ms
  { 
    xbee.print('H');                  //S send the letter H out
    TimerprintH = newtime;            //update timer
  }
  /////////////////////////CHECK THE BUTTON//////////////////////
  buttonState = digitalRead(buttonPin);//S read the state of the pushbutton value:

  if (buttonState == HIGH)    //S is the button not being pressed
  {
    digitalWrite(ButtInd, LOW);//S leave the ButtonIndicatorLED off: 
  }
  else                      //S otherwise
  {
    if (newtime - TimerprintL >= 1000UL)    //S wait 2 seconds between sending L
    {
      xbee.print('L');         //S send the letter 'L' over the wireless connection
      digitalWrite(ButtInd, HIGH);//S and turn ButtonIndicatorLED on:
      TimerprintL = newtime;            //update timer
    }
  }

  //////////////////////////CHECK FOR INCOMING SERIAL///////////
  // see if there's incoming serial data:
  if (xbee.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = (char)xbee.read();

    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') 

      /////// BLINK WITHOUT DELAY /////////
      if(currentMillis - previousMillis > interval) {
        // save the last time you blinked the LED 
        previousMillis = currentMillis;   

        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
          ledState = HIGH;
        else
          ledState = LOW;

        // set the LED with the ledState of the variable:
        digitalWrite(ProxInd, ledState);
      }

    // if it's a capital L, turn on the LED:
    if (incomingByte == 'L') 
    {
      ///////LED FIRE SKETCH///////

      analogWrite(ButtonLed, random(255)); // FIRE, WORKS ON ITS OWN- NOT HERE
      delay(random(30));

      digitalWrite(ProxInd, LOW)  //////DURING FIRE HEARTBEAT STOPS//////

        delay(1000);                ///DREADED DELAY
    }
  }
  else
    digitalWrite(ButtonLed, LOW);
}

Code is untested..

Here are two functions FIRE() and HEART(). The logic is to set a flag to 1 for a 'if" test which will call the function...heartflag and fireflag.

I took your blink with out delay code and changed the variables to one with meaning names to show you the benefits of good names.

unsigned long firetime = 0;       //timer for fire effect
unsigned long heartbeat = 0;      //timeer for heart beats
unsigned long previousBeat = 0;        // will store last time heart LED was updated
unsigned long heartRate = 500;           // interval at which to blink the heart (milliseconds)

The functions

//////// HEART BEAT FUNCTION////
void HEART()
{
  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW)
  {
    ledState = HIGH;
  }
  else
  {
    ledState = LOW;
  }
  // set the LED with the ledState of the variable:
  digitalWrite(ProxInd, ledState);

}
///////LED FIRE FUNCTION///////
void FIRE()
{
  analogWrite(ButtonLed, random(255)); // FIRE, WORKS ON ITS OWN- NOT HERE
  digitalWrite(ProxInd, LOW);  //////DURING FIRE HEARTBEAT STOPS//////
}
/*
  By Alexandra da Fonseca-Klein, Digital Jewellery project,
 For interactive Jewellery 'proximity sensing hearts'
 www.ADFK.co.uk; 2012
 2 Arduino Lilypad Simple Boards are connected via Xbee Series 2 radios.
 The Simple Board lacks Rx/Tx, therefore the use of SoftwareSerial.
 If the 2 wearers are within reach (30 feet approx) one ProximityIndicatorLED
 comes on on the other wearers piece, if a button is pressed, another ButtonLED
 lights up. This is the working combined sketch so that both machines can
 do the same to each other. S before the comment stands for 'from server code'.
 
 */
#include <SoftwareSerial.h>        // use these specified set of commands
//////////////////SERIAL PINS//////////////////
const int RXPin = A4;              // setting the pin to RX
const int TXPin = A5;              // setting the pin to TX
SoftwareSerial xbee (RXPin, TXPin);// defining the new serial name
/////////////////LED PINS//////////////////////
const int ButtonLed = 9;     // the number of the PushLed pin
const int ProxInd =  11;      // the number of the ProximityIndicatorLED pin
const int ButtInd =  13;      //S  the number of the indicator LED pin
////////////////BUTTON PINS/////////////////////
const int buttonPin = 5;     // S the number of the pushbutton pin
////////////////STATE VARIABLES////////////////
int buttonState = 0;         // S variable for reading the pushbutton status
int ledState = LOW;          //state of the heart led
int fireflag = 0;            //flag to show that fire effect needs ran or stopped
int heartflag = 0;           //flag to show that the heart beat need ran or stopped
////////////////TIMER VARIABLES///////////////
unsigned long TimerProxInd;
unsigned long TimerButtonLed;
unsigned long TimerprintH = 0;    //timer for printing H
unsigned long TimerprintL = 0;    //timer for printing L
unsigned long firetime = 0;       //timer for fire effect
unsigned long heartbeat = 0;      //timeer for heart beats
unsigned long previousBeat = 0;        // will store last time heart LED was updated
unsigned long heartRate = 500;           // interval at which to blink the heart (milliseconds)
///////////////SERIAL VARIABLES//////////////
char incomingByte;    // a variable to read incoming serial data into

///////////////SET-UP////////////////////////
void setup()                // definitions that are always true
{
  Serial.begin(9600);
  xbee.begin(9600);// set the data rate for the SoftwareSerial port
  pinMode(ProxInd, OUTPUT);   // the number of the ProximityIndicatorLED pin (that comes on when there's a steady 'H' or Serial input)
  pinMode(ButtonLed, OUTPUT); // the number of the PushLed pin ( that comes on when there's an 'L' read on Serial)
  pinMode(ButtInd, OUTPUT);	//S  declare LED as output the number of the indicator LED pin 
  //(that indicates on the same board that the button has been pressed)
  pinMode(buttonPin, INPUT);     //S  declare pushbutton as input
  digitalWrite(buttonPin, HIGH);  //S default pin state to HIGH or 'on'
}

void loop() // run over and over
{
  unsigned long newtime = millis();

  if (newtime - TimerprintH >= 900UL)    //S wait 900ms
  { 
    xbee.print('H');                  //S send the letter H out
    TimerprintH = newtime;            //update timer
  }
  /////////////////////////CHECK THE BUTTON//////////////////////
  buttonState = digitalRead(buttonPin);//S read the state of the pushbutton value:

  if (buttonState == HIGH)    //S is the button not being pressed
  {
    digitalWrite(ButtInd, LOW);//S leave the ButtonIndicatorLED off: 
  }
  else                      //S otherwise
  {
    if (newtime - TimerprintL >= 2000UL)    //S wait 2 seconds between sending L
    {
      xbee.print('L');         //S send the letter 'L' over the wireless connection
      digitalWrite(ButtInd, HIGH);//S and turn ButtonIndicatorLED on:
      TimerprintL = newtime;            //update timer
    }
  }

  //////////////////////////CHECK FOR INCOMING SERIAL///////////
  // see if there's incoming serial data:
  if (xbee.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = (char)xbee.read();

    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') 
    {
      TimerProxInd = newtime;
      heartflag = 0;
    }

    // if it's a capital L, turn on the LED:
    if (incomingByte == 'L') 
    {
      TimerButtonLed = newtime; 
      fireflag = 1;       //set flag to 1 to call for FIRE!!!
    }
  }

  if (newtime - TimerProxInd >= 2000UL && heartflag == 1)
  {
    heartflag = 0;
    digitalWrite(ProxInd,LOW);
    Serial.println("time is up for TimerProxInd");
  }

  if (newtime - TimerButtonLed >= 1000UL && fireflag == 1)
  {
    fireflag = 0;       //set flag to 0 to put out the fire
    digitalWrite(ButtonLed, LOW);    //set LED to low
    Serial.println("time is up for ButtonLed");
  }

  if (fireflag == 1 && newtime - firetime >= random(30))
  {
    firetime = newtime; 
    FIRE();
  } 

  if ( heartflag == 1 && fireflag == 0 && newtime - previousBeat >= heartRate) //if fire is not running and it is time run HEART()
  {  
    previousBeat = newtime; 
    HEART();
  }

}

//////// HEART BEAT FUNCTION////
void HEART()
{
  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW)
  {
    ledState = HIGH;
  }
  else
  {
    ledState = LOW;
  }
  // set the LED with the ledState of the variable:
  digitalWrite(ProxInd, ledState);

}
///////LED FIRE FUNCTION///////
void FIRE()
{
  analogWrite(ButtonLed, random(255)); 
  digitalWrite(ProxInd, LOW);  //////DURING FIRE HEARTBEAT STOPS//////
}

I'm in the middle of soldering them into their final casings. After I've got them inside, I'll access them again and try the code out.

Super fire effect!
Works 1 second and then off and without delay repeatable. So great.
But the LED heart beat is not working now.
I have all in the casings- will post a picture tomorrow.

I had the heartflag set to 0 instead of 1 in this part:

if (incomingByte == 'H') 
    {
      TimerProxInd = newtime;
      heartflag = 1;
    }

Interesting how you've made the programs in separate voids. I'm learning.

Yeah. Everything works perfect. Both machines.
When the fire comes on the heartbeat stops,
and it looks great.
The other machine has mood light when in proximity
and gently trembles when the button is pressed.
Thank you so much.
Like I said, I'll post the pictures.