// Motorcycle Speed & RPM Pulse Simulator with slow/fast mode switch
void setup() {
Serial.begin(115200);
Serial.println("\nMotorcycle Pulse Simulator (RPM, SPEED)");
setupInputSim();
}
void loop() {
runInputSim();
}
//
# define xSPEED 7 // simulated SPEED output
# define xRPM 6 // simulated RPM output
# define speedControl A0
# define rpmControl A1
// slow mode for testing this simulation
# define modeSwitch 5 // slow/fast mode switch (pullup)
// Global scale factor
float slowScale = 0.1; // slow mode
float fastScale = 1.0; // real mode
float globalScale = slowScale; // initial
byte pulseControl[] = {speedControl, rpmControl};
byte pulseOutput[] = {xSPEED, xRPM};
const byte nPulses = sizeof pulseControl / sizeof *pulseControl;
unsigned long lastPulseTime[nPulses] = {0, 0};
bool pulseState[nPulses] = {LOW, LOW};
// Realistic ranges
float minFreq[nPulses] = {1, 10}; // Hz
float maxFreq[nPulses] = {50, 10000}; // Hz
void setupInputSim() {
pinMode(xSPEED, OUTPUT);
pinMode(xRPM, OUTPUT);
pinMode(modeSwitch, INPUT_PULLUP); // switch to toggle modes
// Read switch on reset for mode
if (digitalRead(modeSwitch) == LOW) {
globalScale = slowScale;
} else {
globalScale = fastScale;
}
}
void runInputSim() {
unsigned long now = micros();
for (byte i = 0; i < nPulses; i++) {
// Read analog slider
int val = analogRead(pulseControl[i]);
// Map slider to frequency
float freq = minFreq[i] + (maxFreq[i] - minFreq[i]) * val / 1023.0;
freq *= globalScale; // apply current mode
// Compute half-period in microseconds
unsigned long halfPeriod = (unsigned long)(500000.0 / freq); // 50% duty cycle
// Toggle output when half-period elapsed
if (now - lastPulseTime[i] >= halfPeriod) {
pulseState[i] = !pulseState[i];
digitalWrite(pulseOutput[i], pulseState[i]);
lastPulseTime[i] = now;
}
}
}
/*
// this is what I fed to chatGPT after explaining why I needded to do this
// side task - generate pulses
# define xSPEED 7 // simulated SPEED output
# define xRPM 6 // simulated RPM output
# define speedControl A0
# define rpmControl A1
void setup() {
Serial.begin(115200);
Serial.println("\nf(RPM, SPEED) -> gear ratio");
setupInputSim();
}
void loop() {
runInputSim;
}
// side task - generate pulses
void setupInputSim()
{
pinMode(xRPM, OUTPUT);
pinMode(xSPEED, OUTPUT);
}
byte pulseControl[] = {speedControl, rpmControl};
byte pulseOutput[] = {xSPEED, xRPM};
const byte nPulses = sizeof pulseControl / sizeof *pulseControl
void runInputSim() {
// loop over all
// read the control voltage (will be one slide fader)
// control a proportion pulse created with a micros()-based timer
}
*/
SPEED
SLOW ....... FAST
RPM
RPM
SPEED
(read at startup)