Counter for measuring distances (newbee)

Hello.
I have now owned my arduino uno for some months but i just cant
learn how to program it and do what i buy it for.
So maybe someone may be kind and help me with this?

I want to do a counter whic i will use for measuring distances.
I also want to save the measured data so i can copy it in to
excel.

i will use magnetic relays (reed) and magnet on a wheel
and a pushbutton which will confirm the measureddata
and reset it for measuring a new distance. I want it to save
all the measured series for later use in ex excel.

so i think if i can make the arduino count the number of
"clicks" in the magnetic reedrelay. And when i measured the
distance save the data and reset for new measure and so
on when i push the button.

Hope someone can help me with this?
Best regards" Daniel

What you describe sounds reasonable. What have you tried so far? Can you post your code and explain what is not working as you would like?

...R

sorry. But i have not coded anything. i just cant figure out
how i can do it. I just dont get it. i have try to read tutorials
and everything but maybe its just not my thing.

Maybe someone kind can help me get started with the
coding of the projekt?

Best regards Daniel

Uppfinnarwannabee:
sorry. But i have not coded anything. i just cant figure out
how i can do it. I just dont get it. i have try to read tutorials
and everything but maybe its just not my thing.

Maybe someone kind can help me get started with the
coding of the projekt?

Sure. Start with an example program. I suggest the ones that came with the Arduino IDE. Start with the Digital-button sketch. A button is a switch is a reed relay... no difference. Wire a pushbutton switch according to the instructions in the button sketch, then upload the sketch. Use it, read the program, understand the program. Then, try another sketch. Figure out how to count things, figure out how to display things. There's no better way to get started than by reading, understanding, and changing programs to suit your purposes.

We are here when you run into things you don't understand.

thanks alot!
i found this:

and run it and it counts the number of push on a button.
the problem now is i dont know how to modify the code
to only print the final result after a number of pushes and then
reset for a new serie measure (after pushing another button)

it now prints out on serial monitor ater ex 8 push 1 2 3 4 5 6 7 8
i want it only to print 8.

best regards Daniel

Uppfinnarwannabee:
the problem now is i dont know how to modify the code
to only print the final result after a number of pushes and then
reset for a new serie measure (after pushing another button)

it now prints out on serial monitor ater ex 8 push 1 2 3 4 5 6 7 8
i want it only to print 8.

Well, how do you want to define when you have pushed the button enough times. You are counting and reporting every time you press the button. If you only want to report the number of pushes after you are done pushing, you have to only count, and not report. Then when you are finished, you report the count. Clear?

OK, so how are you going to determine that you have finished pushing the button?

Since you are really going to be counting reed switch closures, and since you are going to have an actual pushbutton, perhaps you could report the count when you press the actual pushbutton. Sound about right? For testing, you need two buttons. One you call "reed switch", and the other you call "pushbutton".

Don't forget to reset the count to zero after you report it.

yes you are right. i just dont know
how to write the code for that.
and should i connect the push-button to same pin as
reed?

i tried to push the reset button on the uno after every measure serie
and thats work. but still im very far from what i want to do.

program now prints out on serial ex:

1234512345678912312345 etc

i think it can be ok also if it only coud do this instead:

12345
123456789
123
12345

i guess i can use a button to reset and make it print out a new line with the next series
but how?

now my code looks like this, am i doing it correct?

const int  reedPin = 2;    // the pin that the reed-relay is attached to
const int buttonPin = 3;   // the pin that the push-button is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables 
int reedCounter = 0;   // counter for the number of reed switches
int reedState = 0;         // current state of the reed-relay
int lastReedState = 0;     // previous state of the reed-relay
int pushButtonState;
void setup() {
  // initialize the reed pin as a input:
  pinMode(reedPin, INPUT);
  // initialize the button pin as a input
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  pushButtonState = digitalRead(buttonPin);
  if (pushButtonState == LOW)
Serial.print(reedCounter);
  
  
  // read the reed input pin:
  reedState = digitalRead(reedPin);
  // compare the reedState to its previous state
  if (reedState != lastReedState) {
    // if the state has changed, increment the counter
    if (reedState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      
        reedCounter++;
      
    }
    
  }
  // save the current state as the last state,
  //for next time through the loop
  lastReedState = reedState;

 
  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (reedCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
 
}

Hi again. i have now modified the code so it does this.
when i push the reed it does not print out anything on serial.
so if i push it ex 15 times nothing happends.
but if i then push and hold down the "pushbutton" and then press
the reed it prints out 16 on the serial. so im closer now but not there
yet. its kind of fun. but im not sure what im doing. any help would be nice.

this is the code now:

const int  reedPin = 2;    // the pin that the reed-relay is attached to
const int buttonPin = 4;   // the pin that the push-button is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables 
int reedCounter = 0;   // counter for the number of reed switches
int reedState = 0;         // current state of the reed-relay
int lastReedState = 0;     // previous state of the reed-relay
int pushButtonState;
void setup() {
  // initialize the reed pin as a input:
  pinMode(reedPin, INPUT);
  // initialize the button pin as a input
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  
  
  // read the reed input pin:
  reedState = digitalRead(reedPin);
  // compare the reedState to its previous state
  if (reedState != lastReedState) {
    // if the state has changed, increment the counter
    if (reedState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      
        reedCounter++;
      pushButtonState = digitalRead(buttonPin);
  if (pushButtonState == HIGH)

Serial.print(reedCounter); 
    }
    
  }
  // save the current state as the last state,
  //for next time through the loop
  lastReedState = reedState;

 
  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (reedCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
 
}

Use Serial.println() to start a new line after printing.

Serial.print(reedCounter);  // this prints the number without starting a new line

// If you want each number on its own line, use this:
Serial.println(reedCounter); // this prints each number on its own line

// This is another way of doing the same thing:
Serial.print(reedCounter); // this prints the number
Serial.println(); // this starts a new line

For what you're doing, I think you're going to want some kind of display that can actually show numbers, so that you can read the distance while you're using the wheel.

By the way, what size is the wheel?

im not sure how big it is but maybe 1 meter around it.

yes now the program does this:

i push the "reed" ex 10 times
then push and hold "pushbutton" and then press "reed"
it now prints 16 at serial monitor.
then i push "resetbutton" on uno-board
and press "reed" 10 times etc.
it does print on serialboard like this:

16
11
etc. which is what i want.
but is there a way i can make it print out 15 after i push reed 15 times
and then push button, so i dont have to hold it down and get the extra (16)
from pushing reed?

and how can i reset it without have to push the button on unoboard?
(i think i maybe can manage using a extra reed for resetting)
but how do i code for reseting?

thanks for help! best regrads Daniel

Uppfinnarwannabee:
im not sure how big it is but maybe 1 meter around it.

If you are going to use the wheel for measuring, you will need to know the size of the wheel.

but is there a way i can make it print out 15 after i push reed 15 times
and then push button, so i dont have to hold it down and get the extra (16)
from pushing reed?

You now have this in your code:

      pushButtonState = digitalRead(buttonPin);
  if (pushButtonState == HIGH)

Serial.print(reedCounter);

The problem is where you have it in your code.

  reedState = digitalRead(reedPin);
  // compare the reedState to its previous state
  if (reedState != lastReedState) {
    // if the state has changed, increment the counter
    if (reedState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:

      // Anything you have in here will only get done if the reed switch
      // went from "off" to "on".
      // Since you want to be able to output the value of reedCounter
      // without the reed switch being "on", you have to pull the relevant
      // code out from here and put it elsewhere.

    }
    
  }

and how can i reset it without have to push the button on unoboard?
(i think i maybe can manage using a extra reed for resetting)
but how do i code for reseting?

By "reset the counter", you probably mean "set the counter to zero". This is how you do that:

reedCounter = 0;

What you doing more like the arduino bicycle computer
http://lmgtfy.com/?q=arduino+bicycle+computer

Thanks for all the help. i will reed i trough and see if i can figure it
out how to do that.

The bicycle computer is a lot more complicated than what Uppfinnarwannabee is asking for. It might also be difficult for him to adapt to his needs.

Also, just by looking at it, I found one or two bugs in the bicycle computer code. (Example: decimal comma used in place of decimal point. Sorry, but when you're writing C++ or Arduino code, you have to be American and use a dot for the decimal point.)

I think what Uppfinnarwannabee wants is just a simple measurement of distance, not speed or anything else. (If I am mistaken, please correct me.)

Like I said, for distance, you need to know the size of the wheel. Specifically, you need to know how much distance corresponds to 1 turn of the wheel.

Once you find out the correct distance, you can try this code.
(Note: You will need to put in the correct number for wheelCircumference.)
(Note #2: I have edited this code to fix a typo, and again to fix another problem.)

const int  reedPin = 2;    // the pin that the reed-relay is attached to
const int buttonPin = 3;   // the pin that the push-button is attached to
const int ledPin = 13;       // the pin that the LED is attached to
const long wheelCircumference = 1000; // wheel circumference in millimeters

// Variables 
long odometer = 0;   // the odometer (it counts in millimeters)
int reedState = 0;         // current state of the reed-relay
int lastReedState = 0;     // previous state of the reed-relay
int pushButtonState = 1;
int lastPushButtonState = 1;

void setup() {
  // initialize the reed pin as a input:
  pinMode(reedPin, INPUT);
  // initialize the button pin as a input
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  lastPushButtonState = pushButtonState; 
  pushButtonState = digitalRead(buttonPin);
  if (pushButtonState != lastPushButtonState) {
    if (pushButtonState == LOW) {
      // output the odometer reading in meters
      long adjOdometer = odometer + 50; // round to 1 decimal place
      Serial.print(adjOdometer / 1000); // whole meters
      Serial.print(","); // decimal comma (you prefer decimal commas, right?)
      Serial.print((adjOdometer % 1000) / 100); // tenths of a meter
      Serial.println(); // start a new line
      
      // reset odometer to zero
      odometer = 0;
    }
    // debounce
    delay(10);
  }
  
  
  // read the reed input pin:
  reedState = digitalRead(reedPin);
  // compare the reedState to its previous state
  if (reedState != lastReedState) {
    // if the state has changed, maybe add some distance to the odometer
    if (reedState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      
        // increase odometer reading
        odometer += wheelCircumference;
    }
    // debounce
    delay(10);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastReedState = reedState;

 
  // turns on the LED every ten meters by
  // checking the modulo of the odometer reading.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if ((odometer > 0) && ((odometer % 10000) < wheelCircumference)) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
 
}

Uppfinnarwannabee:
but is there a way i can make it print out 15 after i push reed 15 times
and then push button, so i dont have to hold it down and get the extra (16)
from pushing reed?

and how can i reset it without have to push the button on unoboard?
(i think i maybe can manage using a extra reed for resetting)

you have two switches, reed and button, and a variable, count.

In pseudocode...

int count;

void setup() {
   set up pin modes, serial.begin, etc.
}

void   loop() {
   if  reed {
      count += 1
   }
   if button {
      print count
      count = 0
  }
}

Thanks again. So i dont need this? anymoore?:

int reedState = 0;
int lastReedState = 0;

if (reedState != lastReedState) {
if (reedState == HIGH) {

where they only use in the 12345.. etc as it printed it out on serialscreen before?
or what is their function really?
best regards Daniel

odometer. thanks for that code. but it doesnt work well.
it prints out "random" numbers directly when i push the reed
like this ex:

0,1
0,1
1,2
etc...

best regards Daniel

Uppfinnarwannabee:
odometer. thanks for that code. but it doesnt work well.
it prints out "random" numbers directly when i push the reed
like this ex:

0,1
0,1
1,2
etc...

best regards Daniel

Sorry, I noticed a typo in my code, which I fixed. (I had written lastpushButtonState instead of lastPushButtonState.)

I am inserting something called "debouncing". This is because sometimes when you press a switch once, it will "bounce" and will register more than once. I am using some small delays to take care of this.