variable dimension

Hey!
I have to ask about my lack in programming.
I want to measure the duty cycle of a PWM.

int incomingAudio;//storage for A0 data
int pin = 8;
unsigned long duration;
int PERIODO = 20400; //period in microsecond
double duty =0;
int tau = 0;

void setup(){
  
pinMode(pin, INPUT);
Serial.begin(9600);
}

void loop(){
 incomingAudio = analogRead(A0); 
  
  //do other stuff here
 analogWrite(3,incomingAudio >> 2);
 
   tau = pulseIn(pin, HIGH);
   duty = (tau/PERIODO)*100;
   Serial.println(tau);
   Serial.println(duty);
   delay(1000);
}

pulseIn is in micro right? the frequency of my PWM should be 20,40ms so I chqnged it in micro.

the result sometimes is higher then 2. how come?
Thqnk you!!!

What sort of values are shown for "tau"?

tau = pulseIn(pin, HIGH);

"pulseIn" returns "unsigned long", but "tau" is an "int".

You're doing integer division here:

   duty = (tau/PERIODO)*100;

Either make tau float or at least multiply first:

   duty = tau * 100 / PERIODO ;

or you'll simply see zero

int PERIODO = 2040; //period in microsecond
float duty =0;
unsigned long tau = 0;
 tau = pulseIn(pin, HIGH);
   duty = tau * 100/ PERIODO ;

And I obtain for tau 190 175 47 88
For duty: 2.00 7.00 14.00 ect

I change the PERIODO putting one wero less.
my PWM frequency is 20.40ms I measured it with the oscilloscope. And PulseIn is in micro.. so I think the right period should be 20400. but there is one mistake.

You're only measuring the HIGH portion of your PWM cycle, aren't you?

Why is this thread called "variable dimension"?

I am not English mother tounge and I what I meant by Dimension was long int char ect.
SO I wanted clarified first of all that.
And I needed to find the right variable "dimension" in order to proceed with my project... measure of duty cycle! :slight_smile:

"Type of variable" or "data type" :slight_smile:

Thank you a lot Hackscribble!!!!
:slight_smile: