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!!!
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.
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!