Storing a value and entering back in the loop.

I am trying to create a odometer for my bike and I'm using a magnetic reed switch to determine the number of revolutions its going through. I am setting the value high every time it gets near the reeds. I am confused on how to keep storing the number revolutions it goes through as the loop keeps repeating. I tried a for loop but it does not work. The value i have for the distance just keeps repeating. Can anyone tell me which functions work best for this type of sketch. Thank you

Until you post your sketch we have no idea what you've done wrong...

My guess is that you haven't looked at the StateChangeDetection example
in 02.Digital.

@MarkT

#include<Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define reed A0//pin connected to read switch

int reedVal;
float radius = 13;// tire radius (in inches)
float circumference;

void setup(){

circumference = 23.14radius;
pinMode(reed, INPUT);
Serial.begin(9600);
reedVal=digitalRead(reed);
}

void loop(){

int Revolutions = reedVal;
Revolutions = Revolutions + reedVal;
float distance = Revolutions * circumference;
Serial.println(distance);
lcd.setCursor(9,3);
lcd.print(distance);

}

    reedVal=digitalRead(reed);

Read, one time, whether the pin is HIGH or LOW.

void loop(){
 
    int Revolutions = reedVal;
    Revolutions = Revolutions + reedVal;
   float distance = Revolutions * circumference;
    Serial.println(distance);
      lcd.setCursor(9,3);
    lcd.print(distance);
   
}

From then on, it doesn't matter what that pin is doing.

Hey, have I got news for you.

If you named variables meaningfully, Revolutions would be reedState, and it would be quite obvious that you needed to actually read the state of the pin.

There are some basics that your code is missing:

1: The setup function only runs once when the arduino is powered up or after a reset
2: The loop function runs, and when it is finished it immediately runs again, over and over and over....
3: DigitalRead tells you whether there is a high voltage (Vcc) or a low voltage (ground) connected to the pin in question. So it will either return a 1 (HIGH) or a 0 (LOW).
4: Variable scope: variables defined outside the function at the top, like reedval, will remember their value for as long as the arduino has power. It will therefore remember it's value even if functions (like the loop function) finishes and starts again. Variables declared inside a function is temporary for the duration of that function, e.g. Revolutions and distance in your code. Once the function finishes, these variables are effectively destroyed and you won't be able to retrieve their values anymore.
(These are very basic explanations, but once you understand them, you can search for the more complex intricacies around things like variable scope)

Let's look at what you are doing:

  1. When the arduino starts, read the value of the pin, save it in "reedval" - thus reedval is either 1 or 0 at this stage, and will never change, because you never re-assign anything to it.

your loop function does the following:
declare a new variable "revolutions" - this means that every time the loop function runs, this is a brand new variable, so it won't remember it's value from the previous time it ran. "reedval" on the other hand is declared globally, and therefore it can remember it's value. It's value is either 0 or 1, as defined by your setup. Now for some maths. Revolutions = reedval (0 or 1). Revolutions = revolutions (0 or 1) + reedval (0 or 1), thus revolutions is now equal to either 0 or 2. distance is then set to either 0 * circumference, or 2 * circumference, and this value is displayed. Then we repeat EXACTLY the same steps above with EXACTLY the same values again. My guess is that you most likely see 0 on the screen, unless your magnet was aligned with the reed switch when you power up the arduino. This is of course assuming your hardware circuit is in tact, which may also prove to be wrong, but we don't have your circuit so we have to assume it's right.

What you need to do:

  1. Declare your revolutions variable globally, so that you can remember it forever.
  2. Update the value of the reedval variable in your loop, so that you can detect when your wheel turns.
  3. detect state change on the reed value, because you only want to add 1 revolution every time the reed switch goes from 0 to 1. If it remains at 0, or if it remains at 1, you don't want to count. Otherwise if you stop with the magnet next to the switch, your arduino will just keep adding distance.

Something like:

#include<Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define reed A0//pin connected to read switch

int reedVal;
int lastreedVal;
float radius = 13;// tire radius (in inches)
float circumference;
int Revolutions;

void setup(){

  circumference = 2*3.14*radius;
    pinMode(reed, INPUT);
    Serial.begin(9600);
    Revolutions = 0;       //declared globally, and initialised when powered on.
    reedVal=digitalRead(reed); // get the initial state of the wheel
    lastreedVal=reedval; // save the initial state as the last state.
}

void loop(){
  
    reedVal=digitalRead(reed); // every time loop runs, check the wheel. next step would be to debounce it.
    if (reedval != lastreedval && reedval == HIGH) { //if reedval has changed, and has changed to HIGH
       lastreedval = reedval; //remember the current state, so we can check when it changes again
       Revolutions = Revolutions + 1;           // keep incrementing our global variable
       float distance = Revolutions * circumference; 
       Serial.println(distance);
       lcd.setCursor(9,3);
       lcd.print(distance);
    }
    
}