Code needed for reed switch RPM tach

Giday Mates,

I’m trying to hack up some code to drive my arduino uno tachometer. I’m new to electronics and code writing. I’ve been lurking here for a few months but haven’t found exactly what I need.
I want to measure the rpm of a pulse motor. It has 4 south facing magnets around the drum and I’m trying to use an independent reed switch to send the signal to the arduino and have it display on the lcd.

This link is of the circuit diagram that I used. http://www.instructables.com/id/Arduino-Skateboard-Speedometer/step2/The-Electronics/
I built the circuit and when I went to get the code and the link was broken… :0

If some one could help with my sketch I would greatly appreciate it. I don’t understand about half of it, It’s a whole new language to me, but you got to start somewhere. I found another sketch which used an IR LED, I figured I could mod it for a reed switch but didn’t work. I get RPM = 29 on the lcd but it doesn’t change when the motor is spun. I thought there would be analogRead() in there somewhere but I need to list another library for that to happen I think?

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int reedswitch = A0;               // IR LED connected to digital pin 13
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;


void rpm_fun()
 {
   //Each rotation, this interrupt function is run twice, so take that into consideration for 
   //calculating RPM
   //Update count
      rpmcount++;
 }

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
   attachInterrupt(0, rpm_fun, FALLING);

   //Turn on IR LED
   pinMode(reedswitch, INPUT);
   digitalWrite(reedswitch, LOW);

   rpmcount = 0;
   rpm = 0;
   timeold = 0;
 }

void loop(){
  
   //Update RPM every second
   delay(500);
   //Don't process interrupts during calculations
   detachInterrupt(0);
   //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
   //happened once per revolution instead of twice. Other multiples could be used
   //for multi-bladed propellers or fans
   rpm = 15*1000/(millis() - timeold)*rpmcount;
   timeold = millis();
   rpmcount = 0;

   //Print out result to lcd
   lcd.clear();
   lcd.print("RPM=");
   lcd.print(rpm);

   //Restart the interrupt processing
   attachInterrupt(0, rpm_fun, FALLING);
  
}
   //Each rotation, this interrupt function is run twice, so take that into consideration for 
   //calculating RPM

With 4 magnets, that may not be true. You need to count how many interrupts actually happen during one complete revolution.

   //Turn on IR LED
   pinMode(reedswitch, INPUT);
   digitalWrite(reedswitch, LOW);

No, that is NOT what the code is doing.

How is the reed switch wired? What are you NOT using the internal pullup resistor?

   //Update RPM every second
   delay(500);

Useless crap and incorrect comment in only two lines of code. Get rid of the delay(). Look at the blink without delay example.

   detachInterrupt(0);
   //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
   //happened once per revolution instead of twice. Other multiples could be used
   //for multi-bladed propellers or fans
   rpm = 15*1000/(millis() - timeold)*rpmcount;
   timeold = millis();
   rpmcount = 0;

   //Print out result to lcd
   lcd.clear();
   lcd.print("RPM=");
   lcd.print(rpm);

   //Restart the interrupt processing
   attachInterrupt(0, rpm_fun, FALLING);

You are going to miss a boatload of interrupts while clearing the LCD and displaying data on it. The ONLY things that you should be doing in the critical section (while the interrupts are disabled) are copying rpmcount and timeold and resetting them.

I thought there would be analogRead() in there somewhere

For what? What analog device are you using? The reed switch isn’t an analog device. A switch is either on or off. There are no switches that are a little bit on or a little bit off.

but I need to list another library for that to happen I think?

Then you need to correct your thinking.

The trouble is I don't understand what's going on in that code so it's very hard for me to adjust/correct it. I started learning about electronics about 2 months ago so go easy on me.
All the comments are from the original code so some of them don't apply to the bits I've adjusted.
Would it be easier to scrap this code and start fresh or could it be made to work? The original code was for a bicycle speedo so it wasn't turning very fast.
The reed switch is wired just like the circuit diagram, 5v on one side and analog pin 0 on the other. I don't know what a pull up resistor is little own use it. I'm not looking for someone to write the code for me, just help understand it better so that I can write the code myself.

Thanks,
J

I'd probably ditch the reed, and use a digital Hall effect device instead - they don't bounce as badly as reeds do.

The trouble is I don't understand what's going on in that code so it's very hard for me to adjust/correct it.

It really isn't necessary to completely understand the code to guess what detachInterrupt() and attachInterrupt() do. Nor is it necessary to understand the code to see that lcd.clear() takes (a lot of) time, as does lcd.print(). Finally, it really isn't necessary to understand how they work in order to move them out of the detachInterrupt() to attachInterrupt() part of the code.

All the comments are from the original code so some of them don't apply to the bits I've adjusted.

Why not? Leaving misleading comments in place helps nobody.

Would it be easier to scrap this code and start fresh or could it be made to work?

That's not an either/or question. Scrapping that code would be easy. No doubt about that. Whether starting from scratch or fixing that code would be easier is not easy for me to say, not knowing much about your skills.

The reed switch is wired just like the circuit diagram, 5v on one side and analog pin 0 on the other.

But the interrupt handler is fired by something happening on digital pin 2. That would explain the constant RPM no matter how fast you spin the motor.

Why is the digital switch connected to an analog pin?

I don't know what a pull up resistor is

Google does.

little own use it.

I think this is supposed to be "let along use it". Again, google does. The Arduino has built in pullup resistors, so using them is trivial. Look at the digitalWrite() page.

I think this is supposed to be "let along use it".

I think this is supposed to be "let alone use it".

You say tomato...

You say tomato…

Damn it. Tea all over the place…

OK Paul,

This might be easy for you, you probably grew up just knowing about it. I can see however that your specialty is making others feel like retards because your pretty good at it. So if you'd like to climb down off your high horse for just a minute to offer some constructive advice that this retard can understand it would be appreciated. For me to learn about this stuff it is necessary for me to completely understand what a function does & why, other wise I'm always going to be asking retarded questions.

It's wired the way it is because I followed the circuit diagram. I was expecting the correct code at the end of it. I believe it's using an external pull down resistor from ground to A0.
Should there be code connecting pin A0 to D2?

Is this what you mean to move to attachInterrupt()

void loop(){
  

   //Don't process interrupts during calculations
   detachInterrupt(0);
   //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
   //happened once per revolution instead of twice. Other multiples could be used
   //for multi-bladed propellers or fans
   rpm = 15*1000/(millis() - timeold)*rpmcount;
   timeold = millis();
   rpmcount = 0;

   //Restart the interrupt processing
   attachInterrupt(0, rpm_fun, FALLING);
   
     //Print out result to lcd
   lcd.clear();
   lcd.print("RPM=");
   lcd.print(rpm);

What part of blink without delay should I be looking at and why? How would I incorporate that I my code?

It's wired the way it is because I followed the circuit diagram.

No, you didn't. You chose a completely different kind of sensor. That means that you throw that diagram away.

Should there be code connecting pin A0 to D2?

No. There should be code that reads the pin that the device is connected to. You are using a digital device, which should be connected to a digital pin, which is triggering an interrupt. Your hardware is not connected correctly.

For me to learn about this stuff it is necessary for me to completely understand what a function does & why, other wise I'm always going to be asking retarded questions.

Really? So, you COMPLETELY understand attachInterrupt(), Serial.print(), millis(), etc? It is NOT necessary to completely understand a function in order to successfully use the function.

Is this what you mean to move to attachInterrupt()

Yes. The less code between detachInterrupt() and attachInterrupt(), the fewer pulses will be missed.