Is it possible this project with arduino?

Hi,I am arduino beginer and I have question for arduino experts.

I have square wave signal with time from 2ms to 15ms and I need delay this signal in 50% .I mean if input signal time is 4ms output signal time will be 6ms,or if input time is 10ms I nedd on output pin 15ms signal time.The better option in this project would be if I could change the delay time from 30% to 50% with potentiometer.Is it possible read signal time and delay on this way with arduino and what board I need.

Thanks.

Yes this is possible. These times are well within the speed of Arduinos.

There are commands that allow you to read the length of the pulse and therefore extend the time.

How accurate does it need to be?

Weedpharma

There is a big difference between delaying a signal and extending the length of it. Which do you want to do?

I can envisage a problem with extending the length because it would mean data comes in faster than it goes out - with obvious consequences.

What is the project about?
You need to give us a lot more information.

...R

My project is about extending signal from petrol engine ECU.This ECU have 6 injectors.I need 6 input and 6 output on arduino board + one analog input for potentiometre with which I change the injection timing.

For now I have arduino UNO BUONO R3.The question is do I can use digital pin for input and output?I think I must use pins 2,4,7,8,12,13 for input and PWM pins 3,5,6,9,10,11 for output.

If there is a problem like @Robin2 say with extending the length because data comes in faster than it goes out.In this case I think that is beter way to read pulse and extend next pulse(if arduino can do that).
To explain:

The distance between the pulses is min.-2,85ms(on 7000 rpm)max.28ms(on 700 rpm).Is this time of 2,85ms enough to read and send data to extend next pulse?

P-1;read pulse time 1 and extend time on output 2
P-2;read pulse time 2 and extend time on output 3
P-3;read pulse time 3 and extend time on output 4
P-4;read pulse time 4 and extend time on output 5
P-5;read pulse time 5 and extend time on output 6
P-6;read pulse time 6 and extend time on output 1

Please help me how I do that and explain me comand for read time of pulse and how extended pulse time.
Thanks.

It may help if you drew a couple examples showing the mark and space of the input signal and the mark and space of the output signal. It sounds like you are only trying to extend the pulse into the gap before the next pulse. Maybe like this diagram ?

input:
     ______                     _____
_____|     |___________________|     |_________________


output:
       _________                   ________
_______|        |_________________|        |_________________

Extending a pulse just means following it exactly on a leading edge, and adding a delay on the falling edge.

As far as I can see from your description so far there PWM is not appropriate.

Howver I think all the inputs will have to trigger pin-change interrupts.

If you are reading pulses on 6 input pins and generating the output on 6 output pins I don't think there is a prblem with the input coming in too fast. I had assumed there would only be 1 input and 1 output pin.

My sense of how to tackle the problem is something like this (for one I/O pin)

detect the rising edge of the pulse
make the output pin HIGH
save the value of micros()

detect the falling edge of pulse
save the value of micros() in another variable // I assume this is the gap you want to extend
calculate the difference for the width of the pulse
using the calculated pulse width and the input from your potentiometer
calculate the output pulse width
after the pulse width period
make the output pin LOW
repeat for ever

You will need to do this separately for every input.
Note that the output pulse must always be a bit longer than the input due to the time for the calculations

If this does not make sense post a drawing showing the waveforms for input and output that you want to achieve.

...R

PS My pseudo code is intended to do what @aarg said in Reply #5

The picture of signal waveforms look like this(on 800 rpm-leer).

high low on extended output I need high 3,2ms
2,5ms 147,5ms


| |_____________| P-1


| |_____________| P-2


| |_____________| P-3


| |_____________| P-4


| |_____________| P-5


| |_____________| P-6

|_ full cycle 150ms _|

Waveform on max. power and 6000rpm look like this:

input on extended output need this

high-13ms low-7ms high-17ms low-3ms


| |___| | ||

|_ full cycle 20ms _|

Robin2:
Note that the output pulse must always be a bit longer than the input due to the time for the calculations

I think there is no problem if that is small time.
Is this possible programing with arduino?

intercepter:
Is this possible programing with arduino?

That is why I described the program steps in Reply #6. Over to you now to have a go at writing the code.

...R

You make it sound easy.

If the OP using say an ATMEGA328P then there are only 2 interrupt pins. I guess that interrupts rather than simple pin polling is necessary here.
Anyway, that means that each of the pins representing a engine cylinder has also to be connected say with a diode to the chosen interrupt pin. On receiving an interrupt, the interrupt service routine has to determine which cylinder pin was triggered and whether it is a rising or falling edge.
Since the pulse length is dependent on the engine speed (RPM), is may also be possible to determine the pulse length from a table (or function) eliminating then need to detect the falling edge. Deriving the engine speed from the pulses for say cylinder 1 is a simple frequency measurement.

To increase reliability and eliminate any possible start lag, the existing pulse and the output pulse for a specific cylinder should probably be superimposed on each other.

6v6gt:
If the OP using say an ATMEGA328P then there are only 2 interrupt pins.

That is only correct regarding "external interrupts". All of the I/O pins can be configured to generate pin-change interrupts so there can easily be one for each cylinder.

...R

Robin2:
That is only correct regarding "external interrupts". All of the I/O pins can be configured to generate pin-change interrupts so there can easily be one for each cylinder.

...R

Thanks. In that case, I have learned something (more).

Robin2:
As far as I can see from your description so far there PWM is not appropriate.

Howver I think all the inputs will have to trigger pin-change interrupts.

If you are reading pulses on 6 input pins and generating the output on 6 output pins I don't think there is a prblem with the input coming in too fast. I had assumed there would only be 1 input and 1 output pin.

My sense of how to tackle the problem is something like this (for one I/O pin)

detect the rising edge of the pulse

make the output pin HIGH
save the value of micros()

detect the falling edge of pulse
save the value of micros() in another variable // I assume this is the gap you want to extend
calculate the difference for the width of the pulse
using the calculated pulse width and the input from your potentiometer
calculate the output pulse width
after the pulse width period
make the output pin LOW
repeat for ever




You will need to do this separately for every input.
Note that the output pulse must always be a bit longer than the input due to the time for the calculations

If this does not make sense post a drawing showing the waveforms for input and output that you want to achieve.

...R

PS My pseudo code is intended to do what @aarg said in Reply #5

I read attachinterrupt function and see there is 4 mode for triggered(RISING,HIGH,FALLING and LOW).
Is it possible detect high value and read time from this value on input pin,or I must detect rising and falling edge and read time between this two edges?
Can I use interrupt rising and falling trigger on same pin?
When I read input and save this time,how can extend this time.If you understand me I want on output extended time like this (injection time+40% of same time).How calculate this and change this percentage level with +/- 20% with potentiometer.
If there exist similar codes for this problem please show me.
Thanks.

attachInterrupt() is the wrong function. It only works on the interrupt pins and not on the pin-change interrupts. It's a subtle difference, but important. The ISR() macro is the one you want.

The pin-change interrupts will launch your interrupt code every time one of the pins on a "port" changes. You then have to read the values on the pins yourself to find out which one it was and whether it was rising or falling.

This code detect rising edge and make output high.
Is this corect and what is next?

const int in1 = 2;    
const int in2 = 3;
const int out1 = 12;
const int out2 = 13;      

void setup() {
 // initialize the LED pin as an output:
 pinMode(in1, INPUT);
 pinMode(out1, OUTPUT);
 pinMode(in2, INPUT);
 pinMode(out2, OUTPUT);
 // Attach an interrupt to the ISR vector
 attachInterrupt(0, ISR1, RISING);
 attachInterrupt(1, ISR2, RISING);
 
}

void loop() {
 // Nothing here!
}

void ISR1() {
 digitalWrite(out1, HIGH);  

}

void ISR2() {
 digitalWrite(out2, HIGH);

}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code only works with 2 input signals. I believe you need more than that, but it will do for learning.

The next thing, according to Reply #6 is to save the value of micros() (in a separate variable for each input) when the ISR triggers. That value will need an unsigned long variable.

...R

That looks like it might do what you think it does. Have you tested it with an oscilloscope?

Your original request asked to add a delay. You can't spend that duration waiting inside the ISR. Your ISR needs to record the time and then the main loop can look at the times and decide when to switch things on and off. The next step up from the code you have written is to set a variable in the ISR and have the main loop do the actual digitalWrite().

The better option in this project would be if I could change the delay time from 30% to 50% with potentiometer.

What's the purpose of delaying the signals?
Do you need to delay the rising, falling or both edges?
Do you need to change the pulse width?

This is picture from logic analyzer.
Channel 0 is input,channel 1 is inverted delayed output.Other inputs are ch.2,4,5,6,7.
If someone know how finish this,please edit my code.I have arduino mega and few arduino mini pro boards.If mega is not enough for processing all six input I can use 3x mini pro.Each for two input.

I am not sure to understand what you are trying to do.

Do you need to operate each of your 6 cylinders injectors with the same PWM signal (frequency/duty cycle) but delayed one from each other from 1/6 th of the period, AND modify frequency/duty cycle of the 6 PWM signals at the same time, depending on a received information from your ECU and if you want to accelerate or decelerate ?