Pages: [1]   Go Down
Author Topic: Newbie regarding speedometer sensor(reed switch) and arduino coding  (Read 568 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 my coding might kno .

#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) {
    // detected!  blink.
    //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;
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the circuit digram which are related to this project.


* circuit diagram.png (70.24 KB, 903x461 - viewed 16 times.)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Since the purpose of the sensor is to measure speed, and since there are no digitalRead statements, one must presume that the sensor is what drives the interrupt.

Since the interrupt 1 event occurs on pin 3, one side of the sensor connects to pin 3. The pin detects HIGH or LOW signals, so the sensor needs to output either 0V or +5V. The only way it can output +5V is if it is supplied with +5V. Since there is only one other wire, it is obvious that that wire must go to +5V.
Logged

Pages: [1]   Go Up
Jump to: