Hi, I have problem when I place hall effect sensor and lcd on my motorbike. When the motorbike turn on, LCD shows wrong number although the wheel not run. But when motorbike turn off, LCD shows right number. I need help, please.
Thanks
No code, no description of the hardware used, no schemata. How do you expect us to help you?
I using inductive proximity sensor NPN LJ12A3-4-2
this is my code
#include <LiquidCrystal.h>
LiquidCrystal lcd(23,25,27,29,31,33);
//Based on the codes provided by GoForSmoke
// wheel radius in mm * 100 * 2 * ( pi * 10000 ) = 94.248000 mm perimeter.
// 6 0's were used in scaling up radius and pi, 6 places are divided in the end
// and the units work out. You can use integers more accurate than float on
// Arduino at greatly faster speed. Both type of long can hold any 9-digits.
// Arduino variable type long long can hold any 19 digits is 19 place accuracy.
// if you work in Small Units and scale back later, integers are plenty accurate.
// remember, this value has to be divided by microseconds per turn.
const unsigned long wheel_perimeter = 2600UL * 2UL * 31416UL; // = 94,248,000 //radius * 2 * pi, 'UL' to force the constant into an unsigned long constant
// wheel perimeter gets divided by microseconds, 1,000,000/sec (usec or us).
// wheel turns once for 94248000 mm/100 in 1000000 usecs =
//wheel radius is 15mm
unsigned long Speed = 0;
unsigned long PrevSpeed = 0;
volatile byte hall_rising = 0; // interrupt flag
volatile unsigned long irqMicros;
unsigned long startMicros;
unsigned long differenceTimeMicros;
unsigned long displayStartMillis;
const unsigned long displayWaitMillis = 200;
unsigned long ledFlashStartMillis;
const unsigned ledFlashWaitMillis = 100;
unsigned long hallEffectCount = 0;
unsigned long distance = 0;
unsigned long mulai;
unsigned long durasi;
void wheel_IRQ()
{
irqMicros = micros();
hall_rising = 1;
hallEffectCount++;
}
void setup()
{
pinMode( 2, INPUT ); // pin # is tied to the interrupt
//Serial.begin( 9600 ); // can this be faster? faster would be better
//Serial.println(wheel_circumference); //prints the wheel perimeter
//delay( 1000 );
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Kecepatan");
lcd.setCursor(5,1);
lcd.print("Km/jam ");
//digitalPinToInterrupt(pin) instead of 0 and we define any pin we need
attachInterrupt( 0, wheel_IRQ, FALLING ); // pin 2 looks for LOW to HIGH change
}
void loop()
{
//Serial.print("hallEffectCount ");
//Serial.println(hallEffectCount);
while(hall_rising == 1){
differenceTimeMicros = irqMicros - startMicros;
startMicros = irqMicros;
hall_rising = 0;
}
distance = (wheel_perimeter * hallEffectCount)/1000000000; //distance in meters
if( differenceTimeMicros != 0 ){
Speed = wheel_perimeter / differenceTimeMicros; //speed = distance / time
Speed = (Speed*3600)/100000; // this converts the speed from mm/s to Km/h
if ( Speed != PrevSpeed )
{
//Serial.print( Speed ); // this converts the speed from mm/s to Km/h
//Serial.print( "mm/s alt=" );
//Serial.print( "KM/H " );
//Serial.println( " " );
lcd.setCursor(0,1);
lcd.print(Speed);
lcd.print(" ");
delay(50);
}
}else {
Speed = 0;
lcd.setCursor(0,1);
lcd.print(Speed);
lcd.print(" ");
//Serial.print( Speed );
//Serial.print( "mm/s alt=" );
//Serial.println( "KM/H " );
}
PrevSpeed = Speed;
}// end of void loop
I place a neodyum magnet on the wheel
attachInterrupt( 0, wheel_IRQ, FALLING ); // pin 2 looks for LOW to HIGH change
Wrong: with that code it looks for HIGH to LOW changes.
But when motorbike turn off, LCD shows right number.
What does that mean? If you turn it off it should show 0. What does it show when you turn it on?
Post a photo of how you mounted the hall effect sensor to your motorbike. It may be to near to some other magnet like the light machine.
How do you power your arduino on the bike? Did you check that the power is stable enough? Motor bikes often have a 6V battery. 6V is not enough for the Arduino power plug.