ok, so i have my grand trans code that does everything but no first gear on the physical transmission. i have a basic code to run the same case block and it gets first gear output from the transmission. i dont get what could be different?
#include <SPI.h>
#include <mcp_can.h>
MCP_CAN CAN0(53); // CS pin for MCP2515
// Variables to store data from CAN message
int MAP = 0; // Manifold Air Pressure (kPa)
int RPM = 0; // Engine RPM
int CLT = 0; // Coolant Temperature (deg F)
int TPS = 0; // Throttle Position (%)
const int pin45 = 45; // Pin for TPS indicator
unsigned long previousMillis = 0; // Store last time pin 45 state changed
const long interval = 500; // Blink interval (500 milliseconds)
bool pin45State = LOW; // Keep track of the state of pin 45
bool initialized = false; // Add this globally to track initialization state
////////////////////////////////////////////////////////////////////////////////////////////////////
// transmission info
const int reverse_pin = A0; // Pin for reverse switch
const int drive_pin = A1; // Pin for drive switch
const int shiftdown_pin = A2; // Pin for shiftdown switch
const int shiftdown2_pin = A3; // Pin for second shiftdown switch
const int shiftup_pin = A4; // Pin for shiftup switch
const int shiftup2_pin = A5; // Pin for second shiftup switch
const int A_clutch = 2; // PWM output for Solenoid 1
const int B_clutch = 3; // PWM output for Solenoid 2
const int C_clutch = 4; // PWM output for Solenoid 3
const int EDS4 = 5; // PWM output for Solenoid 4
const int MV1 = 6; // Digital output for MV1 solenoid
const int PSI = 11; // PWM output for Pressure Solenoid
const int A_grab = 255;
const int A_free = 0;
const int A_slip = 70;
const int B_grab = 0;
const int B_free = 255;
const int B_slip = 55;
const int C_grab = 255;
const int C_free = 0;
const int C_slip = 55;
const int D_grab = 0;
const int D_free = 255;
const int D_slip = 55;
const int E_grab = 0;
const int E_free = 255;
const int E_slip = 70;
const int D_clutch = LOW; // MV1 condition for multi valve
const int E_clutch = HIGH;
int counter = 1; // Current gear
int prevCounter = 1; // Previous gear
int lastDriveGear = 1; // To remember the last gear in Drive mode
bool inPark = false; // Indicates if the vehicle is in park mode
bool inReverse = false; // Indicates if the vehicle is in reverse mode
bool inDrive = false; // Indicates if the vehicle is in drive mode
bool canShiftDown = true; // Flag to control downshifting
bool canShiftUp = true; // Flag to control upshifting
void handleShiftdown() {
if (digitalRead(shiftdown_pin) == LOW && digitalRead(shiftdown2_pin) == HIGH && canShiftDown) {
canShiftDown = false;
counter--;
if (counter < 1) {
counter = 1; // Ensure gear doesn't go below 1st
}
Serial.println();
Serial.print(" Down Shifted into ");
} else if (digitalRead(shiftdown_pin) == HIGH && digitalRead(shiftdown2_pin) == LOW && !canShiftDown) {
canShiftDown = true;
Serial.println();
}
}
void handleShiftup() {
if (digitalRead(shiftup_pin) == LOW && digitalRead(shiftup2_pin) == HIGH && canShiftUp) {
canShiftUp = false;
counter++;
if (counter > 6) {
counter = 6; // Ensure gear doesn't go above 6th
}
Serial.println();
Serial.print(" Up Shifted into ");
} else if (digitalRead(shiftup_pin) == HIGH && digitalRead(shiftup2_pin) == LOW && !canShiftUp) {
canShiftUp = true;
Serial.println();
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
// VVT and TCC code
const int TCC = 7;
const byte VVT1 = 8;
const byte VVT2 = 9;
bool TCCEnabled = false; // Track the state of TCC
bool TCCEngaged = false; // true if engaged, false if not
int pwmValue = 0;
const int fadeStartRPM = 2600; // RPM threshold to start the fade
const int fadeStopRPM = 1500; // RPM threshold to stop the fade and turn off
// Idle Range
const int idleRangeRPMMin = 1300; // Minimum RPM for the idle range
const int idleRangeRPMMax = 1800; // Maximum RPM for the idle range
const int idleRangeStartPWM = 200; // Start PWM percentage for the idle range
const int idleRangeEndPWM = 255; // End PWM percentage for the idle range
// Torque Range
const int torqueRangeRPMMin = 1800; // Minimum RPM for the torque range
const int torqueRangeRPMMax = 4000; // Maximum RPM for the torque range
const int torqueRangeStartPWM = 255; // Start PWM percentage for the torque range
const int torqueRangeEndPWM = 255; // End PWM percentage for the torque range
// Peak Range
const int peakRangeRPMMin = 4000; // Minimum RPM for the peak range
const int peakRangeRPMMax = 5500; // Maximum RPM for the peak range
const int peakRangeStartPWM = 0; // Start PWM percentage for the peak range
const int peakRangeEndPWM = 0; // End PWM percentage for the peak range
void fadeEDS4();
void startTCC();
void stopTCC();
void fadeTCC(int startValue, int endValue, int duration);
void setup() {
delay(1000);
// Set pin modes
pinMode(reverse_pin, INPUT_PULLUP);
pinMode(drive_pin, INPUT_PULLUP);
pinMode(shiftdown_pin, INPUT_PULLUP);
pinMode(shiftdown2_pin, INPUT_PULLUP);
pinMode(shiftup_pin, INPUT_PULLUP);
pinMode(shiftup2_pin, INPUT_PULLUP);
Serial.begin(115200);
pinMode(A_clutch, OUTPUT);
pinMode(B_clutch, OUTPUT);
pinMode(C_clutch, OUTPUT);
pinMode(EDS4, OUTPUT);
pinMode(PSI, OUTPUT);
pinMode(MV1, OUTPUT);
pinMode(TCC, OUTPUT);
pinMode(pin45, OUTPUT); // RPM indicator
pinMode(10, OUTPUT);
pinMode(VVT1, OUTPUT);
pinMode(VVT2, OUTPUT);
pinMode(TCC, OUTPUT);
stopTCC(); // Make sure TCC is off initially
// Initialize MCP2515 CAN interface at 500 kbps with 8 MHz crystal
if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
Serial.println("Arduino CAN BUS is good!");
}
// Set MCP2515 to normal mode to receive messages
CAN0.setMode(MCP_NORMAL);
}
void loop() {
long unsigned int rxId;
unsigned char len = 0;
unsigned char buf[8];
// Read message from CAN bus
if (CAN0.readMsgBuf(&rxId, &len, buf) == CAN_OK) {
// Check if the received message ID matches the Dash Broadcasting ID (0x5E8)
if (rxId == 0x5E8) {
//Serial.println("Received Dash Broadcasting message:");
// Extract values from the data buffer (big-endian format)
// MAP (Manifold Air Pressure) in kPa, stored in bytes 0 and 1
MAP = (buf[0] << 8) | buf[1]; // Big-endian: MSB first
MAP /= 10; // Divide by 10 according to the data format (multiply/divide info)
// RPM (Engine RPM), stored in bytes 2 and 3
RPM = (buf[2] << 8) | buf[3]; // No scaling needed
// CLT (Coolant Temperature) in degrees Fahrenheit, stored in bytes 4 and 5
CLT = (buf[4] << 8) | buf[5];
CLT /= 10; // Divide by 10 to get temperature in degrees
// TPS (Throttle Position) as a percentage, stored in bytes 6 and 7
TPS = (buf[6] << 8) | buf[7];
TPS /= 10; // Divide by 10 to get percentage
// Print the extracted values to the Serial Monitor
/* Serial.print("MAP: ");
Serial.print(MAP);
Serial.print(" kPa, ");
Serial.print("RPM: ");
Serial.print(RPM);
Serial.print(", ");
Serial.print("CLT: ");
Serial.print(CLT);
Serial.print(" deg F, ");
Serial.print("TPS: ");
Serial.print(TPS);
Serial.println(" %");
*/
int mappedPSI;
if (TPS > 70) {
mappedPSI = 0; // Set PSI to 0 if TPS is above 70%
} else {
mappedPSI = map(TPS, -10, 70, 255, 0); // Map TPS to PSI 255-0
}
analogWrite(PSI, mappedPSI); // Set the PSI output based on the mapped or overridden value
// Control pin 45 based on RPM
unsigned long currentMillis = millis(); // Get the current time
if (RPM < 50) { // CAN BUS Warning led on board
// Blink pin 45 on and off every 500 milliseconds
if (currentMillis - previousMillis >= interval) {
// Save the last time the pin state changed
previousMillis = currentMillis;
// If the output is LOW, turn it HIGH, and vice-versa
pin45State = !pin45State;
digitalWrite(pin45, pin45State);
}
} else if (RPM > 400) {
// Keep pin 45 HIGH if RPM is over 400
digitalWrite(pin45, HIGH);
} else {
// Otherwise, ensure pin 45 is LOW
digitalWrite(pin45, LOW);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Reverse Mode
if (digitalRead(reverse_pin) == LOW) { // Check if reverse pin is LOW (indicating reverse)
if (!inReverse) { // Only enter this block if not already in reverse
inReverse = true; // Set the reverse flag
inPark = false; // Exit park mode
if (inDrive) { // If we were in Drive, store the lastDriveGear
lastDriveGear = counter;
inDrive = false;
}
counter = -1; // Set the counter to -1 for reverse
Serial.println();
Serial.println(" Reverse ");
delay(50);
}
} else { // If reverse pin is HIGH (not in reverse)
if (inReverse) { // If previously in reverse
inReverse = false; // Exit reverse mode
}
}
// Park Mode ......................................................................................................
if (digitalRead(reverse_pin) == HIGH && digitalRead(drive_pin) == HIGH) {
if (!inPark) {
inPark = true; // Enter "park" mode
inReverse = false; // Exit "reverse" mode
if (inDrive) { // If we were in Drive, store the lastDriveGear
lastDriveGear = counter;
inDrive = false;
}
counter = 0; // Set counter to 0 for park/neutral
Serial.println();
Serial.println(" Neutral ");
delay(50);
}
}
// Drive Mode .....................................................................................................
if (digitalRead(drive_pin) == LOW) {
if (!inDrive) { // If we're newly entering Drive mode
inDrive = true; // Set drive mode flag
inPark = false; // Exit park mode
inReverse = false; // Exit reverse mode
if (lastDriveGear == 0) {
lastDriveGear = 1; // Ensure it starts in first gear if the last drive gear was undefined
}
counter = lastDriveGear; // Restore the last drive gear
prevCounter = 0; // Force gear change logic to execute
Serial.println();
Serial.println("Drive");
delay(50);
}
// Handle shifting
handleShiftdown();
handleShiftup();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Gear changing logic
if (counter != prevCounter) {
switch (counter) {
case -1:
analogWrite(B_clutch, B_slip);
analogWrite(EDS4, D_slip);
delay (900);
stopTCC();
digitalWrite(MV1, D_clutch);
analogWrite(A_clutch, A_free);
analogWrite(B_clutch, B_grab);
analogWrite(C_clutch, C_free);
analogWrite(EDS4, D_grab);
break;
case 0:
stopTCC();
digitalWrite(MV1, D_clutch);
analogWrite(A_clutch, 0);
analogWrite(B_clutch, 0);
analogWrite(C_clutch, 0);
analogWrite(EDS4, 0);
break;
case 1:
Serial.println("first gear");
digitalWrite(MV1, LOW);
analogWrite(A_clutch, 250);
analogWrite(B_clutch, 255);
analogWrite(C_clutch, 0);
analogWrite(EDS4, 70);
Serial.println("80");
delay(500);
analogWrite(EDS4, 50);
Serial.println("50");
delay(500);
analogWrite(EDS4, 0);
Serial.println("0");
break;
case 2:
Serial.println(" 2nd gear");
switch (prevCounter) {
case 1:
// analogWrite(C_clutch, C_slip);
// analogWrite(EDS4, D_slip);
Serial.println(" up slip ");
stopTCC();
//delay(200);
digitalWrite(MV1, D_clutch);
analogWrite(A_clutch, 255);
analogWrite(B_clutch, 255);
analogWrite(C_clutch, 255);
analogWrite(EDS4, 255);
Serial.println(" up engaged ");
break;
case 3:
// analogWrite(B_clutch, B_slip);
// analogWrite(C_clutch, C_slip);
Serial.println(" down slip ");
stopTCC();
delay(500);
digitalWrite(MV1, D_clutch);
analogWrite(A_clutch, 255);
analogWrite(B_clutch, 255);
analogWrite(C_clutch, 255);
analogWrite(EDS4, 255);
Serial.println(" down engaged ");
}
break;
case 3:
Serial.println(" 3rd gear");
switch (prevCounter) {
case 2:
analogWrite(B_clutch, B_slip);
analogWrite(C_clutch, C_slip);
Serial.println(" up slip");
stopTCC();
delay(150);
digitalWrite(MV1, D_clutch);
analogWrite(A_clutch, 255);
analogWrite(B_clutch, 0);
analogWrite(C_clutch, 0);
analogWrite(EDS4, 255);
digitalWrite(MV1, E_clutch);
Serial.println(" up engaged ");
break;
case 4:
stopTCC();
digitalWrite(MV1, D_clutch);
analogWrite(A_clutch, 255);
analogWrite(B_clutch, 0);
analogWrite(C_clutch, 0);
analogWrite(EDS4, 255);
digitalWrite(MV1, E_clutch);
Serial.println(" easy money!");
}
break;
case 4:
Serial.println(" 4th gear");
switch (prevCounter) {
case 3:
stopTCC();
digitalWrite(MV1, E_clutch);
analogWrite(A_clutch, 255);
analogWrite(B_clutch, 255);
analogWrite(C_clutch, 0);
analogWrite(EDS4, 0);
Serial.println(" slammin up! ");
delay(1000);
startTCC();
Serial.println(" TCC enabled");
break;
case 5:
stopTCC();
analogWrite(A_clutch, A_slip);
analogWrite(B_clutch, B_slip);
Serial.println(" down slip ");
delay (200);
digitalWrite(MV1, E_clutch);
analogWrite(A_clutch, 255);
analogWrite(B_clutch, 255);
analogWrite(C_clutch, 0);
analogWrite(EDS4, 0);
Serial.println(" down engaged ");
delay(1000);
startTCC();
Serial.println(" TCC enabled");
}
break;
case 5:
Serial.println(" 5th gear");
stopTCC();
digitalWrite(MV1, E_clutch);
analogWrite(A_clutch, 0);
analogWrite(B_clutch, 0);
analogWrite(C_clutch, 0);
analogWrite(EDS4, 0);
Serial.println(" engaged!");
delay(1000);
startTCC();
Serial.println(" TCC enabled");
break;
case 6:
Serial.println(" 6th gear");
stopTCC();
digitalWrite(MV1, E_clutch);
analogWrite(A_clutch, 0);
analogWrite(B_clutch, 255);
analogWrite(C_clutch, 255);
analogWrite(EDS4, 0);
Serial.println(" engaged!");
delay(1000);
startTCC();
Serial.println(" TCC enabled");
break;
}
prevCounter = counter;
delay(25);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// VVT and TCC
// Map the RPM value to the desired PWM range based on the idle range
if (RPM < idleRangeRPMMin) {
pwmValue = 0; // Default PWM value for RPMs below the idle range
} else if (RPM >= idleRangeRPMMin && RPM <= idleRangeRPMMax) {
pwmValue = map(RPM, idleRangeRPMMin, idleRangeRPMMax, idleRangeStartPWM, idleRangeEndPWM);
} else if (RPM >= torqueRangeRPMMin && RPM <= torqueRangeRPMMax) {
pwmValue = map(RPM, torqueRangeRPMMin, torqueRangeRPMMax, torqueRangeStartPWM, torqueRangeEndPWM);
} else if (RPM >= peakRangeRPMMin && RPM <= peakRangeRPMMax) {
pwmValue = map(RPM, peakRangeRPMMin, peakRangeRPMMax, peakRangeStartPWM, peakRangeEndPWM);
}
// Set the PWM output
analogWrite(VVT1, pwmValue);
analogWrite(VVT2, pwmValue);
//Serial.println(pwmValue);
// Check if the RPM is greater than the fade start threshold and TCC is enabled
if (!TCCEngaged && TCCEnabled && RPM > fadeStartRPM) {
fadeTCC(0, 255, 1000); // start, end, time
}
// Check if the RPM is below the fade stop threshold
if (RPM < fadeStopRPM) {
analogWrite(TCC, 0); // Turn off the PSI output
TCCEngaged = false;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// functions
void fadeEDS4(int startValue, int endValue, int duration) {
int steps = 100; // Number of steps for the fade
int delayTime = duration / steps;
float stepValue = (endValue - startValue) / float(steps);
for (int i = 0; i <= steps; i++) {
int value = startValue + (stepValue * i);
analogWrite(EDS4, value);
delay(delayTime);
}
}
// Handles starting TCC by enabling TCC control
void startTCC() {
TCCEnabled = true; // Enable TCC
TCCEngaged = false; // TCC disengaged
}
// Handles stopping TCC by setting it to 0 and disabling TCC control
void stopTCC() {
analogWrite(TCC, 0); // Immediately set TCC value to 0
TCCEnabled = false; // Disable TCC
TCCEngaged = false; // TCC disengaged
}
// Non-blocking fade function for TCC, with a final set to 255 after fade completes
void fadeTCC(int startValue, int endValue, int duration) {
static int currentStep = 0;
static unsigned long previousMillis = 0;
static int steps = 150; // Number of steps for the fade
static int delayTime = duration / steps;
static float stepValue = (endValue - startValue) / float(steps);
static bool fading = false;
// Start the fading process if it's not already started
if (!fading) {
currentStep = 0;
previousMillis = millis();
fading = true;
}
// Continue the fading process
if (fading) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= delayTime) {
previousMillis = currentMillis; // Reset the timer
int value = startValue + (stepValue * currentStep);
analogWrite(TCC, value);
currentStep++;
// Check if fade is complete
if (currentStep > steps) {
fading = false; // Stop the fading
TCCEngaged = true;
analogWrite(TCC, 255); // Ensure TCC stays at 255 after fade completes
}
}
}
}
const int A_clutch = 2; // PWM output for Solenoid 1
const int B_clutch = 3; // PWM output for Solenoid 2
const int C_clutch = 4; // PWM output for Solenoid 3
const int EDS4 = 5; // PWM output for Solenoid 4
const int MV1 = 6;
const int TCC = 7;
const int PSI = 11; // PWM output for Pressure Solenoid
const int VVT1 = 8;
const int VVT2 = 9;
// 1st A/D // 2nd A/C // 3rd A/B // 4th A/E // 5th B/E // 6th C/E
void setup() {
pinMode(MV1, OUTPUT);
pinMode(PSI, OUTPUT);
pinMode(A_clutch, OUTPUT);
pinMode(B_clutch, OUTPUT);
pinMode(C_clutch, OUTPUT);
pinMode(EDS4, OUTPUT);
pinMode(VVT1, OUTPUT);
pinMode(VVT2, OUTPUT);
pinMode(TCC, OUTPUT);
}
void loop() { // I RUN THE BLOCK ABOVE AND IT FUNCTIONS AND GETS FIRST GEAR. BELOW WONT EVEN RUN THE DELAY AND SET TO 0! BASTARD
Serial.println("first gear");
digitalWrite(MV1, LOW);
analogWrite(A_clutch, 250);
analogWrite(B_clutch, 255);
analogWrite(C_clutch, 0);
analogWrite(EDS4, 70);
Serial.println("80");
delay(500);
analogWrite(EDS4, 50);
Serial.println("50");
delay(500);
analogWrite(EDS4, 0);
Serial.println("0");
while (1) {}
}