Programming

For a portion of a loop in our program, we are trying to calculate average time based on 5 different recordings from a timer.

We are using a button to tell the program at what point to record each time.

Our condition is when the button is pressed. The action to perform when this condition is true is to record the time as time1 the first time the button is pressed, time2 the second time the button is pressed and so on to time5 the fifth time the button is pressed. And at the end, to use the sum of time1, time2, time3, time4, and time5 and divide it by 5 to the find the average time. The issue is that we need the program to wait for each condition to occur and only proceed if it is true. What token would require the program to wait for the condition instead of running the loop again and again without any change.

This is the program we wrote:

void setup () {
Serial.begin(9600);
pinMode (7, INPUT);}
long timer;
long time1;
long time2;
long time3;
long time4;
long time5;
long timeA;
long avrgtime;

void loop () {
timer = micros();
while (digitalRead(7)==HIGH) {
long time1 = timer;}
while (digitalRead(7)==HIGH) {
long time2 = timer - time1; }
while (digitalRead(7)== HIGH) {
long time3 = timer - time2 - time1;}
while (digitalRead(7)==HIGH) {
long time4 = timer - time3 - time2 - time1;}
while (digitalRead(7)==HIGH) {
long time5 = timer - time4 - time3 - time2 - time1;}

long timeA = time1 + time2 + time3 + time4 + time5;
long avrgtime = timeA/5;
Serial.println (avrgtime);
delay (100);}

Any ideas?

You've got bad scope issues there, I think.
You need to pass code through the auto format tool to make things clearer

Your code reformatted and put into code tags:

void setup () {
  Serial.begin(9600);
  pinMode (7, INPUT);
}
long timer;
long time1;
long time2;
long time3;
long time4;
long time5;
long timeA;
long avrgtime;

void loop () {
  timer = micros();
  while (digitalRead(7)==HIGH) {
    long time1 = timer;
  }
  while (digitalRead(7)==HIGH) {
    long time2 = timer - time1;  
  }
  while (digitalRead(7)== HIGH) {
    long time3 = timer - time2 - time1;
  }
  while (digitalRead(7)==HIGH) {
    long time4 = timer - time3 - time2 - time1;
  }
  while (digitalRead(7)==HIGH) {
    long time5 = timer - time4 - time3 - time2 - time1;
  }

  long timeA = time1 + time2 + time3 + time4 + time5;
  long avrgtime = timeA/5;
  Serial.println (avrgtime);
  delay (100);
}

What AWOL means is that this variable:

long time1;

... is not the same as this variable:

    long time1 = timer;

Plus you may want to read up on arrays. What if you were asked to do 100 timings? You can see this would get tedious pretty quickly.

Computers are fast. I would expect that code to

  • wait for the pin to go low ( the first loop)
    read and store
  • second loop enters, reads the pin. Its still low.
    read and store
  • third loop enters, reads the pin. Its still low.
    and so on.

You need to have logic to

  • wait for the pin to go low
  • then wait for the pin to go high
    in each of the 5 cases

e.g.

while (pin ==high) {
  timer = value;
}
while (pin == low) {
;  // just wait and do nothing
}
// and on to the next one

You also need a button that will not "bounce" - if it is a pushbutton, connect a 0.1microfarad capacitor across it and feed it via a 1K resistor to the +5V line. An ordinary button can open and close 20 to 30 times as you push it, and the computer can see each of those transitions. Other sorts of sensor might not do that, but you just said button.

Additionally, I suggest that you remove the "long" from all the variables inside the loop (every time you manipulate the variable, you don't need to include its type [e.g. long] before it)! You are constantly declaring variables! You can try for loops when you constantly subtract, use arrays, and use pulseIn (careful with pulseIn's timeout).

@dkl65: That's what they were talking about above when they were discussing "scope".

Besides, the return type of "micros()" is unsigned long.