pleasedontcode

Hall Sequencer rev_01

Aug 27th, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Hall Sequencer
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-08-27 08:17:41
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Turn on and off */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <EasyButton.h>\t//https://github.com/evert-arias/EasyButton
  28. #include <string.h> // for strcmp
  29.  
  30. // Compatibility note:
  31. // The PRELIMINARY CODE defines a pushbutton on D2 (Bbb_PushButton_PIN_D2 = 2).
  32. // The USER CODE uses D2 (pin 2) for the left Hall sensor (L_HALL_PIN = 2) with interrupts.
  33. // This creates a pin conflict. To keep the project compatible with the UNO and avoid
  34. // interrupt conflicts, the pushbutton pin definition is commented out and a note is added.
  35. // If you want to enable a pushbutton, move it to a different pin (e.g., D4) and instantiate EasyButton there.
  36. // Example (uncomment and adjust when ready):
  37. // EasyButton button(4);
  38.  
  39. // The EasyButton object on D2 is not used here due to the Hall sensor on D2.
  40. // EasyButton button(D2);
  41.  
  42. #define POWER_ENABLE_PIN 6 // Pin that enables power to the system (e.g., via a transistor or MOSFET)
  43.  
  44. bool systemOn = false; // Tracks whether the system (motors/power) is enabled
  45.  
  46.  
  47. /****** FUNCTION PROTOTYPES *****/
  48. void setup(void);
  49. void loop(void);
  50.  
  51. // System control functions
  52. void turnOnSystem();
  53. void turnOffSystem();
  54.  
  55. // Hall sensor ISRs
  56. void leftMotorPulse();
  57. void rightMotorPulse();
  58.  
  59. // Motor control functions
  60. void runMotor(int motor, int direction, int speed);
  61. void setMotorBrake(int motor);
  62.  
  63. // Sequence helpers
  64. void bothMotorsSlowThenFast();
  65. void individualMotorCycle();
  66. void bothMotorsOpposite();
  67.  
  68. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  69. // Note: Hall sensor pins and motor control pins are defined below.
  70. // The pushbutton pin from PRELIMINARY CODE (D2) is commented out due to pin conflict with L_HALL_PIN.
  71.  
  72. #define L_PWM_PIN 9      // PWM pin for speed control (must be a PWM pin on Arduino)
  73. #define L_DIR_PIN 8      // Direction pin (Yellow wire)
  74. #define L_BRAKE_PIN 7    // Brake pin (White wire)
  75. #define L_HALL_PIN 2     // Hall sensor pin (Green wire) - Use an interrupt pin
  76.  
  77. // Motor 2 (Right) Pin Definitions
  78. #define R_PWM_PIN 10     // PWM pin for speed control (must be a PWM pin)
  79. #define R_DIR_PIN 11     // Direction pin (Yellow wire)
  80. #define R_BRAKE_PIN 12   // Brake pin (White wire)
  81. #define R_HALL_PIN 3     // Hall sensor pin (Green wire) - Use an interrupt pin
  82.  
  83. // Motor Control Constants
  84. #define SLOW_SPEED 100   // PWM value for slow speed (0-255)
  85. #define FULL_SPEED 255   // PWM value for full speed (0-255)
  86. #define CW HIGH          // Clockwise direction
  87. #define CCW LOW          // Counter-clockwise direction
  88.  
  89. // Hall Sensor variables (optional, but good practice for speed/position feedback)
  90. volatile long leftMotorPulseCount = 0;
  91. volatile long rightMotorPulseCount = 0;
  92.  
  93. // NOTE on pin conflict:
  94. // The following line from PRELIMINARY CODE would conflict with L_HALL_PIN (2):
  95. // const uint8_t Bbb_PushButton_PIN_D2 = 2;
  96. // To avoid conflicts, it is commented out. If you move the pushbutton to another free pin,
  97. // you can define it here and use EasyButton accordingly.
  98.  
  99. //  const uint8_t Bbb_PushButton_PIN_D2 = 2; // Incompatible with Hall sensor on pin 2
  100.  
  101. void setup(void) {
  102.   // Setup Serial communication for debugging
  103.   Serial.begin(9600);
  104.   while (!Serial);
  105.  
  106.   // Power enable pin
  107.   pinMode(POWER_ENABLE_PIN, OUTPUT);
  108.   digitalWrite(POWER_ENABLE_PIN, LOW); // Start powered off
  109.  
  110.   // Set motor control pins as outputs
  111.   pinMode(L_PWM_PIN, OUTPUT);
  112.   pinMode(L_DIR_PIN, OUTPUT);
  113.   pinMode(L_BRAKE_PIN, OUTPUT);
  114.   pinMode(R_PWM_PIN, OUTPUT);
  115.   pinMode(R_DIR_PIN, OUTPUT);
  116.   pinMode(R_BRAKE_PIN, OUTPUT);
  117.  
  118.   // Configure Hall sensor pins with interrupts
  119.   pinMode(L_HALL_PIN, INPUT_PULLUP);
  120.   pinMode(R_HALL_PIN, INPUT_PULLUP);
  121.   attachInterrupt(digitalPinToInterrupt(L_HALL_PIN), leftMotorPulse, RISING);
  122.   attachInterrupt(digitalPinToInterrupt(R_HALL_PIN), rightMotorPulse, RISING);
  123.  
  124.   // Initialize motors to a stopped state
  125.   setMotorBrake(1);
  126.   setMotorBrake(2);
  127.   delay(100);
  128.   Serial.println("System initialized.");
  129. }
  130.  
  131. void loop() {
  132.   // Simple command parser over Serial to turn power on/off
  133.   static char command[12];
  134.   static uint8_t idx = 0;
  135.   while (Serial.available()) {
  136.     char c = Serial.read();
  137.     if (c == '\n' || c == '\r') {
  138.       command[idx] = '\0';
  139.       if (strcmp(command, "ON") == 0) {
  140.         turnOnSystem();
  141.       } else if (strcmp(command, "OFF") == 0) {
  142.         turnOffSystem();
  143.       } else if (strcmp(command, "TOGGLE") == 0) {
  144.         if (systemOn) turnOffSystem(); else turnOnSystem();
  145.       }
  146.       idx = 0;
  147.     } else {
  148.       if (idx < sizeof(command) - 1) {
  149.         command[idx++] = c;
  150.       }
  151.     }
  152.   }
  153.  
  154.   if (systemOn) {
  155.     Serial.println("Executing program sequence...");
  156.  
  157.     // 1. Run both motors slow, then stop, then full speed
  158.     bothMotorsSlowThenFast();
  159.     delay(2000);
  160.  
  161.     // 2. Run left motor back and forth, then right motor
  162.     individualMotorCycle();
  163.     delay(2000);
  164.  
  165.     // 3. Run both motors in opposite directions at the same time
  166.     bothMotorsOpposite();
  167.     delay(5000);
  168.   }
  169.   else {
  170.     static unsigned long lastHint = 0;
  171.     unsigned long now = millis();
  172.     if (now - lastHint > 5000) {
  173.       Serial.println("System is OFF. Send ON to power up.");
  174.       lastHint = now;
  175.     }
  176.   }
  177. }
  178.  
  179. // Function to turn system on
  180. void turnOnSystem() {
  181.   if (!systemOn) {
  182.     digitalWrite(POWER_ENABLE_PIN, HIGH);
  183.     // Allow power to settle
  184.     delay(100);
  185.     // Ensure motors are non-driven when powering up
  186.     digitalWrite(L_BRAKE_PIN, LOW);
  187.     digitalWrite(R_BRAKE_PIN, LOW);
  188.     systemOn = true;
  189.     Serial.println("System turned ON");
  190.   }
  191. }
  192.  
  193. // Function to turn system off
  194. void turnOffSystem() {
  195.   if (systemOn) {
  196.     // Brake both motors and disable power rail
  197.     setMotorBrake(1);
  198.     setMotorBrake(2);
  199.     digitalWrite(POWER_ENABLE_PIN, LOW);
  200.     systemOn = false;
  201.     Serial.println("System turned OFF");
  202.   }
  203. }
  204.  
  205. // Function to control a single motor
  206. void runMotor(int motor, int direction, int speed) {
  207.   if (!systemOn) return; // Do not run motors while powered off
  208.   int pwmPin, dirPin, brakePin;
  209.   if (motor == 1) { // Left Motor
  210.     pwmPin = L_PWM_PIN;
  211.     dirPin = L_DIR_PIN;
  212.     brakePin = L_BRAKE_PIN;
  213.   } else { // Right Motor
  214.     pwmPin = R_PWM_PIN;
  215.     dirPin = R_DIR_PIN;
  216.     brakePin = R_BRAKE_PIN;
  217.   }
  218.  
  219.   // Release brake, set direction, and set speed
  220.   digitalWrite(brakePin, LOW);
  221.   digitalWrite(dirPin, direction);
  222.   analogWrite(pwmPin, speed);
  223. }
  224.  
  225. // Function to brake a single motor
  226. void setMotorBrake(int motor) {
  227.   int pwmPin, brakePin;
  228.   if (motor == 1) {
  229.     pwmPin = L_PWM_PIN;
  230.     brakePin = L_BRAKE_PIN;
  231.   } else {
  232.     pwmPin = R_PWM_PIN;
  233.     brakePin = R_BRAKE_PIN;
  234.   }
  235.  
  236.   // Set PWM to 0 and engage the brake
  237.   analogWrite(pwmPin, 0);
  238.   digitalWrite(brakePin, HIGH);
  239. }
  240.  
  241. // --- Sequence Functions ---
  242.  
  243. void bothMotorsSlowThenFast() {
  244.   Serial.println("Running both motors slow...");
  245.   runMotor(1, CW, SLOW_SPEED);
  246.   runMotor(2, CW, SLOW_SPEED);
  247.   delay(5000);
  248.  
  249.   Serial.println("Stopping motors...");
  250.   setMotorBrake(1);
  251.   setMotorBrake(2);
  252.   delay(2000);
  253.  
  254.   Serial.println("Running both motors full speed...");
  255.   runMotor(1, CW, FULL_SPEED);
  256.   runMotor(2, CW, FULL_SPEED);
  257.   delay(5000);
  258.  
  259.   Serial.println("Stopping motors...");
  260.   setMotorBrake(1);
  261.   setMotorBrake(2);
  262. }
  263.  
  264. void individualMotorCycle() {
  265.   // Left motor CW then CCW
  266.   Serial.println("Left motor CW...");
  267.   runMotor(1, CW, FULL_SPEED);
  268.   setMotorBrake(2);
  269.   delay(3000);
  270.  
  271.   Serial.println("Left motor CCW...");
  272.   runMotor(1, CCW, FULL_SPEED);
  273.   delay(3000);
  274.  
  275.   setMotorBrake(1);
  276.   delay(1000);
  277.  
  278.   // Right motor CW then CCW
  279.   Serial.println("Right motor CW...");
  280.   runMotor(2, CW, FULL_SPEED);
  281.   setMotorBrake(1);
  282.   delay(3000);
  283.  
  284.   Serial.println("Right motor CCW...");
  285.   runMotor(2, CCW, FULL_SPEED);
  286.   delay(3000);
  287.  
  288.   setMotorBrake(2);
  289. }
  290.  
  291. void bothMotorsOpposite() {
  292.   Serial.println("Both motors opposite directions...");
  293.   runMotor(1, CW, FULL_SPEED);
  294.   runMotor(2, CCW, FULL_SPEED);
  295.   delay(5000);
  296.  
  297.   setMotorBrake(1);
  298.   setMotorBrake(2);
  299. }
  300.  
  301. // Hall Sensor Interrupt Service Routines (ISRs)
  302. void leftMotorPulse() {
  303.   leftMotorPulseCount++;
  304. }
  305.  
  306. void rightMotorPulse() {
  307.   rightMotorPulseCount++;
  308. }
  309.  
  310. /* END CODE */
  311.  
Advertisement
Add Comment
Please, Sign In to add comment