/* Object starts a pendulum swing between ldrPin1 and ldrPin2 going from
right to left. Total recorded time from start to finish
*/
const int THRESHOLD_LOW = 650; // Threshold of amount of light in area, can be adjusted to fit lighting conditions
const int THRESHOLD_HIGH = 700; // Same as above
int ldrPin1 = A0; // object swings right, shadow passes over LDR starting timer
int ldrPin2 = A5; // object swings left, shadow passes over LDR stopping timer
int HystereticRead (int pin, int previousState) {
Ok, I have it all set up. I will try that now, I will remove the static double and replace it with unsigned long and see if this works. Im kinda new at this and do not really have much knowledge about all this. I just had a project and was told that this Arduino could help.
Please always do a Tools > Auto Format on your code before posting it to the forum. The random indentation of the code you posted makes it very difficult to read, and thus less likely for you to get help.
I see you tried to use code tags, which is good, but you didn't manage to use them correctly. You can click the "Preview" button to make sure the formatting of your post is correct before posting it.
You need to quickly learn to use the Serial.print() to show you the contents of variables and to identify the exact locations in your code that is being executed. Called DEBUGGING.
I just tried the unsigned long and all it does is record my time in a digital type state in the serial monitor e.g. 1 or 0 so that does not work. Again everything works as it should however the 2nd pass over the ldrPin1 resets the timer and starts from the new start time and records from there. I wish I could post a video but we cannot exceed 2Mb on this forum which limits "us" to still photos sometimes in a pixelated state
The way that I would deal with this problem is to perhaps record the timestamp on every instance that one of the sensors is triggered above/below a specific threshold. Also create a counter to record the number of times the sensor is triggered. And every time the sensor is triggered, you would print to the serial monitor both the counter for that sensor and the timestamp.
Then you can work out the program logic from there.
Like introducing a "cycle" variable, which only increments after x number of triggers.
And then perhaps to only record the STARTING timestamp when the cycle variable is an odd number and the LDR1 counter is an even number. Or whatever pattern you see fit.
First work out the pattern, then program the code accordingly.
Thank you that does give me some other ideas to try out. Did not know about the "cycle" but will mess with that and try it out. Again thanks for all the advice.
The logic which pulls millis() into time0 is only executed once when the ldr 'sees'. But, by going past it you're changing new_state back to the start state and so causing time0 to be loaded again. Create a boolean which has a beginning state of false and include boolean == false in the conditions which load time0. Set the boolean true when time0 is loaded. When ldr2 changes state and the time is calculated also reset the boolean.
Oh...that's how you do the code tags... Ok I didn't know. I will post it it now but I did CNTRL T to format it but I'll do it again right now. Thank you again.
I think this is what you are trying to do.
I hope it does not confuse you.
It is easier to read in the Arduino IDE - just copy and paste into your IDE, and see if it does what you want.
/* Object starts a pendulum swing between ldrPin1 and ldrPin2 going from
right to left. Total recorded time from start to finish
*/
const int THRESHOLD_LOW = 650; // Threshold of amount of light in area, can be adjusted to fit lighting conditions
const int THRESHOLD_HIGH = 700; // Same as above
int ldrPin1 = A0; // object swings right, shadow passes over LDR starting timer
int ldrPin2 = A5; // object swings left, shadow passes over LDR stopping timer
boolean pin1Trigger=false;
boolean pin2Trigger=false;
double totalTime = 0;
double startTime = 0;
double stopTime = 0;
void checkPin(int pin){
int pinRead = analogRead(pin);
if(pinRead>THRESHOLD_HIGH){
//an LDR has been triggered
if(pin == ldrPin1){
pin1Trigger=true;
pin2Trigger=false;
} else {
pin1Trigger = false;
pin2Trigger = true;
}
}
}
void setup() {
Serial.begin (9600);
}
void loop() {
totalTime = 0; //Reset the timer
//While both are false, keep checking for a trigger on LDR1.
while(!pin1Trigger && !pin2Trigger){
checkPin(ldrPin1);
if(pin1Trigger){
//start the timer when the shadow FIRST passes over the LDR on ldrPin1
startTime = millis();
//pin1Trigger is now true - and therefore will break the while loop.
}
}
//While the LDR1 is true, and LDR2 is false, keep checking for a trigger on LDR2
while(pin1Trigger && !pin2Trigger){
checkPin(ldrPin2);
if(pin2Trigger){
//Stop the timer when the shadow FIRST passes over the LDR on ldrPin2
stopTime = millis();
//pinTrigger will now be true - and therefore exit the while loop
}
}
totalTime = (startTime - stopTime)/1000;
Serial.print ("time passed: ");
Serial.print (totalTime);
Serial.println(" (s)");
//Reset the triggers for the next cycle
pin1Trigger = false;
pin2Trigger = false;
}