har prøvd, kom sikkert ut litt feil, det jeg lurer på er om noen har noen fremgangsmåte å få gjort dette på?
Dette er det jeg har til nå, men jeg får ikke til at rpm skal gå stabilt oppover når jeg trykker accelerator, å får ikke til at ved hvær 2000rpm må holde clutch for å bytte til et gir opp eller ned..
får det enda ikke til: dette er det jeg har så langt..
Kode
const int S_NEUTRAL = 1;
const int S_GEAR_UP = 2;
const int S_GEAR_DOWN = 3;
const int S_ACCELERATION = 4;
const int S_BRAKE = 5;
const int S_CLUTCH = 6;
const int S_ENGINE_STALL = 7;
const int S_ENGINE_STOP = 8;
const int S_ENGINE_START = 9;
const int carMaxRev = 9000;
int currentState;
int Gear;
int Rpm;
int Velocity;
int targetRpm; // Target RPM for gearing up
// Replace the following with the actual pin numbers or variables for your buttons and LEDs
const int neutralButtonPin = 2;
const int gearUpButtonPin = 3;
const int gearDownButtonPin = 4;
const int accelerationButtonPin = 11;
const int brakeButtonPin = 6;
const int clutchButtonPin = 7;
const int neutralLedPin = 8;
const int engineOffLedPin = 12;
const int accelerationLedPin = 13;
void setup() {
currentState = S_NEUTRAL;
Gear = 0;
Velocity = 0;
targetRpm = 2000; // Initial target RPM
// Setup buttons
pinMode(neutralButtonPin, INPUT);
pinMode(gearUpButtonPin, INPUT);
pinMode(gearDownButtonPin, INPUT);
pinMode(accelerationButtonPin, INPUT);
pinMode(brakeButtonPin, INPUT);
pinMode(clutchButtonPin, INPUT);
// Setup LEDs
pinMode(neutralLedPin, OUTPUT);
pinMode(engineOffLedPin, OUTPUT);
pinMode(accelerationLedPin, OUTPUT);
// Start serial communication
Serial.begin(9600);
}
float rpmToVelocityConversion(int rpm) {
// Conversion logic here
}
void checkGear() {
if (Gear > 5) {
Gear = 5;
}
if (Gear < 0) {
Gear = 0;
}
}
void changeStateTo(int newState) {
currentState = newState;
}
void updateLEDs() {
// Control LEDs based on the current state
digitalWrite(neutralLedPin, currentState == S_NEUTRAL);
digitalWrite(engineOffLedPin, currentState == S_ENGINE_STOP || currentState == S_ENGINE_STALL);
digitalWrite(accelerationLedPin, currentState == S_ACCELERATION);
}
void loop() {
// Read inputs from buttons
int neutralButtonState = digitalRead(neutralButtonPin);
int gearUpButtonState = digitalRead(gearUpButtonPin);
int gearDownButtonState = digitalRead(gearDownButtonPin);
int accelerationButtonState = digitalRead(accelerationButtonPin);
int brakeButtonState = digitalRead(brakeButtonPin);
int clutchButtonState = digitalRead(clutchButtonPin);
// Update RPM based on the current state and gear
Rpm = rpmToVelocityConversion(analogRead(A0)); // Replace A0 with the actual RPM sensor input pin
// Print RPM to Serial Monitor
Serial.print("RPM: ");
Serial.println(Rpm);
switch (currentState) {
case S_NEUTRAL:
// Handle S_NEUTRAL behavior
// For example, turn on neutral LED
break;
case S_GEAR_UP:
// Handle S_GEAR_UP behavior
if (Rpm >= targetRpm) {
// Gearing up after reaching the target RPM
Gear++;
targetRpm = 2000; // Set target RPM for the next gear change
changeStateTo(S_NEUTRAL);
}
break;
case S_GEAR_DOWN:
// Handle S_GEAR_DOWN behavior
break;
case S_ACCELERATION:
// Handle S_ACCELERATION behavior
while (accelerationButtonState == HIGH) {
// Increase RPM only if the acceleration button is held down
if (Rpm < targetRpm) {
// Increase RPM until it reaches the target RPM
Rpm += 10; // Adjust the increment value as needed
// Add a small delay to avoid rapid RPM increase
delay(50);
// Print RPM to Serial Monitor
Serial.print("RPM: ");
Serial.println(Rpm);
}
// Read inputs from buttons
accelerationButtonState = digitalRead(accelerationButtonPin);
clutchButtonState = digitalRead(clutchButtonPin);
gearUpButtonState = digitalRead(gearUpButtonPin);
}
// Check for clutch and gear up button presses
if (Rpm >= targetRpm && clutchButtonState == HIGH && gearUpButtonState == HIGH) {
changeStateTo(S_GEAR_UP);
}
break;
case S_BRAKE:
// Handle S_BRAKE behavior
break;
case S_CLUTCH:
// Handle S_CLUTCH behavior
break;
case S_ENGINE_STALL:
// Handle S_ENGINE_STALL behavior
break;
case S_ENGINE_STOP:
// Handle S_ENGINE_STOP behavior
break;
case S_ENGINE_START:
// Handle S_ENGINE_START behavior
break;
}
// Update common tasks or variables here
// Update LEDs based on the current state
updateLEDs();
// Check for button presses and update the state accordingly
if (neutralButtonState == HIGH) {
changeStateTo(S_NEUTRAL);
} else if (gearUpButtonState == HIGH) {
changeStateTo(S_GEAR_UP);
} else if (gearDownButtonState == HIGH) {
changeStateTo(S_GEAR_DOWN);
} else if (accelerationButtonState == HIGH) {
changeStateTo(S_ACCELERATION);
} else if (brakeButtonState == HIGH) {
changeStateTo(S_BRAKE);
} else if (clutchButtonState == HIGH) {
changeStateTo(S_CLUTCH);
}
// Perform a delay or use non-blocking techniques based on your requirements
delay(100);
}
Sist endret av Dyret; 28. november 2023 kl. 16:58.
Grunn: La til [code]