Go to Post Read the rules. And then read the rules again. Then, get together as a group and read the rules together. - Knufire [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

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 22-11-2015, 10:19
Ragingenferno's Avatar
Ragingenferno Ragingenferno is offline
That one guy
AKA: Josh
FRC #3053 (Stingers)
Team Role: Programmer
 
Join Date: Feb 2014
Rookie Year: 2012
Location: NYC
Posts: 39
Ragingenferno is just really niceRagingenferno is just really niceRagingenferno is just really niceRagingenferno is just really niceRagingenferno is just really nice
Timing an LDR in C++

I'm so confused on this topic. I'm doing this for a arduino mega (pretty sure arduino is C++) What I'm trying to do is this: Read the value back from the LDR (Light Dependent Resistor) and start a timer for when the value is in a certain range and perform a function if the time recorded is a certain amount of time.

For example: I will have the LDR taped to a screen. SO when the screen is black (LDR value is about 120-150), it starts timing how long it was black for and keeps timing until the screen displays light (LDR 600 - 620). Hypothetically, lets say the screen was black for about 12000-12600ms, if the time recored falls between that, it does something but if its above 12600ms it does something else. (If, else statement)


However, all I know how to do is get the value of the LDR, I don't know anything else.

Can someone help me with the code for the timing function, I'm lost.

If someone can pleaseeeeeeee help that would be the best thing ever
  #2   Spotlight this post!  
Unread 22-11-2015, 15:45
dubiousSwain's Avatar
dubiousSwain dubiousSwain is offline
The ride never ends
AKA: Christian Steward
FRC #5420 (Velocity)
Team Role: Mentor
 
Join Date: Oct 2011
Rookie Year: 2011
Location: USA
Posts: 304
dubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond repute
Re: Timing an LDR in C++

Hmm...
I don't know much about C++, but I would start by triggering a timer function when the sensor reads black, then when the sensor reads white, write the timer function's time to a variable then use that in an elif statement (excuse my python)

something like:
Code:
void main() {
      if (sensor<black){
            startTimer();
      }
      if (sensor == white && timerStarted == true){
                if (timerValue<12600){
                        doSomething();
                }
                if (timerValue>12600){
                         doSomethingElse();
                }
         }
 }
PM me if you need anything else
__________________
2015 MAR District Champions




  #3   Spotlight this post!  
Unread 23-11-2015, 20:59
jgrindle's Avatar
jgrindle jgrindle is offline
Losing Sleep
AKA: John Grindle
FRC #5122 (RobOTies)
Team Role: Programmer
 
Join Date: Nov 2014
Rookie Year: 2015
Location: Old Town, ME
Posts: 34
jgrindle will become famous soon enough
Re: Timing an LDR in C++

Quote:
Originally Posted by dubiousSwain View Post
Hmm...
I don't know much about C++, but I would start by triggering a timer function when the sensor reads black, then when the sensor reads white, write the timer function's time to a variable then use that in an elif statement (excuse my python)

something like:
Code:
void main() {
      if (sensor<black){
            startTimer();
      }
      if (sensor == white && timerStarted == true){
                if (timerValue<12600){
                        doSomething();
                }
                if (timerValue>12600){
                         doSomethingElse();
                }
         }
 }
PM me if you need anything else
Dubios you pretty much got it in terms for C++, just in a not very psuedoy pseudo code. For C++'s elif it's just
Code:
if(somethinghappens){
	dosomething;
}else if(Somethingelsehappens){
	dosomeotherthing;
}
Does the same thing as your elif, but just not as fancy or short
__________________

FRC 2015 Season: Programmer, Electrical, PIDTuner, Safety Captain
FRC 2015 Off-Season: Programmer, CAD Designer, Driver, Drive Team Coach, Electrical, Mechanical, PIDTuner

Last edited by jgrindle : 23-11-2015 at 21:00. Reason: clarification
  #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.
  #5   Spotlight this post!  
Unread 24-11-2015, 12:41
dubiousSwain's Avatar
dubiousSwain dubiousSwain is offline
The ride never ends
AKA: Christian Steward
FRC #5420 (Velocity)
Team Role: Mentor
 
Join Date: Oct 2011
Rookie Year: 2011
Location: USA
Posts: 304
dubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond reputedubiousSwain has a reputation beyond repute
Re: Timing an LDR in C++

Quote:
Originally Posted by heuristics View Post
You'll probably want to use a a state machine and grab samples from your LDR every 20ms or so.
In FRC programming, I've noticed that nine times out of ten, state machines are the way to go
__________________
2015 MAR District Champions




Closed Thread


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 03:15.

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