Go Down

Topic: input TCRT5000L output serial print of the input high time period in msec (Read 3755 times) previous topic - next topic

PaulS

Time value, whether from millis() or micros() are unsigned long, not unsigned int.

mikegd

Hi

I tried the micro, changing %d to %u, and long instead of int in all combinations.
The best results are still the code of Nov6. Changing to micro was good for the crossing
but not for the interval. If a code could be designed changing the crossing only to micro leaving the
interval on millis I think that will do.

PaulS

Quote
I tried the micro, changing %d to %u, and long instead of int in all combinations.
But, I'm not going to tell you what happened, or what the code looks like...

Quote
but not for the interval. If a code could be designed changing the crossing only to micro leaving the
interval on millis I think that will do.
That puts you in the minority, then.

cedarlakeinstruments

PaulS is correct, the timing functions are ulong, not uint. Sorry about that. I figured this would be over and done with in five minutes, but I don't want to leave you stuck now. But hey, at least it's free :-)

What's the purpose of this exercise, anyway?

Code: [Select]

unsigned long _lastBall = 0;

 void setup()
 {
  // put your setup code here, to run once:
   Serial.begin(9600);
   pinMode(8, INPUT);
   Serial.println("Starting up...");
}

void loop() {
  // put your main code here, to run repeatedly:
 
 unsigned long transition = 0;
 unsigned long start = 0;

  char log[50];
  Serial.println("Waiting for ball");
  // Wait for ball
  while (digitalRead(8) == LOW);
  start = millis();
  Serial.println("Got ball");

  // Ball at sensor, wait for it to pass
  while (digitalRead(8) == HIGH);

  // Record time of crossing. Cast to unsigned int to handle rollovers
  transition = (unsigned long)( millis() - start);
  // Get time since last ball seen
  sprintf(log, "crossing %u, interval %u", transition, (unsigned long)(start - _lastBall));
  _lastBall = millis();
 
  // Print data
  Serial.println(log);
}
Electronics and firmware/software design and assistance. No project too small

PaulS

Code: [Select]
  transition = (unsigned long)( millis() - start);
There is no need to cast an unsigned long to unsigned long.

cedarlakeinstruments

Electronics and firmware/software design and assistance. No project too small

mikegd

Hi cedarlakeinstruments
I am trying to organise myself so I can give you better feedback.
It must be difficult trying to design a sketch when you cannot see the results.

if you see attachment SKETCH NOV 10 . The sketch is on the left the resulting log
printed on the right.
As you can see the the interval is 0
I removed the "Waiting for ball" and "got ball". See SKETCH NOV 10A.
I then changed the millis to micros. see SKETCH NOV 10C.
This is as far as I can go in changing the code. The crossing reading is excellent
even though a little erratic ( I think due to it being reflective on a ball bearing),
 I may have to change the sensor, but that's for later. The interval is 0 for some reason.

The closest sketch to achieving the aim is SKETCH NOV6A but when I change the millis to micros we get
SKETCH NOV6B in which the crossing when reaching 32000 switches to negative and the interval is all over the place.

The aim is to design a sensor which detects successive rolling balls the crossing of the ball (around 30ms) and the interval between balls (around 12 sec), preferably the crossing logged in microsec and the interval in millisec.
The reason the interval in NOV 6A is around 500ms is because I have a small test rig which is essentially a swinging pendulum to simulate the passing of ball. So we expect the crossing to get longer as the pendulum slows with the interval remaining constant.
All this is to aid in the design and building of a recirculating roller ball sculpture.
Hope this format is of help and thank you again for your time and patience.

PaulS

Quote
if you see attachment
You seem to have forgotten to actually attach anything...

mikegd


mikegd

Hi
Apparently the attachments cannot handle .docx so converted to .doc, and try again.

PaulS

Quote
Apparently the attachments cannot handle .docx so converted to .doc, and try again.
Well, I can't handle doc files, either. What is wrong with a simple .txt file?

mikegd

Hi
I will resend in .txt

PaulS

Time functions, like millis() and micros() return values. It behooves you to pay attention to the type value that they return.

mikegd

Hi cedarlakeinstruments
I have improved my feedback, can you see why in SKETCH NOV 10C the interval returns a 0, when
it should be around 500millis.

cedarlakeinstruments

Honestly, it looks like it's cycling through the loop with a 1 millisecond period continuously which is why that number keeps incrementing. The interval is zero because the high and low transitions are being seen within one millisecond.

Your sensor is possibly noisy and emitting 1 & 0 frequently enough that the sensor transitions are being seen immediately.

Electronics and firmware/software design and assistance. No project too small

Go Up