LED "pulse" instead of "blink"

Hi Guys,

I've made this little "starter" code, based on an example from Arduino IDE :

const int ledPin =  13; 
int ledState = LOW;
unsigned long previousMillis = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  unsigned long currentMillis = millis();
  int interval = map(analogRead(0), 0, 1024, 1000, 10);
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  }
}

It is a little nothing, but a really good starting point for my Master Clock generator for my modular synth.

My question is that is it possible, somehow, make the LED just pulse, instead of blink?
Right now, it is either on or off, my target is to make it just "pulse", around 7 ms long.

I saw a video on youtube, but there was no code example unfortunately :S

To make the pin "pulse" for 7 ms:

digitalWrite(ledPin, HIGH);
delay(7);
digitalWrite(ledPin LOW);

Actually, that will be more than 7 ms because of the overhead of the delay command and the second digitalWrite command. If you really exactly 7ms you may be able to achieve it but you would need to play with the "7" and view the pulsing with an oscilloscope.

 if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  }

When you change the state of the LED, you can change the value of "interval" too

Wow, it is soooo simple, thank you JaBa!!!
You've just made my day.

Ok, next problem :smiley:

I've expanded my code, with another pot.

As you can see, the pot generates a number between 10 and 100, and compares it into a random generated number (0-100) every time if "tick" is HIGH.
If "density" bigger than "comparator" then it sends a "pulse" to digital out 4.

The problem is that it is not working...

I think the problem is that I'd like write them in the same time maybe?

const int tick = 3;
const int tock = 4;
 
int tickState = LOW;
int tockState = LOW;
unsigned long previousMillis = 0;

void setup() {
  pinMode(tick, OUTPUT);
  pinMode(tock, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  unsigned long currentMillis = millis();
  int clockspeed = map(analogRead(0), 0, 1024, 1000, 10);
  if (currentMillis - previousMillis >= clockspeed) {
    previousMillis = currentMillis;
    if (tickState == LOW) {
      tickState = HIGH;
      digitalWrite(tick, tickState);
      delay(7);
      digitalWrite(tick, LOW);
    } else {
      tickState = LOW;
    }
    Serial.println(clockspeed);
  } 
}
void loop2 (){
    if (tickState = HIGH) {
      int comparator = random (0,100);
      int density = map(analogRead(1), 0, 1024, 10, 100);
      if (comparator < density) {
        digitalWrite(tock, HIGH);
        delay(7);
        digitalWrite(tock, LOW); 
        } else {
        tockState = LOW;
        }
      }
}

AWOL:

 if (currentMillis - previousMillis >= interval) {

previousMillis = currentMillis;
   if (ledState == LOW) {
     ledState = HIGH;
   } else {
     ledState = LOW;
   }
   digitalWrite(ledPin, ledState);
 }



When you change the state of the LED, you can change the value of "interval" too

Yes, but as you can see, interval is changed by a pot...

As you can see, this should be just an adjustable 0-5V, 7ms pulse generator, where you can adjust the pulsing frequency with the pot.

I've used the BLINK examples as starting point, if there are other, more simple methods, I'm open for them.

if (tickState = HIGH)

Oops

encoderaudio:
Yes, but as you can see, interval is changed by a pot...

As you can see, this should be just an adjustable 0-5V, 7ms pulse generator, where you can adjust the pulsing frequency with the pot.

I've used the BLINK examples as starting point, if there are other, more simple methods, I'm open for them.

But, as you can see, you've introduced a delay into a blink without delay.

AWOL:
But, as you can see, you've introduced a delay into a blink without delay.

You've got me :smiley:
How to do it without it?

On the other hands, thank you for the tip, now both LEDs are working!!!

const int tick = 2;
const int tock = 12;

int tickState = LOW;
int tockState = LOW;
unsigned long previousMillis = 0;

void setup() {
 pinMode(tick, OUTPUT);
 pinMode(tock, OUTPUT);
 Serial.begin(9600);
}
void loop() {
 unsigned long currentMillis = millis();
 int clockspeed = map(analogRead(0), 0, 1024, 1000, 10);
 if (currentMillis - previousMillis >= clockspeed) {
   previousMillis = currentMillis;
   if (tickState = HIGH) {
     tickState = HIGH;
     digitalWrite(tick, tickState);
     delay(7);
     digitalWrite(tick, LOW);
     int comparator = random (0,100);
     int density = map(analogRead(1), 0, 1024, 10, 100);
     if (comparator < density) {
       digitalWrite(tock, HIGH);
       delay(7);
       digitalWrite(tock, LOW);
       } else {
       tockState = LOW;
       }       
   } else {
     tickState = LOW;
   }
   Serial.println(clockspeed);
 } 
}

if (tickState = HIGH) {Oops

AWOL:
if (tickState = HIGH) {Oops

Ok, I don't get this...

"tick = HIGH" is an assignment, and because "HIGH" is non-zero, the value of the expression is always true.
In other words, the "if" is redundant - the following code will always be executed.

encoderaudio:
You've got me :smiley:
How to do it without it?

Very simply - as I pointed out, you have a different time interval for "on" than the time interval for "off"

AWOL:
"tick = HIGH" is an assignment, and because "HIGH" is non-zero, the value of the expression is always true.
In other words, the "if" is redundant - the following code will always be executed.

I see.
Could you change it, as an example, for a n00b :smiley:

I'd rather you thought about it a little longer - it really isn't difficult, and it won't take long to prototype.

You already change the state of the LED (on or off) at some time determined by the expression if (millis () - previousMillis >= interval).

At the same time as you're changing the state of the LED, you could change the value of the interval that the LED remains in that state.

AWOL:
But, as you can see, you've introduced a delay into a blink without delay.

Right? With a 7 millisecond full-stop in processing, the highest frequency this synth can handle is 140 hertz. This is a very bass instrument, like piano keys 30 and below.

encoderaudio:
... Could you change it, as an example, for a n00b :smiley:

You'll only be a noob your first day ea, so jump right in. AWOL is suggesting that you look at the code you have already posted, and see if you can use the millis()-previousMillis timing loop to also change the time between turning the output on and off, thus getting rid of that toxic "delay(7);" statement. That alone limits your timing to a very low frequency. It's a D3 note.

What are you trying to do - make an audio frequency, or make a time standard that other devices will use to synchronize their noisemaking?

AWOL:
I'd rather you thought about it a little longer - it really isn't difficult, and it won't take long to prototype.

You already change the state of the LED (on or off) at some time determined by the expression

if (millis () - previousMillis >= interval)

.

At the same time as you're changing the state of the LED, you could change the value of the interval that the LED remains in that state.

The interval is changed by pot, so, honestly, I have no better idea :frowning:

ChrisTenone:
Right? With a 7 millisecond full-stop in processing, the highest frequency this synth can handle is 140 hertz. This is a very bass instrument, like piano keys 30 and below.

You'll only be a noob your first day ea, so jump right in. AWOL is suggesting that you look at the code you have already posted, and see if you can use the millis()-previousMillis timing loop to also change the time between turning the output on and off, thus getting rid of that toxic "delay(7);" statement. That alone limits your timing to a very low frequency. It's a D3 note.

What are you trying to do - make an audio frequency, or make a time standard that other devices will use to synchronize their noisemaking?

Yes, I'd like to make a clock generator, which send 7 ms pulses to my modular synth. The pot should change the frequency of the pulsing, between 10 ms and 1 sec. (cca.)

The interval is changed by pot, so, honestly, I have no better idea

Instead of making the interval being changed by the pot, make an interval being changed by the pot, and the other interval being changed by the code

encoderaudio:
Yes, I'd like to make a clock generator, which send 7 ms pulses to my modular synth. The pot should change the frequency of the pulsing, between 10 ms and 1 sec. (cca.)

Sooo,

You want an output that looks like this:

Right?

And the the seven millisecond timing is important to ... what level? Since you can time with either milliseconds or microseconds, a precision of a hundred ppm or so seems like a reasonable limit.

ChrisTenone:
Sooo,

You want an output that looks like this:

Right?

And the the seven millisecond timing is important to ... what level? Since you can time with either milliseconds or microseconds, a precision of a hundred ppm or so seems like a reasonable limit.

That is right Chris, that is the target.

7 ms...you know, I just sad something. 10 ms is also OK.
Modern Buchlas using 4 ms triggers, in Eurorack modular the average is (I would say) 2-10 ms.
Basically, 7 was just a number, not critical, but it should be constant.