-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added constructors for two pin configuration. Added new flexible methods for callback usage. Added new costant STOP. Added class to initialize both motors at once. Rewrited old examples. Writed new examples. Redrawed wiring schemas. Created new folder for schemas.
- Loading branch information
1 parent
5ea93e9
commit 7e7ce8c
Showing
23 changed files
with
2,153 additions
and
163 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,78 @@ | ||
/* | ||
Author : Andrea Lombardo | ||
Site : https://www.lombardoandrea.com | ||
Source : https://github.com/AndreaLombardo/L298N/ | ||
Here you can see how to work with callback option. | ||
Every five seconds callback function is called and encreasing the speed. | ||
When maximum speed is reached then starts again from zero. | ||
In the meaning time your loop cicle is never bloked. | ||
Speed range go from 0 to 255, default is 100. | ||
Use setSpeed(speed) to change. | ||
Sometimes at lower speed motors seems not running. | ||
It's normal, may depends by motor and power supply. | ||
Wiring schema in file "L298N - Schema_with_EN_pin.png" | ||
*/ | ||
|
||
// Include the library | ||
#include <L298N.h> | ||
|
||
//pin definition | ||
#define EN 9 | ||
#define IN1 8 | ||
#define IN2 7 | ||
// Pin definition | ||
const unsigned int IN1 = 7; | ||
const unsigned int IN2 = 8; | ||
const unsigned int EN = 9; | ||
|
||
//create a motor instance | ||
// Create one motor instance | ||
L298N motor(EN, IN1, IN2); | ||
|
||
//initial speed | ||
// Initial speed | ||
unsigned short theSpeed = 0; | ||
|
||
void setup() { | ||
|
||
//set the initial speed | ||
void setup() | ||
{ | ||
// Set initial speed | ||
motor.setSpeed(theSpeed); | ||
|
||
} | ||
|
||
void loop() { | ||
|
||
//move motor for 5 seconds and then execute the callback function | ||
void loop() | ||
{ | ||
// Move motor for 5 seconds and then execute the callback function | ||
// Easy way | ||
motor.forwardFor(5000, callback); | ||
|
||
// More flexible way | ||
// motor.runFor(5000, L298N::FORWARD, callback); | ||
} | ||
|
||
void callback() { | ||
/* | ||
Each time the callback function is called increase the speed of the motor or reset to 0. | ||
This function can be named with any name. | ||
*/ | ||
void callback() | ||
{ | ||
|
||
//each time the callback function is called increase the speed of the motor or reset to 0 | ||
if (theSpeed <= 255) { | ||
// If speed is over 255 then encrease | ||
if (theSpeed <= 255) | ||
{ | ||
theSpeed += 25; | ||
} else { | ||
} | ||
else | ||
{ | ||
theSpeed = 0; | ||
} | ||
|
||
//re-enable motor movements | ||
/* | ||
Each time a callback function is called | ||
the motor is placed in "don't move" status | ||
To restore it's capability use reset() method. | ||
*/ | ||
motor.reset(); | ||
//set the new speed | ||
motor.setSpeed(theSpeed); | ||
|
||
// Set the new speed | ||
motor.setSpeed(theSpeed); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Author : Andrea Lombardo | ||
Site : https://www.lombardoandrea.com | ||
Source : https://github.com/AndreaLombardo/L298N/ | ||
Based on Arduino Basic Fade example. | ||
Speed range go from 0 to 255, default is 100. | ||
Use setSpeed(speed) to change. | ||
Sometimes at lower speed motors seems not running. | ||
It's normal, may depends by motor and power supply. | ||
Wiring schema in file "L298N - Schema_with_EN_pin.png" | ||
*/ | ||
|
||
// Include the library | ||
#include <L298N.h> | ||
|
||
// Pin definition | ||
const unsigned int IN1 = 7; | ||
const unsigned int IN2 = 8; | ||
const unsigned int EN = 9; | ||
|
||
// Create one motor instance | ||
L298N motor(EN, IN1, IN2); | ||
|
||
int speedness = 0; | ||
int speedAmount = 1; | ||
|
||
void setup() | ||
{ | ||
// Used to display information | ||
Serial.begin(9600); | ||
|
||
// Wait for Serial Monitor to be opened | ||
while (!Serial) | ||
{ | ||
//do nothing | ||
} | ||
} | ||
|
||
void loop() | ||
{ | ||
// Apply faded speed to both motors | ||
motor.setSpeed(speedness); | ||
|
||
// Tell motor A to go forward (may depend by your wiring) | ||
motor.forward(); | ||
|
||
// Alternative method: | ||
// motor.run(L298N::FORWARD); | ||
|
||
//print the motor satus in the serial monitor | ||
printSomeInfo(); | ||
|
||
// Change the "speedness" for next time through the loop: | ||
speedness = speedness + speedAmount; | ||
|
||
// Reverse the direction of the fading at the ends of the fade: | ||
if (speedness <= 0 || speedness >= 255) | ||
{ | ||
speedAmount = -speedAmount; | ||
} | ||
|
||
// Wait for 30 milliseconds to see the dimming effect | ||
delay(30); | ||
} | ||
|
||
/* | ||
Print some informations in Serial Monitor | ||
*/ | ||
void printSomeInfo() | ||
{ | ||
Serial.print("Motor is moving = "); | ||
Serial.print(motor.isMoving() ? "YES" : "NO"); | ||
Serial.print(" at speed = "); | ||
Serial.println(motor.getSpeed()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Author : Andrea Lombardo | ||
Site : https://www.lombardoandrea.com | ||
Source : https://github.com/AndreaLombardo/L298N/ | ||
Here you can see how to work in a common configuration without the needed of Enable pin. | ||
Make sure your module has a jumper in place. | ||
When L298N has a jumper instead of Enable pin, the speed is always 255. | ||
Wiring schema in file "L298N - Schema_without_EN_pin.png" | ||
*/ | ||
|
||
// Include the library | ||
#include <L298N.h> | ||
|
||
// Pin definition | ||
const unsigned int IN1 = 7; | ||
const unsigned int IN2 = 8; | ||
|
||
// Create one motor instance | ||
L298N motor(IN1, IN2); | ||
|
||
void setup() | ||
{ | ||
// Used to display information | ||
Serial.begin(9600); | ||
|
||
// Wait for Serial Monitor to be opened | ||
while (!Serial) | ||
{ | ||
//do nothing | ||
} | ||
} | ||
|
||
void loop() | ||
{ | ||
|
||
// Tell the motor to go forward (may depend by your wiring) | ||
motor.forward(); | ||
|
||
// Alternative method: | ||
// motor.run(L298N::FORWARD); | ||
|
||
//print the motor satus in the serial monitor | ||
Serial.print("Is moving = "); | ||
Serial.println(motor.isMoving()); | ||
|
||
delay(3000); | ||
|
||
// Stop | ||
motor.stop(); | ||
|
||
// Alternative method: | ||
// motor.run(L298N::STOP); | ||
|
||
Serial.print("Is moving = "); | ||
Serial.println(motor.isMoving()); | ||
|
||
delay(3000); | ||
|
||
// Tell the motor to go back (may depend by your wiring) | ||
motor.backward(); | ||
|
||
// Alternative method: | ||
// motor.run(L298N::BACKWARD); | ||
|
||
Serial.print("Is moving = "); | ||
Serial.println(motor.isMoving()); | ||
|
||
delay(3000); | ||
|
||
// Stop | ||
motor.stop(); | ||
|
||
Serial.print("Is moving = "); | ||
Serial.println(motor.isMoving()); | ||
|
||
delay(3000); | ||
} |
Oops, something went wrong.