Pages: 1 2 3 [4]   Go Down
Author Topic: Simple While Loop Problem  (Read 4447 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
  }
}
Logged

Pages: 1 2 3 [4]   Go Up
Jump to: