/* 6/5/2024 ENGR 103 Final Project Jimi Hendrix Robot Jelani Dupon & Esther Prusynski Heer */ //Define both pins for each motor #define M2A 17 #define M2B 16 #define M1A 19 #define M1B 18 //Define set variables #define SLOWEST_SPEED 95 #define MOVE_DURATION 800 #define SPIN_DURATION 1840 #define SHAKE_DURATION 500 #define SHAKE_REPETITIONS 1 //Set speed and direction of both motors void motor(uint8_t left, uint8_t right) { if (left == 128) { ledcWrite(3, 0); ledcWrite(2, 0); } else if (left > 128) { left = map(left, 128, 255, SLOWEST_SPEED, 255); //Left motor moves forward ledcWrite(3, 0); ledcWrite(2, left); } else { left = map(left, 128, 0, SLOWEST_SPEED, 255); //Left motor moves in reverse ledcWrite(3, left); ledcWrite(2, 0); } if (right == 128) { ledcWrite(1, 0); ledcWrite(0, 0); } else if (right > 128) { //Right motor moves forward right = map(right, 128, 255, SLOWEST_SPEED, 255); ledcWrite(1, 0); ledcWrite(0, right); } else { right = map(right, 128, 0, SLOWEST_SPEED, 255); //Right motor moves in reverse ledcWrite(1, right); ledcWrite(0, 0); } } //Sets the 4 motor control lines as outputs void setup() { pinMode(M1A, OUTPUT); pinMode(M1B, OUTPUT); pinMode(M2A, OUTPUT); pinMode(M2B, OUTPUT); // Connects 4 PWM channels to the 4 motor control Pins ledcSetup(0, 30000, 8); ledcAttachPin(M1A, 0); ledcSetup(1, 30000, 8); ledcAttachPin(M1B, 1); ledcSetup(2, 30000, 8); ledcAttachPin(M2A, 2); ledcSetup(3, 30000, 8); ledcAttachPin(M2B, 3); } void loop() { // First 360-degree spin //Left motor moves forward, right motor holds motor(255, 0); delay(SPIN_DURATION); motor(128, 128); // Stops motors delay(1000); // Move straight for 1 ft //Both motors move forward motor(255, 255); delay(MOVE_DURATION); motor(128, 128); delay(1000); // Perform shaking/rocking movement for (int i = 0; i < SHAKE_REPETITIONS; i++) { // Shake left motor(128, 255); delay(SHAKE_DURATION); // Shake right motor(255, 128); delay(SHAKE_DURATION); } // Reverse backwards //Both motors reverse motor(0, 0); delay(MOVE_DURATION); motor(128, 128); delay(1000);} }