0
votes

I am programming Arduino to know the time between high to low or low to high. But i am getting constant values from timer interrupt value. It was 1246*10ms= 12460. What is wrong with this code. I am tracking my signal with oscilloscope. Actual time was an approximately 250ms. Help me please.

#include <TimerOne.h>

void setup(void)
{
  pinMode(7, INPUT);
  Timer1.initialize(100);//1000000=1s
  Timer1.attachInterrupt(blinkLED);
  Serial.begin(9600);
}


int ledState = LOW,T=0,state0=0,state1=0;
volatile unsigned long blinkCount = 0;  variables

void blinkLED(void)
{
  T++;
}

void loop(void)
{
  state0=state1;
  state1=digitalRead(7);
  if(state0!=state1)
  {
    //Serial.print("state1=");
    //Serial.print(state1);
    //Serial.print("  T=");
    Serial.println(T);
    T=0;
  }
}
1

1 Answers

0
votes

Firstly please make sure the code you post is valid. I think the 'variables' should be a comment? Also your code is not very readable (please add some comments).

For measuring the length of pulses Arduino has a built in function called 'pusleIn()'. Have a look at he documentation.

If you want to measure high and low pulses you should also have a look at 'attachInterrupt()' with the mode set to CHANGE.

I hope this helps you out a bit.