Reading vehicle speed sensor with interrupts not working

Hey guys i am trying to read the vehicle speed sensor in my truck ( 14V 8000 pulses per mile). I put the signal through a resistor divider ( 2.2k and 1k) and read 3.23V with truck stationary and around 1.62V with the truck moving. The problem I am having is that the interrupt function in my code doesnt seem to be working. Can anyone see where the problem is?

Thanks!

#include <PID_v1.h>
#include <PWM.h>
#include <SPI.h>
#include <LiquidCrystal.h>

int32_t frequency = 167; //frequency (in Hz)

const int numReadings= 3;

//exhaust pressure sensor
int ebp_readings[numReadings];      
int ebp_index = 0;                  
float ebp_total = 0;                  
float ebp_average = 0; 
const int ebp = A0;

//throttle position sensor
int tps_readings[numReadings];      
int tps_index = 0;                  
float tps_total = 0;                  
float tps_average = 0; 
const int tps = A1;
int tps_counter = 0;

//vehicle speed sensor
unsigned long current_time2;
unsigned long previous_time2 = 0;
volatile int vss_counter = 0;
unsigned long vss_freq = 0;


//Relay
int relay = 12;

//PWM pin
int pwmOut = 9;

// LCD timer
unsigned long current_time1;
unsigned long previous_time1 = 0;


//pid information
double Setpoint, Input, pidOutput;
PID myPID(&Input, &pidOutput, &Setpoint,1,0,0, DIRECT);

// initialize lcd
LiquidCrystal lcd (8, 7, 6, 5, 4, 3);

void setup() {
  pinMode(relay, OUTPUT);
  pinMode(pwmOut, OUTPUT);
   
  //vehicle speed sensor
  attachInterrupt(0, vspeed, FALLING);
  
  //exhaust pressure sensor
  for (int ebp_thisReading = 0; ebp_thisReading < numReadings; ebp_thisReading++)
    ebp_readings[ebp_thisReading] = 0;

    //throttle position sensor
  for (int tps_thisReading = 0; tps_thisReading < numReadings; tps_thisReading++)
    tps_readings[tps_thisReading] = 0;  
 

  //initialize all timers except for 0, to save time keeping functions
  InitTimersSafe(); 
  //sets the frequency for pwmOut pin 9
  bool success = SetPinFrequencySafe(pwmOut, frequency);  //change to pwmOut after testing
  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(pwmOut, OUTPUT);
  }

  //initialize PID variables
  Input = ebp_average;
  Setpoint = 253;  //test value = 10 psig
  myPID.SetOutputLimits(38, 115);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);    
  
  // lcd setup
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("BRAKE%:");
  lcd.setCursor(0,1);
  lcd.print("EBP:");

}

void vspeed(){
  detachInterrupt(0);
  ++vss_counter;
}

void loop() {
  
   current_time1 = millis();
  // LCD Display
  if(current_time1 - previous_time1 > 400){
    previous_time1 = current_time1;
   
  //print exhaust pressure sensor measurement
  lcd.setCursor(4, 1);
  lcd.print("   ");
  lcd.setCursor(4, 1);
  lcd.print(((ebp_average * 0.00488 * 19) - 13), 0);
  
  lcd.setCursor(12,0);
  lcd.print("     ");
  lcd.setCursor(12,0);
  lcd.print(vss_freq);
  
  lcd.setCursor(12,1);
  lcd.print("         ");
  lcd.setCursor(12,1);
  lcd.print(tps_counter);
  }

  //exhaust pressure sensor
  ebp_total= ebp_total - ebp_readings[ebp_index];         
  ebp_readings[ebp_index] = analogRead(ebp); 
  ebp_total= ebp_total + ebp_readings[ebp_index];         
  ebp_index = ebp_index + 1;                    
  if (ebp_index >= numReadings)              
    ebp_index = 0;                           
    
  ebp_average = ebp_total / numReadings;    
  
  //throttle position sensor
  tps_total= tps_total - tps_readings[tps_index];         
  tps_readings[tps_index] = analogRead(tps); 
  tps_total= tps_total + tps_readings[tps_index];         
  tps_index = tps_index + 1;                    
  if (tps_index >= numReadings)              
    tps_index = 0;                           
    
  tps_average = tps_total / numReadings; 
  
  //vehicle speed sensor
  current_time2 = millis();
    
    if(current_time2 - previous_time2 >= 1000) {
    vss_freq = vss_counter;
    vss_counter = 0;
    previous_time2 = current_time2;
    }
  
  //tps counter
  if(tps_average <=81){
    ++tps_counter;
  }
  else{
    tps_counter = 0;
  }

  //Output
  if(tps_counter >= 15){
    digitalWrite(relay, HIGH);
    Input = ebp_average;
    myPID.Compute();
    pwmWrite(pwmOut, pidOutput); 
    //print pidoutput
    lcd.setCursor(7, 0);
    lcd.print("   ");
    lcd.setCursor(7,0);
    lcd.print(((pidOutput / 255) * 100) * 2, 0); 
  }
    else{
    pwmWrite(pwmOut, 0);
    digitalWrite(relay, LOW);
    lcd.setCursor(7,0);
    lcd.print("   ");
    lcd.setCursor(7,0);
    lcd.print("OFF");
  }
   
}
void vspeed(){
  detachInterrupt(0);
  ++vss_counter;
}

That looks suspicious to me. Why are you detaching the interrupt? Would seem to count to 1 then never increment again?
Plus I think the vspeed() function must be placed/declared outside the setup() and loop() functions?

Lefty

Thanks for looking it over lefty. I removed the detached interrupt and still no luck. I made a code that just does the interrupt and it works as it should. hmm... i dont understand..

#include <SPI.h>
#include <PWM.h>
#include <LiquidCrystal.h>
int32_t frequency = 167; //frequency (in Hz)
const int pwmOut = 9;

//vehicle speed sensor
unsigned long current_time2;
unsigned long previous_time2 = 0;
volatile int vss_counter = 0;
unsigned long vss_freq = 0;
const int vssIn = 2;

// initialize lcd
LiquidCrystal lcd (8, 7, 6, 5, 4, 3);

void setup(){

  pinMode(vssIn, INPUT);
  digitalWrite(vssIn, HIGH);
  attachInterrupt(0, vspeed, FALLING);
  
  lcd.begin(16, 2);
    //initialize all timers except for 0, to save time keeping functions
  InitTimersSafe(); 
  //sets the frequency for pwmOut pin 9
  bool success = SetPinFrequencySafe(pwmOut, frequency);  //change to pwmOut after testing
  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(pwmOut, OUTPUT);
    pwmWrite(pwmOut, 200);
  }
  
}

void vspeed(){
  ++vss_counter;
}

void loop(){
  
  //vehicle speed sensor
  current_time2 = millis();
    
    if(current_time2 - previous_time2 >= 1000) {
    vss_freq = vss_counter;
    vss_counter = 0;
    previous_time2 = current_time2;
    }
  
  lcd.setCursor(12,0);
  lcd.print("     ");
  lcd.setCursor(12,0);
  lcd.print(vss_freq);
}

the interrupt function in my code doesnt seem to be working

What isn't working ?

I got it working!!!! Thanks guys!

Hey Mitch. You said you got your interrupt to work. Does it fire at both 3.23V and at 1.62V? Why are you getting a reading when the truck isn't moving?

I'm working on a project of my own using the vss....which I believe is an ABS sensor in my 2006 Toyota Sienna. It is a purple wire in a cluster way up behind the steering column. It gives me a reading of 2.35V on my multimeter. I'm assuming it is an analog sine wave, but I don't have an o-scope. I was thinking I'd have to step up the voltage to 5v for the interrupt to fire every time the vss/abs ticks.

I've read about it online, but I really don't know much about the vss/abs. Not even sure if I'm talking about it correctly. Anyone have experience with this? Mitch, how about you?