Go to Post All I can say is - we are such geeks! - Raul [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Spotlight this post!  
Unread 23-11-2015, 22:13
heuristics heuristics is offline
Registered User
FRC #3634
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2014
Location: Trumbull, CT
Posts: 21
heuristics is on a distinguished road
Re: Timing an LDR in C++

You'll probably want to use a a state machine and grab samples from your LDR every 20ms or so.

Something like this:

Code:
const int BLACK = 0;
const int WHITE = 0;

int main()
{
	Timer t;
	LDR ldr;
	t.start();

	int state = WHITE;

	while (true)
	{
		int value = ldr.getValue();
		double blackStart = 0.0;
		
		switch (state)
		{
			case WHITE:
				if (value >= 120 && value <= 150)
				{
					blackStart = t.Get();
					state = BLACK;
				}
				break;
				
			case BLACK:
			{
				if (value >= 600 && value <= 620)
				{
					double timeDiff = t.Get() - blackStart;
					if (timeDiff >= 12.0 && timeDiff <= 12.6)
					{
						// do something
					}
					else if (timeDiff > 12.6)
					{
						// do something else
					}
					
					state = WHITE;
				}
			}
		}
		
		// sleep 20ms
		// This is C++11, not sure what the wpilib sleep API is.
		std::this_thread::sleep_for(std::chrono::milliseconds(20));
	}
}
Only problem is if your screen flashes white in < 20ms, you might not detect the change. You can try to offset that by reducing the thread's sleep time, or by getting rid of it completely, but then your program will peg the CPU.
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 22:34.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi