# duration on metro lib

hi

I used metro lib to set a pulse sequence. my way is to assign "bpm" and control the duration. the idea is to set high the output than the metro interval and set low than the duration. i have do the code below who do the stuff but have wobbly result. what is wrong on my code ? thanks

``````// Instantiate a metro object and set the interval to 250 milliseconds (0.25 seconds).
Metro metro0 = Metro(500); // set metro interval
int duration = 100;

void setup()
{
Serial.begin(9600);

pinMode(LED0,OUTPUT);

}

void loop()
{

currentMillis = millis();

if (metro0.check() == 1) {  // check if the metro has passed its interval

digitalWrite(LED0,HIGH); // output is high
}

if (currentMillis - lastTime  > duration) {
digitalWrite(LED0,LOW);
lastTime = currentMillis;
}
``````

You aren't really controlling the pulse duration, you're just turning the pulse off at a fixed interval after you turned it off last time - but not accounting for processing delays.

Given what you're trying to achieve, I suggest you simply use the technique demonstrated in the 'blink without delay' example sketch to start the pulse at regular intervals, and end the pulse after the length exceeds your desired duration.

Also, if keeping strict phase is important to you, I suggest that when you detect the interval has passed you update the lastTime value by incrementing it, rather than by setting it to the current time:

``````if(millis() - lastTime >= period)
{
lastTime += period;
... other stuff ...
}
``````

Surely you meant to set lastTime when you switch the LED on, and not after you switch it off, otherwise you are switching if off every 100ms without any synchronization to the point where its switched on. You want the LED on for 100ms every 250ms?

hi

thanks MarkT, move -lastTime- make run good. yes, need to synchronize it with ON process...

You want the LED on for 100ms every 250ms?

yes that's right, but interval and duration can change in the time, I using sensor for control it...

code is like this :

``````unsigned long lastTime = 0;
int duration = 100; // duration time - Led is ON -
unsigned long currentMillis;

Metro metro0 = Metro(500);

void setup()
{
Serial.begin(9600);

pinMode(LED0,OUTPUT);
}

void loop()
{

currentMillis = millis();

if (metro0.check() == 1) {  // check if the metro has passed its interval (default = 500 ms)

digitalWrite(LED0,HIGH);
lastTime = currentMillis;
}

if (currentMillis - lastTime  > duration) {
digitalWrite(LED0,LOW);
}
``````

Thanks & best 0-0

hi

another basic question :
for Init digital output I used :

``````int solePin[] = { 2, 3, 4, 5 , 6, 7, 8 ,9}; // define solenoide pin connections

void setup(){

// init output for solenoide  ======================== //
for (int id = 0; id < 8 ; id++)
{
pinMode(solePin[id],OUTPUT);
pinMode(solePin[id],LOW);    // init as low
}

void loop(){
digitalWrite(solePin[0],HIGH);
...
}
``````

but the init on setup don’t run.

if i used on setup

``````  pinMode(solePin[0],OUTPUT);
pinMode(solePin[1],OUTPUT);
pinMode(solePin[2],OUTPUT);
pinMode(solePin[3],OUTPUT);
...
pinMode(solePin[7],OUTPUT);
``````

run good.

what is wrong on my first code ???

thanks

but the init on setup don't run.

You have some proof of this?

ok ! if i comment

pinMode(solePin[id],LOW);

setup run good.

but how make to init output as low, because when I start my program output are HIGH. I have try to make in two time with two for ( 1st for init as output, 2nd for init as low) but if I do that, program don't run.

How i can init output as low ? thanks

You have provided no proof, or even any evidence, that the pins are not properly set LOW when that code is in place. You really need to stop making claims that you can not support.

If you have some proof, or even some evidence that does not constitute proof, show it.

PaulS, I don't claims nothing and I'm too bad on programming (I'm artist) to proof anything on arduino or in programming. It's not who I search and not what I want to do.

I just try to know why output are HIGH when i run the program and why if I write : pinMode(solePin[id],LOW); nothing run. Sure the problem is on my program. I just want to put aside part for find where problem can come, and resolve it.

sorry...

0-0

PaulS, I don't claims nothing

Yes, you did - you claimed that setup didn't run correctly, but as PaulS pointed out, you provided no proof, and posted only a fragment of code to "illustrate" your point. If you want help, then post all the code. Did you perhaps mean to write

``````pinMode(solePin[id],OUTPUT);
digitalWrite(solePin[id],LOW);    // init as low
}
``````

?

oran_outan: but the init on setup don't run.

if i used on setup ... run good.

I'm sure you'll find the setup code runs in both examples you gave, but may simply not be doing what you intended. In the second example you do not set the output states to LOW, so the two examples do not have identical effect. Could it be that setting the output LOW causes some problem?

Does this make anything clearer?

``````#define HIGH 0x1
#define LOW  0x0

#define INPUT 0x0
#define OUTPUT 0x1
``````

when you substitute those values into your original code

`````` pinMode(solePin[id],OUTPUT);
pinMode(solePin[id],LOW);    // init as low
``````

(which is why I suggested "digitalWrite" instead of the second "pinMode", up there ^^^ )

hi all

yes, is that, I need : digitalWrite and not pinMode. effectively, this make LOW on setup.

thanks