NEED HELP FINISHING MY CODE!!! CAN'T FIGURE IT OUT

/* 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) {

int photo = analogRead (pin);
if (previousState == LOW && photo >= THRESHOLD_HIGH) {
return HIGH;
}
else if (previousState == HIGH && photo < THRESHOLD_LOW) {
return LOW;
}
else {
return previousState;
}
}
void setup() {
Serial.begin (9600);
}
void loop() {
static int state0, state1;
static double time0, time1, time2;

int new_state = HystereticRead (A0, state0);
if (state0 == LOW && new_state == HIGH) {

time0 = millis ();

}
state0 = new_state;

new_state = HystereticRead (A5, state1);
if (state1 == LOW && new_state == HIGH) {
time1 = millis ();
time2 = (time1 - time0) / 1000;

Serial.println ("time passed: (s)");
Serial.println (time2);
}
state1 = new_state;
}``

Not sure, but
static double time0, time1, time2;
should be ‘unsigned long’.

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.

Paul

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.

Serial.println() is your friend.

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.

"Cycle" would just be another "counter" like variable that would increment with every cycle.

Sammygrind:
I wish I could post a video but we cannot exceed 2Mb on this forum

You should be able to post a link to YT video of the project.

Yes I think I will, hopefully I will get some more results that way. Thank you.

Ok I uploaded a video on yt for those that need visual reference like myself the video can be seen at:

Arduino LDR timer issues I did not include the code on yt.

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.

Hmmm ok that sounds promising, not sure I'm smart enough to figure all that out on my own... Did you watch my video on yt?

Sammygrind:
Ok I uploaded a video on yt for those that need visual reference like myself the video can be seen at:

Arduino LDR timer issues I did not include the code on yt.

Hmmm that link goes to the Pi forum. :wink:

Yeah I realised that when I tried searching for it my bad... Here is the youtube code that takes you right to it. Sorry about that lol

Time to show the code as it is now.

Use CTRL T to format the sketch.

Please use code tags. Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

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.

/* 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) {

  int photo = analogRead (pin);
  if (previousState == LOW && photo >= THRESHOLD_HIGH) {
    return HIGH;
  }
  else if (previousState == HIGH && photo < THRESHOLD_LOW) {
    return LOW;
  }
  else {
    return previousState;
  }
}
void setup() {
  Serial.begin (9600);
}
void loop() {
  static int state0, state1;
  static double time0, time1, time2;

  int  new_state = HystereticRead (A0, state0);
  if (state0 == LOW && new_state == HIGH) {

    time0 = millis ();

  }
  state0 = new_state;

  new_state = HystereticRead (A5, state1);
  if (state1 == LOW && new_state == HIGH) {
    time1 = millis ();
    time2 = (time1 - time0) / 1000;

    Serial.println ("time passed: (s)");
    Serial.println (time2);
  }
  state1 = new_state;
}

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;
}