// ============================================================================ // Source: STEAM Clown - www.steamclown.org // GitHub: https://github.com/jimTheSTEAMClown/arduinoCode // Hacker: Jim Burnham - STEAM Clown, Engineer, Maker, Propmaster & Adrenologist // This example code under the GNU Lesser General Public License v3.0 // and any docs and lesson examples is licensed under the CC BY-NC-SA 3.0. // https://creativecommons.org/licenses/by-nc-sa/3.0/ // Create Date: 09/16/2018 // Design Name: dcMotorTest_02_STEAMClown // Description: This motor test is the same as dcMotorTest_STEAMClown, but // uses finction calls // Dependencies: // Revision: // Revision 0.02 - Updated for SVCTE Mechatronics Class 2018 - 09/16/2018 // Revision 0.01 - Created 09/16/2018 // Additional Comments: // // ============================================================================ // General Pins and Variables int boardLED=13; // Motor Control Pins and Variables byte motorSpeed=0; // change this (0-255) to control the speed of the motors // Left Motor const int leftDirControl1 = 2; const int leftDirControl2 = 3; const int leftSpeedControlPin = 4; // Needs to be a PWM pin to be able to control motor speed void setup() { // initialize serial communication @ 9600 baud: Serial.begin(9600); //Define L298N Dual H-Bridge Motor Controller Pins pinMode(leftDirControl1,OUTPUT); pinMode(leftDirControl2,OUTPUT); pinMode(leftSpeedControlPin,OUTPUT); // Stop All Motors stopMotor(); delay(500); } void loop() { // same code as dcMotorTest, but uses function calls. call functions with motor speed stopMotor(); delay(500); motorSpeed = 140; //Sets speed variable via PWM = 140; spinMotorClockwise(motorSpeed); delay(1000); stopMotor(); delay(500); motorSpeed = 140; //Sets speed variable via PWM = 140; spinMotorCounterClockwise(motorSpeed); delay(1000); } //------------------------------------------------------------------------ int spinMotorClockwise(int spinSpeed) { analogWrite(leftSpeedControlPin, spinSpeed);//Sets speed variable via PWM digitalWrite(leftDirControl1, LOW); digitalWrite(leftDirControl2, HIGH); Serial.print("Clockwise with a speed of "); Serial.println(spinSpeed); Serial.println(" "); // Creates a blank line printed on the serial monitor } int spinMotorCounterClockwise(int spinSpeed) { analogWrite(leftSpeedControlPin, spinSpeed);//Sets speed variable via PWM digitalWrite(leftDirControl1, HIGH); digitalWrite(leftDirControl2, LOW); Serial.print("CounterClockwise with a speed of "); Serial.println(spinSpeed); Serial.println(" "); } void stopMotor() { analogWrite(leftSpeedControlPin, 0);//Sets speed variable via PWM digitalWrite(leftDirControl1, LOW); digitalWrite(leftDirControl2, LOW); Serial.println("All Motors STOPPED"); Serial.println(" "); }