Hi guys so my code had been running successfully yesterday and now today the interrupt function is not working... Can any one see a problem?
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;
unsigned long 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;
const int vssIn = 2;
//Relay
const int relay = 12;
//PWM out pin
const int pwmOut = 9;
//PID output
float brake_output;
// 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);
const int test = 11;
void setup() {
pinMode(tps, INPUT);
pinMode(ebp, INPUT);
pinMode(relay, OUTPUT);
pinMode(pwmOut, OUTPUT);
pinMode(vssIn, INPUT);
digitalWrite(vssIn, HIGH);
pinMode(test, OUTPUT);
pwmWrite(test, 200);
//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 = 356; //test value = 20 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(){
++vss_counter;
}
void loop() {
current_time1 = millis();
// LCD Display
if(current_time1 - previous_time1 > 400){
previous_time1 = current_time1;
//print brake output
lcd.setCursor(7, 0);
lcd.print(" ");
lcd.setCursor(7,0);
lcd.print(brake_output, 0);
//print exhaust pressure sensor measurement
lcd.setCursor(4, 1);
lcd.print(" ");
lcd.setCursor(4, 1);
lcd.print(((ebp_average * 0.00488 * 19) - 12), 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;
//tps counter
if(tps_average <=85){
++tps_counter;
}
else{
tps_counter = 0;
}
//vehicle speed sensor
current_time2 = millis();
if(current_time2 - previous_time2 >= 1000) {
vss_freq = vss_counter;
vss_counter = 0;
previous_time2 = current_time2;
}
//Output
if(tps_counter >= 3000 && vss_counter >= 44){
digitalWrite(relay, HIGH);
Input = ebp_average;
myPID.Compute();
pwmWrite(pwmOut, pidOutput);
brake_output = (((pidOutput / 255) * 100) * 2);
}
else{
digitalWrite(relay, LOW);
pwmWrite(pwmOut, 0);
brake_output = 0;
}
}