Hi guys i need some help with changing timer 1 and timer 2 frequencies. I am already using the pwm library but i think that it changed both timers to 167Hz.
I need pin 9 at 167Hz and now i need another pin at a different frequency so that i can output an analog voltage through an rc filter.
Here is the code
Thanks for your help!
#include <PID_v1.h>
#include <PWM.h>
#include <SPI.h>
#include <LiquidCrystal.h>
int32_t frequency = 167; //frequency (in Hz)
//exhaust pressure sensor
float ebp_reading = 0;
float ebp_oldaverage = 0;
float ebp_newaverage = 0;
const int ebp = A0;
//throttle position sensor
float tps_reading = 0;
float tps_oldaverage = 0;
float tps_newaverage = 0;
const int tps = A5;
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;
//duty cycle in measurement
const int pwmIn = 11;
float totaltime = 6000.0;
float dutycycleIn;
float dutycycleOut;
float dutycycleOutD;
float dutycycle_reading = 0;
unsigned long durationL;
float durationL_oldaverage = 0;
float durationL_newaverage = 0;
//PWM out pin
const int pwmOut = 9;
//exhaust brake status, 0=OFF, 1=ON
int brake_status = 0;
// exhaust brake on/off switch
const int eb_switch = 12;
int eb_switch_value = 0;
//pid information
double Setpoint, Input, pidOutput;
PID myPID(&Input, &pidOutput, &Setpoint,1,1,0, DIRECT);
// initialize lcd
LiquidCrystal lcd (8, 7, 6, 5, 4, 3);
// LCD timer
unsigned long current_time1;
unsigned long previous_time1 = 0;
unsigned long start_time;
unsigned long finish_time;
float elapsed_time = 0;
void setup() {
pinMode(pwmIn, INPUT);
pinMode(tps, INPUT);
pinMode(ebp, INPUT);
pinMode(pwmOut, OUTPUT);
pinMode(vssIn, INPUT);
digitalWrite(vssIn, HIGH);
pinMode(eb_switch, INPUT);
digitalWrite(eb_switch, HIGH);
//vehicle speed sensor
attachInterrupt(0, vspeed, FALLING);
//initialize all timers except for 0, to save time keeping functions
InitTimersSafe();
//sets the frequency for pwmOut pin 9
bool success = SetPinFrequencySafe(pwmOut, frequency);
//if the pin frequency was set successfully, turn pin 9 on
if(success) {
pinMode(pwmOut, OUTPUT);
}
//initialize PID variables
Input = ebp_newaverage;
Setpoint = 431; //test value = 30 psig
myPID.SetOutputLimits(38, 128);
//turn the PID on
myPID.SetMode(AUTOMATIC);
// lcd setup
lcd.begin(16, 2);
lcd.clear();
lcd.print("VGT%:");
lcd.setCursor(0,1);
lcd.print("BRK%:");
lcd.setCursor(10,1);
lcd.print("EBP:");
}
void vspeed(){
++vss_counter;
}
void loop() {
start_time = millis();
//duty cycle in measurement
dutycycle_reading = pulseIn(pwmIn, LOW);
durationL_newaverage = ((dutycycle_reading * 0.5) + ((1 - 0.5) * (durationL_oldaverage)));
durationL_oldaverage = durationL_newaverage;
dutycycleIn = ((durationL_oldaverage / 6000) * 100);
dutycycleOut = map(dutycycleIn, 0, 100, 0, 255);
//exhaust pressure sensor
ebp_reading = analogRead(ebp);
ebp_newaverage = ((ebp_reading * 0.3) + ((1 - 0.3) * (ebp_oldaverage)));
ebp_oldaverage = ebp_newaverage;
//throttle position sensor
tps_reading = analogRead(tps);
tps_newaverage = ((tps_reading * 0.3) + ((1 - 0.3) * (tps_oldaverage)));
tps_oldaverage = tps_newaverage;
//tps counter
if(tps_oldaverage <= 70){
++tps_counter;
}
else{
tps_counter = 0;
}
//vehicle speed sensor
current_time2 = millis();
if(current_time2 - previous_time2 >= 500) {
vss_freq = vss_counter;
vss_counter = 0;
previous_time2 = current_time2;
}
// exhaust brake on/off switch
eb_switch_value = digitalRead(eb_switch);
//Output
if(tps_counter >= 150 && vss_freq >= 15 && eb_switch_value == LOW){
Input = ebp_oldaverage;
myPID.Compute();
pwmWrite(pwmOut, pidOutput);
brake_status = 1; //EXHAUST BRAKE ON
//send fake exhaust pressure signal to ford pcm
}
else{
pwmWrite(pwmOut, dutycycleOut);
pidOutput = 0;
brake_status = 0; //EXHAUST BRAKE OFF
}
current_time1 = millis();
// LCD Display
if(current_time1 - previous_time1 > 500){
previous_time1 = current_time1;
//print vgt output
lcd.setCursor(5, 0);
lcd.print(" ");
lcd.setCursor(5, 0);
lcd.print(dutycycleOut * 0.3921, 0); // MULTIPLY BY 1.8 TO GET FROM 0 TO 100 FOR DISPLAY PURPOSE ONLY AFTER TESTING IS COMPLETE
//print exhaust brake output
lcd.setCursor(5, 1);
lcd.print(" ");
lcd.setCursor(5, 1);
lcd.print(pidOutput * 0.3921, 0);
// print exhaust brake status
lcd.setCursor(13, 0);
lcd.print(" ");
lcd.setCursor(13, 0);
if(brake_status == 1){
lcd.print("ON");
}
else{
lcd.print("OFF");
}
//print exhaust pressure sensor measurement
lcd.setCursor(14, 1);
lcd.print(" ");
lcd.setCursor(14, 1);
lcd.print(((ebp_oldaverage * 0.00488 * 19) - 10), 0);
}
finish_time = millis();
elapsed_time = (finish_time - start_time);
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(elapsed_time, 0);
}