HELP! Need help with coding!

Hi everyone!

My team and I really need help with some coding. We have a project that requires the use of an optical sensor (Speed Detection 4 Pin 1CH Velocity Measurement Sensor Module 3.3-5V), we have a dc motor which we will attach a plastic circle with gaps and rotate it through the sensor, whereby, the LCD will not display.

So basically, do you have a code that I can try out?
This is what we have so far.

#include <LiquidCrystal.h> // for LCD to be included
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //pins connected to the arduino

int encoder_pin = 8;
unsigned int rpm;
volatile byte pulses;
unsigned long timeold;
unsigned int pulsesperturn = 1;

void counter ()
{
pulses++;
}

void setup() {
lcd.begin(16,2);
pinMode (encoder_pin, INPUT);
attachInterrupt (0, counter, FALLING);
pulses = 0;
rpm = 0;
timeold = 0;
}

void loop() {

if (millis() - timeold >= 1000){
detachInterrupt(0);
rpm = (60 * 1000 / pulsesperturn )/ (millis() - timeold)* pulses;
timeold = millis();
pulses = 0;
lcd.setCursor(0,1);
lcd.print("RPM: ");
lcd.print(rpm);
lcd.print(rpm,DEC);
}

}
int vallen;
int displayWidth = 128;
int letterWidth = 18;
void updateDisplay(int rpm){
if(rpm >999)
vallen = 4;
else if(rpm > 9)
vallen = 2;
else
vallen = 1;

}

THANKS!
THB.

Try printing a message to the LCD from setup()

    detachInterrupt(0);Did you mean to leave the interrupt unattached ?

void updateDisplay(int rpm)What is the purpose of this function ?

I see this a lot and have a question for those who know interrupts better than I do. Do the lines in the OP's code:

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //pins connected to the arduino
//...
attachInterrupt (0, counter, FALLING);

cause a problem? If interrupt vector 0 is associated with pin 2, do the statements above cause a conflict of some sort? Or, does the attachInterrupt() and resultant call to counter() override the lcd() momentarily, or is it simply not a problem?

do the statements above cause a conflict of some sort?

Of course they do.

Or, does the attachInterrupt() and resultant call to counter() override the lcd() momentarily

If the LiquidCrystal class declares that pin 2 is an OUTPUT, and changes the state of the pin now and then, the interrupt handler will be called every time the pin goes LOW. That is going to wreak havoc with the other (intended) use of the pin.

What everyone else said. Interrupt 0 is not the same as pin 0. It is usually pin 2, which means that you can't put your LCD on pin 2.

Thanks Paul(s). It confirms what I thought. Given that, I wonder why so many books and tutorials have the following object instantiation:

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

This is the code the OP used, but in his defense, he took it from here which is the Arduino tutorial. I understand what pin 13 does, but why start the pin countdown with 12, then 11, then jump to 5? What makes 10-6 off limits? I try to avoid using 2 and 3 in these situations...just in case. I'm sure the people who wrote that Arduino tutorial have a good reason for this. I just don't know what it is.

why start the pin countdown with 12, then 11, then jump to 5?

I can't think of a really good reason (or any reason). Certainly you shouldn't connect pin 2 to the LCD and also use it for some other purpose.

I'm sure the people who wrote that Arduino tutorial have a good reason for this.

Well, I am NOT sure. The only reason that I can see for that selection of pins is that it avoids the ones most motor shields use.