Go Down

Topic: Array for Pulse Width Values (Read 2997 times) previous topic - next topic


Apr 12, 2013, 02:53 pm Last Edit: Apr 12, 2013, 02:56 pm by jt002 Reason: 1
I'm working on a project and have created a sensor that outputs pulses with various amplitudes and widths.

I'd like to write the input pulses to an array so that I can determine what values the micro-controller is reading based on the input. Attached is an image of what the pulses look like from a scope with 2V div and 5ms div. The green values are the input to the micro-controller.

Would the following code be a good way to capture all of the pulses into an array?

I'm going into the lab later tonight to work on this and wanted some input before testing.

Ideally the code will write 100 pulses to an array and when the count reaches 100 it will write it to serial. Thanks for any input!

Code: [Select]

int pinIn = 0;                  // Interrupt 0 on D2
int pin = 2;                 //PulseIn D2
long pulseArray[100];  //Array for capturing 100 pulses
int count = 0;
long duration = 0;

void pulseinArray()
duration = pulseIn(pin,HIGH);
pulseArray[count] = duration;

void setup()
pinMode(pin, INPUT);

//Attach the interrupt to the input pin and monitor for HIGH
attachInterrupt(pinIn, pulseinArray, HIGH);

for (int i = 0; i < 100; i++)
 pulseArray[i] = 0;

void loop()                    
if (count > 100)
for (int i = 0; i < 100; i++)
 Serial.print("Pulse Width ");
 Serial.print(" ");

for (int i = 0; i < 100; i++)
 pulseArray[i] = 0;
Serial.println("END ARRAY PRINT");
count = 0;



Hi,  you should declare all variables used to exchange Data from the ISR with loop()  as volatile. The array with long will use 400byte memory. Depending on the arduino you are using this can become an issue in case u extend your Sketch in Future.
Also check the Sensor and storage Forums, there are some threads how to gather Data.
I'm not sure if this combination of ISR and PulseIn works, just try.
Best, Robert

Go Up