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");
}
}