Go Down

Topic: Newbie regarding speedometer sensor(reed switch) and arduino coding (Read 2224 times)previous topic - next topic

JasonLoh

Currently i am troubleshooting a project regarding using electro-luminscent wire to display out the speed together with the speedometer sensor(reed switch). It's done by previous patch of students who has already completed, thats what my teacher told me. when the teacher pass it to me, the sensor wire have loosen. There are 2 wires for the sensor.

The question is i don't know where should i connect the sensor.

The arduino board which i am using was arduino duemilanove AT328.

Below are the coding..
#define onesPins
#define tensPins

int oldOnes = -1;
int oldTens = -1;

int x;

int sleepms = 5;
volatile int sensorState = LOW;  // toggled by the interrupt routine.

unsigned long thisTime, lastTime;
float wci = (27.0 * PI);  // "wci" is wheel circumfrence (inches) = pi * diameter
int spinTime;
double gSpeed;
int foo = (1000.0 * 3600.0) / 63360.0; // miliseconds per hour divided by inches per mile.

unsigned long fartTime = 0;
int fartState = 0;

void setup()
{
int p;
for (p = 1; p <= 9 ; p += 2)  {
pinMode(onesPins(p), OUTPUT);
}
for (p = 0; p < 7; p++) {
pinMode(tensPins(p), OUTPUT);
}

// wait for interrupts on pin 3.
attachInterrupt(1, spoke_int, RISING);

thisTime = millis();
gSpeed = 0;

//Serial.begin(19200);
good_morning();
sensorState = LOW;
}

void loop()
{
byte speedmph;

speedmph = checkSpeed();

if (speedmph > 0)  {
displaySpeedInLights(speedmph);

} else {
fartAround();
}
delay(sleepms);
}

/* this takes an integer between 0 and 79, and
picks which pins to turn on.  it sets the result
in the global array newPinState.
*/

void displaySpeedInLights(int myspeed)  {

//Serial.print("speed = ");
//Serial.print(myspeed);
//Serial.print("\n");

// special case for zero speed: all lights off.
if (myspeed == 0)  {
all_off();
return;
}

// integer math working for us, for once.
// we will have to double-check that the rounding goes the right way.

// we only disply the even speeds, so decrement by 1 if even.
myspeed = (((myspeed + 1) / 2 ) * 2) - 1;

int tens = myspeed / 10;
int ones = myspeed - (tens * 10);

if (oldOnes != ones)  {
if (oldOnes >= 0) {
digitalWrite(onesPins(oldOnes), LOW);
}
digitalWrite(onesPins(ones), HIGH);
oldOnes = ones;
}

if (oldTens != tens)  {
if (oldTens >= 0) {
digitalWrite(tensPins(oldTens), LOW);
}
digitalWrite(tensPins(tens), HIGH);
oldTens = tens;
}
}

int checkSpeed() {
double mySpeed;
// was the sensor pinged since we last checked?
if (sensorState == HIGH) {
//ledState = !ledState;
//digitalWrite(ledPin, ledState);

lastTime = thisTime;
thisTime = millis();
// TODO: deal with wraparound of millis() .
spinTime = thisTime - lastTime;

/*
* wci is thousandths of an inch,
* spinTime is thousandths of a second.
* so wci/spintime is inches per second.
* we want miles per hour.
*
* in case you were wondering:
* 1 mile = 63 360 inches
* 1 hour = 3 600  seconds
*
*/

// multiplication is communitive, right?
mySpeed = wci / spinTime ;
//speed = ((wci / spinTime) * 3600.0) / 63360.0;

// Serial.print("Speed = ");
//Serial.print(speed * 1000.0, DEC);
//Serial.println(" milimiles/hour");
//Serial.print(" inches/seconds = ");
//Serial.print(speed * 1000.0 * 3600.0, DEC);
//Serial.print(" inches/hour = ");
//Serial.print((speed * 1000.0 * 3600.0) / 63360.0, DEC);
//Serial.print(" miles/hour = ");
//Serial.print(speed * foo, DEC);
//Serial.print(" miles/hour\n");

sensorState = LOW;
gSpeed = mySpeed;
} else if ((millis() - lastTime) > 7000){
gSpeed = mySpeed = 0;
} else {
mySpeed = gSpeed;
}
//return (gSpeed * foo);
// kph version: 1.61 kilometers per mile
return (gSpeed * foo * 1.61);
}

// display some wacky attention-grabbing thing when we're not moving
void fartAround() {

//Serial.print("Farting around\n");
if (millis() - fartTime > 1000)  {
// change state!
fartState = !fartState;
fartTime = millis();
}

if (fartState == 1) {
// light up 23 for now.
displaySpeedInLights(23);
//good_morning();
} else {
all_off;
}
}

// the interrupt routine
void spoke_int() {
sensorState = HIGH;
}

// a little wake-up test.
void good_morning() {
byte p;
//Serial.print("Good morning!\n");
for (p = 1; p <= 9 ; p += 2)  {
digitalWrite(onesPins(p), HIGH);
delay(100);
digitalWrite(onesPins(p), LOW);
}

for (p = 0; p < 7; p++) {
digitalWrite(tensPins(p), HIGH);
delay(100);
digitalWrite(tensPins(p), LOW);
}

}

void all_off() {
digitalWrite(0,LOW);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
oldOnes = -1;
oldTens = -1;
}

johnwasser

The Arduino reference page for attachInterrupt() says:

"Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18)."

Since your code is looking for a rising edge on interrupt 1 you want to connect Pin 3 to ground with a resistor and use the closing of the reed switch to connect Pin 3 to +5.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

masavee

why reed switch,slowly use,no good,hall sensor cheap and good,i use my old car vw passat variant, car odometer back have place add hall sensor but no have digital speed meter no can use,old model, but i add sensor chip same place and take data out my arduino.
odometer inside have many volswagen disck were can take out hall sensor data.
i looking many speed/odometer code but many no good ,i need only speed,odometer,trip meter,and i think add fuel tank potentiometer data too and calculate how much has fuel /km and how much can drive fuel/km.same than all volvo car have.
but no have found anythink good code,im newbie and no understand good all codes what i found at net.
and many have too lot all other,gps,tracker,temperature,other.sd card logger or other what no need old car.
and i looking many make gps odometer,no good no work good and real anytime and true.
all peoples must forget only gps measure odometer,no newer can working good,i know thats good,i use my car last 4 year navigator and no newer have realtime.and right place and true speed.
best have hall or car own speed sensor and gps,and all other together.
gps update only sometime/week./day.reference place mean.
but have vthere somebody good gode simply speed/odometer whit hall sensor?
no found net anythink good what understand and can use
hall sensor have digitall.(better use than analog)guikly .

Go Up

Please enter a valid email to subscribe