Find time duration between two pulse of same signal - arduino

i want to find time duration between two pulse of same signal. That's mean time duration between falling edge of first pulse and rising edge of second signal. Here, i'm using Arduino Uno board.
Here, Image show my two pulse of same signal. i want to find time t.
I want some logic or arduino code.

Check function pulseIn()
It will solve your problem.
Note: Just put LOW as parameter.

Related

Interrupt arduino when a particular value is read by a ultrasonic range finder

I'm new to arduino and currently using Arduino Uno. I need to interrupt arduino when a specific value is read by ultrasonic range finder. Since i'm entirely new to this i dont know anything on interrupts in arduino, I'm expecting a clear answer.
I assume that you are using SC-RS04 module, because that is what Arduino hobbyst usually use.
Acccording to the datasheet, the distance is encoded in the duration of an echo pulse Arduino will recive from the module.
To calculate the width of the pulse you will need to set up two interrupts.
One is RISING interrupt for catching the start of the pulse. The interrupt handler will use the millis function to record the time of the start of the pulse.
The other is FALLING interrupt for recording the end of the pulse. This interrupt handler will use millis function and previously recorded time to calculate the durantion and from that duration it will calculate the distance. If the distance is below a specific threshold, it will call your logic.
Read here about setting up interrupts.

Find time difference between two pulses using PIC16F628

I want to find time difference between two pulses using PIC16F628.
I am using a 4MHz external oscillator, MikroC compiler.
As a simple example let's assume there is a push button. When we press it, it sends a high signal to a pin. We press this button twice with some delay in between, I want to find the time difference between these two button presses.
Thank you.
As mentioned in the comments, the simplest way to do this is to use a timer/counter combo. I found this quick tutorial on how to do this specifically for PIC: http://www.mikroe.com/chapters/view/17/chapter-4-examples/#c4v5.
Have a look at 4.5 and 4.6, they give you exactly the information you'll need to get the count of timer intervals between pulses. The basic technique is to start a timer, associate an interrupt handler (Read: function) with the timer, and then increment a counter everytime the interrupt handler is called. Next time you see the pulse, read what the counter value is.
After that, all you need to know is the timebase you've set the counter to (which will be some integer subdivision of your oscillator rate, and is selectable in code usually) and you can convert # of timer intervals to time in seconds/millis/nanos.

Interrupt works too fast on the Arduino

I know this sounds a bit funny :). But I am trying to eliminate possibilities:
On the Arduino Uno I have attached an interrupt triggered on HIGH to a routine which only increments a volatile defined long counter. This counter is displayed on an LCD screen.
If I connect a pulse generator with a frequency of 1 Hz at TTL levels, I would expect the counter to increase with about 1 per second. However this is not the case.
As the frequency is 1 Hz (duty cycle 50%) could it be possible that once the counter is incremented the IRS is exited (and clears the interrupt flag) BUT: the INT0 level is still HIGH so the ISR would be called again? At 1 Hz 50% duty, the HIGH would stay for 500 ms and at 16 mHz...
The processor at the heart of any Arduino has two different kinds of interrupts: “external”, and “pin change”. There are only two external interrupt pins on the ATmega168/328 (ie, in the Arduino Uno/Nano/Duemilanove), INT0 and INT1, and they are mapped to Arduino pins 2 and 3. These interrupts can be set to trigger on RISING or FALLING signal edges, or on low level. The triggers are interpreted by hardware, and the interrupt is very fast. The Arduino Mega has a few more external interrupt pins available.
So as commented: It triggers on an edge!
See more details on the Arduino Playground web page.
Two electrical reasons can explain why interrupt does not function as you need.
1- The pulse generator output and MCU input can have an impedance mismatch, which can cause ringing on the waveform edges. For example, if your function generator has a 50 ohm output capable of generating high frequencies you might see a problem driving a high impedance input like the Arduino at low frequency.
The name "pulse generator" makes me think this is a 50 ohm out device intended to make very short pulses with sharp edges. In such a case, you add a terminating resistor at the destination (load) to match the impedance of the source (pulse generator). For a 50 ohm output, 47 ohm would be close enough. If the output is 100 kohm, then place a matching resistor at the Arduino.
2- Just the opposite, the generator waveform edges may be so slow that the voltage passes through TTL 0 to 1 transition multiple times. If you have noise on your signal input, a slow edge could be causing multiple triggers. For example, if you are picking up some 60 Hz ripple from a power supply and grounding issues, your square wave edges won't be as square as you think.
In such cases hysteresis is a solution. There are many ways to de-glitch (debounce) in code. There is no answer that is right for all problems. A simple example would be that the ISR you require that the input reads high twice in a row for the edge to be accepted.

Arduino encoder interrupts corrupting serial data

I have an Arduino Mega connected to a 6 axis robotic arm. All 6 interrupts are attached to encoders (one encoder pin on an interrupt, the other on a vanilla digital input). The interrupts are handled with this code:
void readEncoder1(){
//encoders is a 2d array, where the first d is the axis, and the two pin numbers
//first pin is on an interrupt (CHANGE), and second is a standard digital in
if (digitalRead(encoders[0][0]) == digitalRead(encoders[0][1])) {
positions[0]++;
} else {
positions[0]--;
}
if(servoEnable){
updatePositions(); //// compares positions[] to targets[] and adjusts motor speed accordingly
}
}
This is designed to keep the arm locked at a certain position- if the arduino detects that the position of the motor is off by a certain threshold, it updates the power going to the motor to keep the arm in position.
The problem is this, then -- if two or three (or more) axis are under load (requiring constant updating to stay in position) or they are moving, the Arduino will stop receiving intact commands on Serial input, several characters will be dropped. The interrupts are obviously running quite quickly, and for some reason this is causing commands to become corrupted. Is there any way around this? Architecturally, am I doing this right? My main instinct is to call updatePositions() in the main run loop at, say, 100 ms intervals, will this significantly reduce interrupt overhead? I guess what my question boils down to is how do I get reliable serial commands into the Arduino even if all 6 encoders are pulsing away?
Quadrature encoders were designed to be read by hardware counters. Pulse rates are generally high with the motor running at full speed. One megahertz is not unusual. The higher the number of pulses, the better the servo loop works and the more accurate you can position the motor.
Doing this is in software with a low-power cpu is, well, challenging. It will fall apart when the ISR takes longer than the interval between pulses. You'll lose pulses and thus position. Especially bad because there is no way you can detect this error condition. And that this loss happens when the robot is moving fast, the worst case condition to lose control.
You absolutely cannot afford to update the servo loop in the interrupt handler so get rid of that first. Keep the ISR to the bare minimum, only count the position and nothing else. The servo loop should be separate, driven by a timer interrupt or tick. You cannot properly control a robot with a 100 msec servo update unless it is big an sluggish, this needs to be a handful of milliseconds at most to get smooth acceleration and stable feedback.
There's a limited amount of wisdom in spending forty bucks to control thousands of dollars worth of robot hardware. Not being able to keep up in the servo loop is something you can detect, shut it down when the position error builds up too much. There's nothing you can do about losing pulses, that's a wreck. Get the hardware counters.
First rule of embedded systems:
Do as little as possible in interrupts.
In your case, just update the positions in the interrupt and run your position/speed control loop in the background or at a lower priority.
Aside: I assume you are aware that you are "losing" encoder pulses as you don't have an interrupt on one of the channels?
Also, interrupt-driven encoder-analysis is very noise-prone. If you get a noise pulse, you'll likely only see an interrupt for one of the edges as they'll be too close together to process both.
A more robust way is to use a state machine which watches all 4 transitions, but that requires either interrupts on both edges of both channels, or polling fast enough to not miss anything up the to rate you are expecting to see.

capture the incoming signal

i'm using msp430f2013 micro controller in my project.. in that i need to calculate the incoming train of pulse signal frequency.... i don't know how to do it.... can anyone help me in this.. example code is more usefull to me.... advance thanks for
You need to read the manual for the micro-controller, then work out how to set up a timer which can measure the interval between two pulse edges (e.g. from one leading edge to the next). The frequency, f, will be the reciprocal of this time interval, t, i.e.
f = 1 / t
There are various ways to do this, perhaps the simplest to understand is to setup a timer as a simple counter. Poll the input pin, when it changes state save the count on the timer, when it changes state again save the count on the timer, subtract one time from the other and that is how many clock ticks of some frequency X ticks per second. your difference is y ticks per input pulse. y / x the ticks cancel out and you get seconds per pulse. If you are measuring a full period rising edge to rising edge or falling edge to falling edge then it is the same solution the difference is which timer samples to subtract (last rising edge and current rising edge for example).
Some microcontrollers have the ability to interrupt when there is a state change on the input pin (or at least the same edge, rising or falling), and you may prefer to use that method to sample the timer, subtract and get ticks per period, etc to get cycles per second (frequency).
Using a timer can be tricky, I always start by using the timer to blink an led, first once per second to get in the ball park, then once every 5 or 10 or 30 seconds, and compare that to a second hand on a watch or some other reference to verify that you are accurate and not a dozen percent off this way or that. That establishes understanding of the timer and its divisor, from there you can start to work on using it to measure the input. to make sure I have the gpio programmed right (the led exercise covers some of that already) I sample the input pin and change the led state with the input pin state and can often then look at the led to see blinks or a dull glow to see that I am able to sample the gpio pin. then put it all together and sample the timer when the input changes state, first polling then if need be interrupts or other.

Resources