Pages: [1]   Go Down
Author Topic: Array for Pulse Width Values  (Read 2378 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
 count++;
}



void setup()
{              
   Serial.begin(115200);
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(i);
  Serial.print(" ");
  Serial.println(pulseArray[i]);
}

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

}}




* pulses.jpg (321.38 KB, 1632x918 - viewed 26 times.)
« Last Edit: April 12, 2013, 07:56:10 am by jt002 » Logged

Offline Offline
Newbie
*
Karma: 3
Posts: 39
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Pages: [1]   Go Up
Jump to: