i got my code running great and a clean hall input to the uno. the problem comes in when i have the 2x 12 volt VVT solenoids powered up and functioning.
i put 1kΩ Resistor to 5v and 0.1 µF Capacitor to ground for the analog 1 input but it is not enough to get it back to clean.
i have shielded cable this is running through now too
top one is with solenoids working and bottom is with clean input and no output
const byte analogPin = A0; // Analog pin connected to the Hall sensor
const float threshold = 4.5; // Voltage threshold for the cycle
const int numCycles = 4; // Number of cycles to count
unsigned long cycleStartTimes[numCycles]; // Array to store start times for each cycle
unsigned long cycleEndTimes[numCycles]; // Array to store end times for each cycle
int cycleCount = 0;
bool aboveThreshold = false;
// Define output pin for PWM
const byte VVT1 = 9;
const byte VVT2 = 10;
void setup() {
Serial.begin(115200);
cycleStartTimes[0] = millis(); // Record the initial start time
// Initialize the PWM output pin
pinMode(VVT1, OUTPUT);
pinMode(VVT2, OUTPUT);
}
void loop() {
// Read the voltage from the analog pin
float voltage = analogRead(analogPin) * (5.0 / 1023.0);
// Detect the transition from below to above threshold
if (!aboveThreshold && voltage > threshold) {
aboveThreshold = true; // We're above the threshold
}
// Detect the transition from above to below threshold
if (aboveThreshold && voltage < threshold) {
aboveThreshold = false; // We're below the threshold
// Record the end time of the current cycle
cycleEndTimes[cycleCount] = millis();
cycleCount++; // Increment the cycle count
// If we have completed the required number of cycles
if (cycleCount >= numCycles) {
unsigned long totalTime = 0;
// Calculate the total time for the cycles
for (int i = 0; i < numCycles; i++) {
unsigned long cycleTime = cycleEndTimes[i] - cycleStartTimes[i];
totalTime += cycleTime;
}
// Calculate the average time per cycle
float averageTime = totalTime / (float)numCycles;
float averageTimeSeconds = averageTime / 1000.0; // Convert milliseconds to seconds
float rpm = 60.0 / averageTimeSeconds; // Calculate RPM based on average cycle time
// Divide the RPM by 2
rpm /= 2;
// Convert RPM to a whole number
int wholeRpm = (int)(rpm + 0.5); // Simple rounding without math.h
Serial.print("RPM: ");
Serial.println(wholeRpm);
int pwmValue = 0;
// Map the RPM value to the desired PWM range based on the RPM range
if (wholeRpm >= 800 && wholeRpm <= 1500) {
pwmValue = map(wholeRpm, 800, 1500, 20, 240); // Off idle
} else if (wholeRpm > 1500 && wholeRpm <= 5000) {
pwmValue = map(wholeRpm, 1501, 5000, 240, 20); // Full RPM range
}
// Ensure the PWM value is within the range
pwmValue = constrain(pwmValue, 20, 240);
// Set the PWM output
analogWrite(VVT1, pwmValue);
analogWrite(VVT2, pwmValue);
Serial.print("PWM %");
Serial.println(pwmValue / 2.55);
// Reset for next measurement
cycleCount = 0;
cycleStartTimes[0] = millis(); // Record the new initial start time
} else {
// Record the start time for the next cycle
cycleStartTimes[cycleCount] = millis();
}
}
}





