Duty Cycle (of pulse train signal through analog channel x )= pulseIn(x,HIGH)/(pulseIn(x,HIGH)+pulseIn(x,LOW)) + some constant
If you can represent that in the form of periods (time) of high / low signal, then yes.
What constant other than zero?
What's the constant for?
pulseIn waits at least one pulse so it takes at least 3 pulses to measure.
better write a dedicated function for dutycycle, something like:
unsigned long dutyCycle(uint8_t pin, unsigned long timeout)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
unsigned long widthHIGH = 0;
unsigned long widthLOW = 0;
unsigned long numloops = 0;
unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
// wait for any previous pulse to end
while ((*portInputRegister(port) & bit) == bit)
if (numloops++ == maxloops)
return 0;
// wait for the pulse to start
while ((*portInputRegister(port) & bit) != bit)
if (numloops++ == maxloops)
return 0;
// measure HIGH
while ((*portInputRegister(port) & bit) == bit) {
if (numloops++ == maxloops)
return 0;
widthHIGH++;
}
// measure LOW
while ((*portInputRegister(port) & bit) != bit) {
if (numloops++ == maxloops)
return 0;
widthLOW++;
}
return widthHIGH / (widthHIGH + widthLOW);
}
please give it a try.
KenF:
What's the constant for?
to fix the math
robtillaart:
to fix the math
Ah I see. If the results prove incorrect it merely means that the constant is wrong. Sounds like the perfect equation to me.
more serious, depending on the code used a constant is needed e.g. to adjust the time for initializing
sorry for late reply constant was for standard error based equal to the 1 unit == 3.89 mV i think. (i dont know what decimal you start getting errors using floats though
i am just trying to relate the quantities you get if you upload the following and observe the steps of convergence in the pattern you see on the serial feed.
#include <EEPROM.h>
int AVGLIST1[8];
unsigned long time;
unsigned long TIME[8];
unsigned long TIME0[8];
unsigned long TIME1[8];
void setup() {
Serial.begin(57600);
}
void loop() {
A1:
for(int k=0;k<8;k++){
int m=0;
AVGFIND(k);
AVGTIME(k);
TIME0[k-1]=TIME[k];
if(TIME0[k]=TIME0[k-1]){m=m+1;}
if(m>1){TIME1[k]=TIME0[k];m=0;}
if(TIME0[k]!=0) {
Serial.print(k);
Serial.print(" ");
Serial.print(AVGLIST1[k]);
Serial.print(" ");
Serial.print(TIME0[k]);
Serial.print(" ");
Serial.print(" ");}
}
Serial.println();
Serial.print(millis());
Serial.print(" ");
Serial.println();
}
void AVGFIND(int i)
{
float val;
float SUM;
float AVG=0;
{
for(int j=0;j<=512;j++){
val=analogRead(i)/4;
EEPROM.write(j,val);
SUM=SUM+4*EEPROM.read(j);
if(j==512){
AVG=(SUM)/512;
if(AVGLIST1[i]!=0){
AVGLIST1[i]=(AVG+AVGLIST1[i])/2;
SUM=0;
}else{AVGLIST1[i]=AVG;}
SUM=0;
}
}
}
}
void AVGTIME(int i)
{
int val1;
int val2;
int j0=0;
if(TIME[i]!=0){
time = random(0,TIME[i]);}else{time = random(1000000);}
val1=analogRead(i);
long unsigned t1=micros();
delayMicroseconds(time);
val2=analogRead(i);
long unsigned t2=micros();
if(val1==val2){if(TIME[i]!=0){TIME[i]=min(TIME[i],t2-t1-time);}else{TIME[i]=t2-t1-time;}
}
}
im mucking around with an idea of amplification of an analog with a group of digital PWM pins.
my values are 429000 something 16600 then 112 and remains
please use code tags when posting code . It is the second icon from the right.
Or use [cide] [/cide] tags directly but then with an o instead of i
robtillaart:
please use code tags when posting code . It is the second icon from the right.
Or use [cide] [/cide] tags directly but then with an o instead of i
Rob you know you can show
[code]and [/code]
By simply surrounding them with
[nobbd] and [/nobbd] but with c instead of d
Great!, learned today how to use `[nobbc]```[/nobbc]