In my program i am using the pulseIn() function with the time out specified:
int pulsePin = 10;
unsigned long pHigh;
unsigned long pLow;
unsigned long interval;
void setup() {
pinMode (pulsePin, INPUT);
Serial.begin(115200);
}
void loop() {
for (int i = 0; i < 5; )
{
int see = millis();
Serial.print(see); Serial.print("\t: ");
pHigh = pulseIn(pulsePin, HIGH, 100000);
Serial.print(pHigh); Serial.print("\t:\t");
pLow = pulseIn(pulsePin, LOW, 100000);
Serial.println(pLow);
interval = pHigh + pLow;
if (interval > 1000) {
Serial.println(interval);
i++;
}
}
}
i am using a breadboard 328p set with internal clock at 8mhz, running serial at 115200 baud
if there is no pulse present i would expect that variable 'see' would increment at least 200ms plus a couple for the serial.prints.
but this is the result with timestamp:
13:56:01.530 -> 4544 : 0 : 0
13:56:01.671 -> 4683 : 0 : 0
13:56:01.811 -> 4823 : 0 : 0
13:56:01.952 -> 4962 : 0 : 0
13:56:02.093 -> 5101 : 0 : 0
13:56:02.233 -> 5240 : 0 : 0
13:56:02.374 -> 5380 : 0 : 0
13:56:02.514 -> 5519 : 0 : 0
13:56:02.655 -> 5658 : 0 : 0
13:56:02.795 -> 5797 : 0 : 0
13:56:02.936 -> 5937 : 0 : 0
13:56:03.077 -> 6076 : 0 : 0
13:56:03.217 -> 6215 : 0 : 0
returning about every 140ms
Is there something i'm missing about this timeout function, i've read somewhere that internal clock at 115200 is unreliable and should be used only with lower bauds?
Please post the ENTIRE code.
Regards,
bidouilleelec
try
pHigh = pulseIn(pulsePin, HIGH, 100000UL);
...
pLow = pulseIn(pulsePin, LOW, 100000UL);
aarg:
try
pHigh = pulseIn(pulsePin, HIGH, 100000UL);
...
pLow = pulseIn(pulsePin, LOW, 100000UL);
to no avail, I changed the baud just in case there is an issue with the internal clock, but its the same result. Also tried on a nano clone with no difference.
its not a problem at the moment just looking methodology because I will be using the timeout function.
When I run
int pulsePin = 10;
unsigned long pHigh;
unsigned long pLow;
unsigned long interval;
void setup() {
pinMode (pulsePin, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
unsigned long see = millis();
Serial.print(see); Serial.print("\t: ");
pHigh = pulseIn(pulsePin, HIGH, 100000UL);
Serial.print(pHigh); Serial.print("\t:\t");
pLow = pulseIn(pulsePin, LOW, 100000UL);
Serial.print(pLow);
interval = pHigh + pLow;
Serial.print("\t:\t");
Serial.println(interval);
}
0 : 0 : 0 : 0
138 : 0 : 0 : 0
277 : 0 : 0 : 0
415 : 0 : 0 : 0
555 : 0 : 0 : 0
693 : 0 : 0 : 0
832 : 0 : 0 : 0
970 : 0 : 0 : 0
1110 : 0 : 0 : 0
1249 : 0 : 0 : 0
1387 : 0 : 0 : 0
I don't see a problem with pulseIn there...
aarg:
[/code]
0 : 0 : 0 : 0
138 : 0 : 0 : 0
277 : 0 : 0 : 0
415 : 0 : 0 : 0
555 : 0 : 0 : 0
693 : 0 : 0 : 0
832 : 0 : 0 : 0
970 : 0 : 0 : 0
1110 : 0 : 0 : 0
1249 : 0 : 0 : 0
1387 : 0 : 0 : 0
I don't see a problem with pulseIn there...
except mills() suggests that 60ms+/- is missing from the timeout argument
I think it may have to do with there not being anything returned to pulseIn() and somehow it cuts short the low pulse. need to read more........
Internally, pulseIn calls
unsigned long maxloops = microsecondsToClockCycles(timeout)/16;
unsigned long width = countPulseASM(portInputRegister(port), bit, stateMask, maxloops);
maxloops counts cycles of a timing loop inside countPulseASM(). That makes 'timeout' an estimated, not exact timing value.