Trying to create an asymmetrical blink without delay PLEASE HELP!

I am trying to create a heart beat using a relay + light and I can't have a delay because I am using a ping sensor and motor to make the heart beat faster and sculpture vibrate as you get closer. This is for a sculpture I am doing and my professor and I have tried many ways but can't seem to get it to work. All I need is the relay heart heart beat code and what I would I would map. I can do the rest.

PLEASE HELP :stuck_out_tongue_closed_eyes: :'( !!!!!

I assume that you have seen the BlinkWithoutDelay example in the IDE. Modify it like this

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

Choose appropriate values for longValue and shortValue to suit your needs.

This is what I have so far, using your code. I get no blinking at all.

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
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 = 1000;           // interval at which to blink (milliseconds)


void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);  
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  

  
  
  unsigned long currentMillis = millis();
  
  Serial.print(interval); 
  Serial.print(" ");
  Serial.print(currentMillis - previousMillis); 
  Serial.print(" ");
  Serial.println(ledState); 

  //if(currentMillis - previousMillis > 1000) {
  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;
      interval = 1000;
    }
    else
    {
      ledState = LOW;
      interval = 700;
    }
  }

You have only modified the ledState variable. There must be a digitalWrite statement after that. Look at the sample sketch.

I am not following what your saying. Can you try and show me?

Look at the last few lines in the BlinkWithoutDelay example in the IDE. You forgot to include the digitalWrite function to actually change the state of the pin.

it blinks but still in a symmetrical blink not a "heart beat" asymmetrical blink.

http://www.gammon.com.au/blink

For a heartbeat, don't you just want bo-bomp ... bo-bomp ... etc?

So you want blink / short delay / blink / long delay etc.

So in the blink without delay, alternate the delay times.

well i am looking for a code that uses no delay (blink WITHOUT delay) so alternating my delay time doesn't really help me..

Would someone mind just including the full code. i have been messing with this for a week and half and can't get it. I would greatly appreciate it.

You were very close! Did some tweaking...

// constants won't change. Used here to
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
boolean ledState = LOW;             // ledState used to set the LED
unsigned 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.
unsigned long interval = 1000;           // interval at which to blink (milliseconds)


void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.

  unsigned long currentMillis = millis();

  Serial.print(interval);
  Serial.print(" ");
  Serial.print(currentMillis - previousMillis);
  Serial.print(" ");
  Serial.println(ledState);

  if (currentMillis - previousMillis > interval) {

    // save the last time you blinked the LED
    previousMillis = currentMillis;
    ledState = !ledState;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
    {
      digitalWrite(ledPin, HIGH);
      interval = 100;
    }
    else
    {
      digitalWrite(ledPin, LOW);
      interval = 1000;
    }
  }
}

It turned out to be surprisingly challenging. :)

I used a state machine in the end before I went completely insane.

http://www.gammon.com.au/statemachine

We have four states, as I see it:

BEAT PAUSE BEAT LONG_PAUSE

That's the bo-bomp ... bo-bomp ... part

So the state machine below cycles between 4 states, going:

  • High (short time)
  • Low (short time)
  • High (short time)
  • Low (long time)

You can tweak the intervals below to get a more natural look.

const byte ledPin =  13;      
unsigned long previousMillis = 0;       
const unsigned long shortInterval = 200;
const unsigned long longInterval = 800;

bool shortBeat;

void setup() 
{
  pinMode(ledPin, OUTPUT);      
}

byte state;
long interval;          

void loop()
{
int ledState;           
  
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis >= interval) 
    {
    previousMillis = currentMillis;   

    switch (state++ & 3)
      {
      case 0:
        ledState = HIGH;
        interval = shortInterval;
        break;
        
      case 1:
        ledState = LOW;
        interval = shortInterval;
        break;
        
      case 2:
        ledState = HIGH;
        interval = shortInterval;
        break;

      case 3:
        ledState = LOW;
        interval = longInterval;
        break;
      }  // end of switch
    
    digitalWrite(ledPin, ledState);
  }
}

Ninja'd by dlloyd. :P

Ninja'd by dlloyd. :P

Although his code doesn't look like a hearbeat to me.

Think of the rhythm ...

Uh-oh ... I forgot pacemaker()!

Nick Gammon Your code works perfect!!! Thank you so much!!! You are a life saver! :grinning:

If you specify what you need it would far more easy to help you. So attach a timing diagram or a state diagram. Also you should do this in the first place since it helps you structure your code

Another take on the same problem

const byte ledPin =  13;
unsigned long previousMillis = 0;
const byte states[] = {HIGH, LOW};
long intervals[] = {200, 200, 200, 800};
byte state;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= intervals[state % 4])
  {
    previousMillis = currentMillis;
    digitalWrite(ledPin, states[state++ % 2]);
  }
}

Nice. A bit more configurable than mine. :)

Thanks. I had fun doing it. I was amazed by how much code I could take out by using the arrays, the modulo operator and sneaky stuff like incrementing the state as part of the digitalWrite() and doing the modulo as part of the array index, although that is starting to border on making the code unreadable and in practice would probably be better as separate statements.