Testing my project on wokwi dosent work right

Hi i am making a project of moving a ferris wheel with 11 buttons on wokwi and chat gpt
Play button start the sequence: move the wheel to each cabin waiting for 5 seconds on each, do a number of complete revolution choose it with the encrease button and repeat the first sequence and stop
encrease: set the number of revolutions
cabins 1 to 8: go to an specific cabin
stop: stop all the sistem
My problem is every time I press play, the system have to validate if is in cabin 1 before starting the sequence and move there if is not and does that, for example i press cabin 7 and stops encrease revolution and press play the mottor goes to cabin 1 and start from there the sequence but after i press stop and encrease a revolution and press play start from where i press stop doesn't move to cabin 1 and i don't know if is because i am testing this on wokwi or is a problem with the code

// Versión: 2.7

#include <Arduino.h>

// Pines de conexión
#define STEP_PIN 20
#define DIR_PIN 21
#define INCREASE_PIN 5
#define PLAY_PIN 2
#define STOP_PIN 4
#define CABIN_BUTTON_START 12
#define CABIN_BUTTON_END 19

// Configuración del motor paso a paso
#define STEPS_PER_REV 200
#define CABIN_STEPS 25
#define TOTAL_STEPS 200

// Variables de estado
int currentSteps = 0; // Posición actual en pasos
int targetSteps = 0;
int selectedRevolutions = 0;
bool isRunning = false;
volatile bool stopRequested = false; // Volatile para usar con interrupciones

void IRAM_ATTR handleStop() {
    stopRequested = true; // Interrupción para manejar el botón "Stop"
}

void setup() {
    Serial.begin(115200);

    // Configurar pines
    pinMode(STEP_PIN, OUTPUT);
    pinMode(DIR_PIN, OUTPUT);
    pinMode(INCREASE_PIN, INPUT_PULLUP);
    pinMode(PLAY_PIN, INPUT_PULLUP);
    pinMode(STOP_PIN, INPUT_PULLUP);

    for (int i = CABIN_BUTTON_START; i <= CABIN_BUTTON_END; i++) {
        pinMode(i, INPUT_PULLUP);
    }

    // Configurar interrupción para el botón "Stop"
    attachInterrupt(digitalPinToInterrupt(STOP_PIN), handleStop, FALLING);

    // Mensaje inicial
    Serial.println("Ferris Wheel By Ferdinand Insert Coin");
}

void loop() {
    if (digitalRead(INCREASE_PIN) == LOW && !isRunning) {
        increaseRevolutions();
        delay(200); // Anti-rebote
    }

    if (digitalRead(PLAY_PIN) == LOW && !isRunning) {
        playSequence();
        delay(200); // Anti-rebote
    }

    for (int i = CABIN_BUTTON_START; i <= CABIN_BUTTON_END; i++) {
        if (digitalRead(i) == LOW && !isRunning) {
            moveToCabin(i - CABIN_BUTTON_START + 1);
            delay(200); // Anti-rebote
        }
    }
}

void increaseRevolutions() {
    selectedRevolutions++;
    Serial.print("Revolutions selected: ");
    Serial.println(selectedRevolutions);
}

void playSequence() {
    if (selectedRevolutions == 0) {
        Serial.println("Please insert coin");
        return;
    }

    // Verificar y mover a Cabina 1 antes de iniciar cualquier secuencia
    if (currentSteps != 0) {
        Serial.println("Returning to Cabin 1 before starting sequence...");
        moveToCabin(1);
    }

    Serial.println("Starting passenger boarding...");
    stopRequested = false;
    isRunning = true;

    // Subida de pasajeros
    performBoardingSequence();

    // Vueltas completas
    performRevolutions();

    // Bajada de pasajeros (ahora en sentido horario desde Cabina 1)
    performUnboardingSequence();

    // Verificar y mover a Cabina 1 al final de la secuencia
    if (currentSteps != 0) {
        Serial.println("Returning to Cabin 1 after sequence...");
        moveToCabin(1);
    }

    // Reset del sistema
    isRunning = false;
    selectedRevolutions = 0;
    Serial.println("System reset. Ready for new game.");
}

void performBoardingSequence() {
    for (int cabin = 1; cabin <= 8; cabin++) {
        if (stopRequested) return; // Detener si se solicitó
        moveToCabin(cabin);
        delay(5000); // Espera en la cabina
    }
}

void performRevolutions() {
    int totalStepsToMove = selectedRevolutions * TOTAL_STEPS;

    for (int i = 0; i < totalStepsToMove; i++) {
        if (stopRequested) return; // Detener si se solicitó
        stepMotor(true);  // Mueve el motor en sentido horario
    }
}

void performUnboardingSequence() {
    for (int cabin = 1; cabin <= 8; cabin++) {
        if (stopRequested) return; // Detener si se solicitó
        moveToCabin(cabin);
        delay(5000); // Espera en la cabina
    }
}

void moveToCabin(int cabin) {
    int target = (cabin - 1) * CABIN_STEPS;
    int stepsToMove = calculateShortestPath(target);

    moveSteps(stepsToMove);
    currentSteps = (currentSteps + stepsToMove + TOTAL_STEPS) % TOTAL_STEPS;

    Serial.print("Moved to cabin ");
    Serial.println(cabin);
}

int calculateShortestPath(int target) {
    int distance = target - currentSteps;

    // Asegura la distancia más corta para el movimiento
    if (distance > TOTAL_STEPS / 2) {
        distance -= TOTAL_STEPS;
    } else if (distance < -TOTAL_STEPS / 2) {
        distance += TOTAL_STEPS;
    }

    return distance;
}

void moveSteps(int steps) {
    bool direction = steps > 0;
    digitalWrite(DIR_PIN, direction);

    for (int i = 0; i < abs(steps); i++) {
        if (stopRequested) {
            Serial.println("Emergency stop requested. Stopping motor.");
            return; // Detener la secuencia inmediatamente
        }
        stepMotor(direction);
    }
}

void stepMotor(bool direction) {
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(500);
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(500);
}

void emergencyStop() {
    Serial.println("Emergency stop activated. System halted.");
    isRunning = false;
    stopRequested = true;
}

Thanks

Which ESP32 are you using? (S3?)

Would you use translate.google.com to get your meaning in English more clearly? I am struggling with all the details you provide.

Explain how this should work.

I found the problem.

2 Likes

My project consists of controlling a Ferris wheel with 11 buttons (Play, Stop, Increase, and Cabins 1 to 8). The Increase button adds a number of turns to the Ferris wheel. The Play button makes it rotate in a programmed sequence plus the turns I previously selected. The cabin buttons move the Ferris wheel to a specific cabin in the most efficient and shortest way possible, and the Stop button acts as an emergency stop for the Play button sequence.

The Play button always positions the Ferris wheel at Cabin 1 before starting the sequence. For example, if I move the Ferris wheel to Cabin 6, add turns from there, and press Play, it will first move to Cabin 1 and then start the turns, and this is working correctly. When I select a specific cabin, it always returns to Cabin 1 as I want. However, it should do the same when I press Stop and the system resets. Currently, when I add more turns and press Play, the motor starts from where it left off after pressing Stop, and that is not what I want. I want it to always check where the Ferris wheel is, and if it is not at Cabin 1, move back to it.
Chat gpt wrote the code and I am testing the code on wokwi because right know i can use real hardware and so far the code is working but the stop button part i can not able to get it and I dont know if is wokwi or not

You want the wheel to return to cabin 1 when you press STOP?

You already have a function called moveToCabin() . If it's working correctly as you say, it's enough to call it after you press the stop button. Thus, everytime you have this line:

if (stopRequested) return;

or whatever looks like it, you should have:

if (stopRequested) moveToCabin(1);

Probably not Wokwi. But Wokwi allows you to share your project. Why don't you do it and let people test your ChatGPT's code?

Thanks for the answer let me share the wokwi here

I have read your descriptions, but would you make a list of steps I must make to see the problem?

  1. press "x"
  2. see the wheel turn to ?here?
  3. press "y" three times
  4. see the wheel do ?this?

Thanks
Sequence play:
1st press increase to increase the revolutions when you press play
2nd press play and the motor do the sequence plus the revolution set it with the increase button, finish that stopping and resetting the system to be able to play again
Cabins buttons:
move the system to each cabin
Stop:
Is like emergency stop stopping everything and resetting the system to play again or go to an specific cabin
I want when i press play always start from cabin 1 moving, the motor to that cabin if not on that one and i can achieve that only if i am in another cabin pressing cabin buttons not stop button
examples:
start the sequence press stop in cabin 4 encrease revolution and press play again the sequence dosent move to cabin one start when i press stop
Press button cabin 6 increase revolutions press play and the motor moves to cabin 1 and start the sequence
I hope its clear because english is my second language

i was able to fix it after many conversation with chat gpt

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.