Created
July 6, 2023 10:43
-
-
Save dwaard/d25767459b3266ba0bae34d45792fc47 to your computer and use it in GitHub Desktop.
Grove motor driver example
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
#include "Grove_Motor_Driver_TB6612FNG.h" | |
#include <Wire.h> | |
MotorDriver motor; | |
#define FWD 128 | |
#define BCK -FWD | |
double radians; | |
void setup() | |
{ | |
// join I2C bus (I2Cdev library doesn't do this automatically) | |
Wire.begin(); | |
Serial.begin(9600); | |
Serial.println("Initializing motor"); | |
motor.init(); | |
Serial.println("Done"); | |
radians=0; | |
} | |
void loop() { | |
int speed = sin(radians) * 80; | |
radians += 0.4; | |
Serial.print("Angle: "); | |
Serial.print(radians); | |
Serial.print("; Speed: "); | |
Serial.println(speed); | |
motor.dcMotorRun(MOTOR_CHA, speed); | |
motor.dcMotorRun(MOTOR_CHB, -speed); | |
delay(500); | |
} | |
// void loop() | |
// { | |
// // drive 2 dc motors at speed=255, clockwise | |
// Serial.println("Turn direction 1"); | |
// delay(1000); | |
// // brake | |
// Serial.println("brake"); | |
// motor.dcMotorBrake(MOTOR_CHA); | |
// motor.dcMotorBrake(MOTOR_CHB); | |
// delay(1000); | |
// // drive 2 dc motors at speed=200, anticlockwise | |
// Serial.println("Turn direction 2"); | |
// motor.dcMotorRun(MOTOR_CHA, BCK); | |
// motor.dcMotorRun(MOTOR_CHB, FWD); | |
// delay(1000); | |
// // stop 2 motors | |
// Serial.println("stop"); | |
// motor.dcMotorStop(MOTOR_CHA); | |
// motor.dcMotorStop(MOTOR_CHB); | |
// delay(1000); | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment