Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Hall Sequencer
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-08-27 08:17:41
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Turn on and off */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <EasyButton.h>\t//https://github.com/evert-arias/EasyButton
- #include <string.h> // for strcmp
- // Compatibility note:
- // The PRELIMINARY CODE defines a pushbutton on D2 (Bbb_PushButton_PIN_D2 = 2).
- // The USER CODE uses D2 (pin 2) for the left Hall sensor (L_HALL_PIN = 2) with interrupts.
- // This creates a pin conflict. To keep the project compatible with the UNO and avoid
- // interrupt conflicts, the pushbutton pin definition is commented out and a note is added.
- // If you want to enable a pushbutton, move it to a different pin (e.g., D4) and instantiate EasyButton there.
- // Example (uncomment and adjust when ready):
- // EasyButton button(4);
- // The EasyButton object on D2 is not used here due to the Hall sensor on D2.
- // EasyButton button(D2);
- #define POWER_ENABLE_PIN 6 // Pin that enables power to the system (e.g., via a transistor or MOSFET)
- bool systemOn = false; // Tracks whether the system (motors/power) is enabled
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // System control functions
- void turnOnSystem();
- void turnOffSystem();
- // Hall sensor ISRs
- void leftMotorPulse();
- void rightMotorPulse();
- // Motor control functions
- void runMotor(int motor, int direction, int speed);
- void setMotorBrake(int motor);
- // Sequence helpers
- void bothMotorsSlowThenFast();
- void individualMotorCycle();
- void bothMotorsOpposite();
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- // Note: Hall sensor pins and motor control pins are defined below.
- // The pushbutton pin from PRELIMINARY CODE (D2) is commented out due to pin conflict with L_HALL_PIN.
- #define L_PWM_PIN 9 // PWM pin for speed control (must be a PWM pin on Arduino)
- #define L_DIR_PIN 8 // Direction pin (Yellow wire)
- #define L_BRAKE_PIN 7 // Brake pin (White wire)
- #define L_HALL_PIN 2 // Hall sensor pin (Green wire) - Use an interrupt pin
- // Motor 2 (Right) Pin Definitions
- #define R_PWM_PIN 10 // PWM pin for speed control (must be a PWM pin)
- #define R_DIR_PIN 11 // Direction pin (Yellow wire)
- #define R_BRAKE_PIN 12 // Brake pin (White wire)
- #define R_HALL_PIN 3 // Hall sensor pin (Green wire) - Use an interrupt pin
- // Motor Control Constants
- #define SLOW_SPEED 100 // PWM value for slow speed (0-255)
- #define FULL_SPEED 255 // PWM value for full speed (0-255)
- #define CW HIGH // Clockwise direction
- #define CCW LOW // Counter-clockwise direction
- // Hall Sensor variables (optional, but good practice for speed/position feedback)
- volatile long leftMotorPulseCount = 0;
- volatile long rightMotorPulseCount = 0;
- // NOTE on pin conflict:
- // The following line from PRELIMINARY CODE would conflict with L_HALL_PIN (2):
- // const uint8_t Bbb_PushButton_PIN_D2 = 2;
- // To avoid conflicts, it is commented out. If you move the pushbutton to another free pin,
- // you can define it here and use EasyButton accordingly.
- // const uint8_t Bbb_PushButton_PIN_D2 = 2; // Incompatible with Hall sensor on pin 2
- void setup(void) {
- // Setup Serial communication for debugging
- Serial.begin(9600);
- while (!Serial);
- // Power enable pin
- pinMode(POWER_ENABLE_PIN, OUTPUT);
- digitalWrite(POWER_ENABLE_PIN, LOW); // Start powered off
- // Set motor control pins as outputs
- pinMode(L_PWM_PIN, OUTPUT);
- pinMode(L_DIR_PIN, OUTPUT);
- pinMode(L_BRAKE_PIN, OUTPUT);
- pinMode(R_PWM_PIN, OUTPUT);
- pinMode(R_DIR_PIN, OUTPUT);
- pinMode(R_BRAKE_PIN, OUTPUT);
- // Configure Hall sensor pins with interrupts
- pinMode(L_HALL_PIN, INPUT_PULLUP);
- pinMode(R_HALL_PIN, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(L_HALL_PIN), leftMotorPulse, RISING);
- attachInterrupt(digitalPinToInterrupt(R_HALL_PIN), rightMotorPulse, RISING);
- // Initialize motors to a stopped state
- setMotorBrake(1);
- setMotorBrake(2);
- delay(100);
- Serial.println("System initialized.");
- }
- void loop() {
- // Simple command parser over Serial to turn power on/off
- static char command[12];
- static uint8_t idx = 0;
- while (Serial.available()) {
- char c = Serial.read();
- if (c == '\n' || c == '\r') {
- command[idx] = '\0';
- if (strcmp(command, "ON") == 0) {
- turnOnSystem();
- } else if (strcmp(command, "OFF") == 0) {
- turnOffSystem();
- } else if (strcmp(command, "TOGGLE") == 0) {
- if (systemOn) turnOffSystem(); else turnOnSystem();
- }
- idx = 0;
- } else {
- if (idx < sizeof(command) - 1) {
- command[idx++] = c;
- }
- }
- }
- if (systemOn) {
- Serial.println("Executing program sequence...");
- // 1. Run both motors slow, then stop, then full speed
- bothMotorsSlowThenFast();
- delay(2000);
- // 2. Run left motor back and forth, then right motor
- individualMotorCycle();
- delay(2000);
- // 3. Run both motors in opposite directions at the same time
- bothMotorsOpposite();
- delay(5000);
- }
- else {
- static unsigned long lastHint = 0;
- unsigned long now = millis();
- if (now - lastHint > 5000) {
- Serial.println("System is OFF. Send ON to power up.");
- lastHint = now;
- }
- }
- }
- // Function to turn system on
- void turnOnSystem() {
- if (!systemOn) {
- digitalWrite(POWER_ENABLE_PIN, HIGH);
- // Allow power to settle
- delay(100);
- // Ensure motors are non-driven when powering up
- digitalWrite(L_BRAKE_PIN, LOW);
- digitalWrite(R_BRAKE_PIN, LOW);
- systemOn = true;
- Serial.println("System turned ON");
- }
- }
- // Function to turn system off
- void turnOffSystem() {
- if (systemOn) {
- // Brake both motors and disable power rail
- setMotorBrake(1);
- setMotorBrake(2);
- digitalWrite(POWER_ENABLE_PIN, LOW);
- systemOn = false;
- Serial.println("System turned OFF");
- }
- }
- // Function to control a single motor
- void runMotor(int motor, int direction, int speed) {
- if (!systemOn) return; // Do not run motors while powered off
- int pwmPin, dirPin, brakePin;
- if (motor == 1) { // Left Motor
- pwmPin = L_PWM_PIN;
- dirPin = L_DIR_PIN;
- brakePin = L_BRAKE_PIN;
- } else { // Right Motor
- pwmPin = R_PWM_PIN;
- dirPin = R_DIR_PIN;
- brakePin = R_BRAKE_PIN;
- }
- // Release brake, set direction, and set speed
- digitalWrite(brakePin, LOW);
- digitalWrite(dirPin, direction);
- analogWrite(pwmPin, speed);
- }
- // Function to brake a single motor
- void setMotorBrake(int motor) {
- int pwmPin, brakePin;
- if (motor == 1) {
- pwmPin = L_PWM_PIN;
- brakePin = L_BRAKE_PIN;
- } else {
- pwmPin = R_PWM_PIN;
- brakePin = R_BRAKE_PIN;
- }
- // Set PWM to 0 and engage the brake
- analogWrite(pwmPin, 0);
- digitalWrite(brakePin, HIGH);
- }
- // --- Sequence Functions ---
- void bothMotorsSlowThenFast() {
- Serial.println("Running both motors slow...");
- runMotor(1, CW, SLOW_SPEED);
- runMotor(2, CW, SLOW_SPEED);
- delay(5000);
- Serial.println("Stopping motors...");
- setMotorBrake(1);
- setMotorBrake(2);
- delay(2000);
- Serial.println("Running both motors full speed...");
- runMotor(1, CW, FULL_SPEED);
- runMotor(2, CW, FULL_SPEED);
- delay(5000);
- Serial.println("Stopping motors...");
- setMotorBrake(1);
- setMotorBrake(2);
- }
- void individualMotorCycle() {
- // Left motor CW then CCW
- Serial.println("Left motor CW...");
- runMotor(1, CW, FULL_SPEED);
- setMotorBrake(2);
- delay(3000);
- Serial.println("Left motor CCW...");
- runMotor(1, CCW, FULL_SPEED);
- delay(3000);
- setMotorBrake(1);
- delay(1000);
- // Right motor CW then CCW
- Serial.println("Right motor CW...");
- runMotor(2, CW, FULL_SPEED);
- setMotorBrake(1);
- delay(3000);
- Serial.println("Right motor CCW...");
- runMotor(2, CCW, FULL_SPEED);
- delay(3000);
- setMotorBrake(2);
- }
- void bothMotorsOpposite() {
- Serial.println("Both motors opposite directions...");
- runMotor(1, CW, FULL_SPEED);
- runMotor(2, CCW, FULL_SPEED);
- delay(5000);
- setMotorBrake(1);
- setMotorBrake(2);
- }
- // Hall Sensor Interrupt Service Routines (ISRs)
- void leftMotorPulse() {
- leftMotorPulseCount++;
- }
- void rightMotorPulse() {
- rightMotorPulseCount++;
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment