need help with millis function

I’m a beginner to arduino and all I can really do is play with LEDs. But I’m trying to make a bike speedometer without all the stuff the internet says I need. I’m gonna set up some wires so every time the bike wheel goes around, the circuit gets completed. I want to the arduino to sense how many times the circuit gets completed per second then multiply the amount of times by 85, that’s how many inches my wheel is around, so the arduino gets how many inches per second I’m going. Then, I will convert that to miles per hour using math. It keeps showing up with zero though and I think I’m using the millis function wrong. I don’t know for sure though. Here is my code:

const int measurePin = 2;

void setup() {
  pinMode(measurePin, INPUT);
  Serial.begin(9600);
}

void loop() {
  int count = 0;
  while (millis() <= 1000){
    if (digitalRead(measurePin) == HIGH){
      count++;
  }
  }
 count = count * 85;
 Serial.println(count);
}

while (millis() <= 1000)This will work only during the first one second after you power up your Arduino.

I recommend that you study this tutorial on using millis(): Blink without delay() explained line-by-line

0 * a number is still 0.

Are you sure pin is going high?

Also, jremington is correct, that will run once and each time after it will print 0. So 2 possible issues. The issue that jremington pointed out, needs to be fixed regardless.

  1. Pin is never going high.
  2. First time you get a value, each time after you get 0.

I_LIKE_GRAPES:
I’m a beginner

I_LIKE_GRAPES:
without all the stuff the internet says I need.

Nothing like setting yourself up for failure right off the bat.

What you need to do is not tell the code to stop for 1 second and then calculate. Let the loop function run free. It should be repeating thousands of times per second. On most of those it will do nothing. Occasionally it will see the contact close and it will increment your counter. Every second it should see that 1 second has passed since the last calculation and do the calculation again. Look at the “Blink Without Delay” example and any of the hundreds of great write-ups and tutorials on that concept. Only instead of having lines in there to turn on or off an led you’ll have lines to do your calculations.

One more note, your code uses INPUT for the pinMode and looks for a pin going HIGH when the contacts are closed. It hurts the brains of most noobs, but that’s backwards. Things should be wired to go LOW when they close. Then you can use INPUT_PULLUP instead of INPUT and avoid floating inputs.

You do know that when an input pin has nothing attached, that doesn’t mean it will read low right? It will read whatever it wants when it is floating. So you’re going to see some confusing results if you don’t attend to that as well.

OP Please examine this code.

unsigned long priorTime = 0;

const int interval = 1000;

void setup()
{
  Serial.begin(115200);

}

void loop()
{
  if (millis() - priorTime >= interval)
  {
    Serial.println("Time To Do Stuff");
    priorTime = millis();
  }
}