Arduino Mega 2560 DC Motor code
DC motors can be controlled by the L298N DC motor driver IC, which is connected to your microcontroller. L298Ns can control up to 2 DC motors. You can easily add motors through the program code. You can set DC motor speed by changing the duty-cycle of each PWM signal. The PWM is a square-wave signal which has two parameters: frequency and duty-cycle. If a PWM signal's duty-cycle is 100% than the motor spins with maximum rpm. In case of 0% the motor will stop. Motor speed and direction in your Ozeki software can be changed on each motor with the motor's own adjustable slide bar from -100% to 100%. If the slide crosses 0 than the motor will change spin direction.
Required hardware
- Arduino Mega 2560
- DC Motor Controller Board LM298
- DC Motor(s)
Source code to install on controller
Before you upload this code to your Arduino, please format the EEPROM...
#include <OzIDManager.h>
#include <OzDCMotorControllerL298N.h>
OzIDManager* manager;
OzDCMotorControllerL298N* dcMotorController;
const DCMotorMode motorMode = DCMotorMode::PWM; //Default value: PWM
const DCMotorUnit motorUnit = DCMotorUnit::BOTH; //Default value: MOTOR_B
const int inPin1 = 14; //Default value: 14
const int inPin2 = 15; //Default value: 15
const int inPin3 = 16; //Default value: 16
const int inPin4 = 17; //Default value: 17
const int ENA = 9;
const int ENB = 10;
void setup()
{
Serial.begin(115200);
manager = new OzIDManager;
manager->_sendACK = true;
manager->_checksum = true;
OzCommunication::setIDManager(manager);
dcMotorController =
new OzDCMotorControllerL298N(motorMode, inPin1, inPin2, inPin3, inPin4, ENA, ENB);
int x=1;
manager->sendLinkSetup();
manager->PrintWelcomeLine(dcMotorController, x++, "MyDCMotors");
}
void loop()
{
OzCommunication::communicate();
dcMotorController->ownLoop();
}