Go Down

Topic: Simple While Loop Problem (Read 4810 times) previous topic - next topic

phinsil6

UPDATE: I removed the delayMicroseconds().  Found a proper value to divide "time" by and now I get pretty accurate times.  For the final test, I will connect pin 9 on the uno to another OUTPUT pin on a Mega and write the pin LOW from the MEGA for varying small and large amounts of time to get an even more accurate value.  Below is my code that seems to work well (at least for now).  Thanks everyone for all your help for the hardware and software.
Code: [Select]
int speed1 = 9;
int tripped;
float time;
float Speed;

void setup()
{
  pinMode(speed1, INPUT);
  Serial.begin(9600);
  Serial.println("Ready to Test Speed");
  digitalWrite(speed1, HIGH);
}

void loop()
{
  tripped = digitalRead(speed1);
  if (tripped ==0)
  {
    time = 0;
    Serial.println();
    Serial.println("Reading...");
    while (tripped == 0)
    {
      time = time++;
      tripped = digitalRead(speed1);
    }
    time = (time)/70070;
    Serial.println(time);
//    Speed = (5.0/12.0) / time;  (5 in/12 in) / duration = X ft/s
    Serial.println(Speed);
  }
}

Go Up