Monday, November 3, 2025

FYP JD 2025... Coding Projek Quratul Sugoi...

 


// Linear motor control

// Assumes switches/buttons are wired to GND when active and pins use INPUT_PULLUP.

// So: IDLE = HIGH, PRESSED/ACTIVE = LOW


const byte mspeed = 255; // motor pwm speed (0-255)

const int RPWM   = 5;    // right PWM pin (one direction)

const int LPWM   = 6;    // left PWM pin (other direction)


const int button2 = 2;   // trigger forward (edge)

const int button3 = 3;   // trigger backward (edge)

const int estop   = 7;   // emergency stop (active LOW)

const int limitF  = 8;   // forward limit switch (active LOW)

const int limitB  = 9;   // backward limit switch (active LOW)


// motorState:  1 = forward (extend)

//              -1 = backward (retract)

//               0 = stop

int motorState = 0;


// debounce variables for the two buttons

unsigned long lastDebounceTime2 = 0;

unsigned long lastDebounceTime3 = 0;

const unsigned long debounceDelay = 50; // ms


int lastRawButton2 = HIGH;

int lastRawButton3 = HIGH;

int stableButton2 = HIGH;

int stableButton3 = HIGH;


void setup() {

  pinMode(button2, INPUT_PULLUP);

  pinMode(button3, INPUT_PULLUP);

  pinMode(estop, INPUT_PULLUP);

  pinMode(limitF, INPUT_PULLUP);

  pinMode(limitB, INPUT_PULLUP);


  pinMode(RPWM, OUTPUT);

  pinMode(LPWM, OUTPUT);


  analogWrite(RPWM, 0);

  analogWrite(LPWM, 0);

}


void loop() {

  // -- Emergency stop (active LOW) --

  if (digitalRead(estop) == LOW) { // estop pressed -> stop immediately

    stopMotor();

    motorState = 0;

    // Do not return; continue to read buttons (you may want to require estop release before restarting)

  }


  // -- Read and debounce button2 (forward) --

  int raw2 = digitalRead(button2);

  if (raw2 != lastRawButton2) {

    lastDebounceTime2 = millis();

  }

  if ((millis() - lastDebounceTime2) > debounceDelay) {

    if (raw2 != stableButton2) {

      // state changed after debounce

      int prevStable = stableButton2;

      stableButton2 = raw2;

      // falling edge: HIGH -> LOW is a press (with INPUT_PULLUP)

      if (prevStable == HIGH && stableButton2 == LOW) {

        // request forward

        motorState = 1;

      }

    }

  }

  lastRawButton2 = raw2;


  // -- Read and debounce button3 (backward) --

  int raw3 = digitalRead(button3);

  if (raw3 != lastRawButton3) {

    lastDebounceTime3 = millis();

  }

  if ((millis() - lastDebounceTime3) > debounceDelay) {

    if (raw3 != stableButton3) {

      int prevStable = stableButton3;

      stableButton3 = raw3;

      if (prevStable == HIGH && stableButton3 == LOW) {

        // request backward

        motorState = -1;

      }

    }

  }

  lastRawButton3 = raw3;


  // -- Limit switches: active LOW -- stop motor if limit reached while moving --

  if (motorState == 1 && digitalRead(limitF) == LOW) {

    // reached forward limit

    stopMotor();

    motorState = 0;

  }

  if (motorState == -1 && digitalRead(limitB) == LOW) {

    // reached backward limit

    stopMotor();

    motorState = 0;

  }


  // -- Drive motor based on motorState --

  if (motorState == 1) {

    // Forward / extend

    analogWrite(RPWM, 0);

    analogWrite(LPWM, mspeed);

  } else if (motorState == -1) {

    // Backward / retract

    analogWrite(RPWM, mspeed);

    analogWrite(LPWM, 0);

  } else {

    // Stop

    stopMotor();

  }

}


void stopMotor() {

  analogWrite(RPWM, 0);

  analogWrite(LPWM, 0);

}


###########################################

bagi kegunaan student2 lain pulak....

No comments:

Post a Comment