testing the motor controller

So I’ve received the motor controller so thought I’d give it a quick (safe) test out of the mower. Basically I hooked it up to an Uno I had lying around to the motor controller (not the L298 shown in the picture, but that is all fritzing seems to have), hooked the motor controller up to a 5V supply (an Uno output) and them measured the output of the controller on one of the analog ports.

 

const int pwmPin = 5;
const int dirPin = 7;
int counter = 0;
int changeCounter = 0;
int currentSpeed = 0;
int targetSpeed = 0;
void setup() {
 Serial.begin(19200);
 pinMode(dirPin, OUTPUT);
 pinMode(pwmPin, OUTPUT);
digitalWrite(dirPin, HIGH);
 analogWrite(pwmPin, 0);
}
void setMotorSpeed(int target)
{
 if (currentSpeed < target)
 {
 currentSpeed += 5;
 analogWrite(pwmPin, currentSpeed);
 }
 else if (currentSpeed > target)
 {
 currentSpeed -= 5;
 analogWrite(pwmPin, currentSpeed);
 }
}
void loop() {
setMotorSpeed(targetSpeed);
// Change the target speed every 100 loops
 changeCounter++;
 if (changeCounter > 100)
 {
 if (targetSpeed == 0)
 {
 targetSpeed = 130;
 }
 else
 {
 targetSpeed = 0;
 }
 changeCounter = 0;
 }
double a0value = 0;
// read the average value over 100ms -ish
 double loopTime = 100;
 for (int i = 0 ; i < loopTime ; i++)
 {
 a0value += analogRead(A0) / loopTime;
 delay (1);
 }
counter++;
 if (counter > 2)
 {
 Serial.print(a0value * 5 / 1023.0);
 Serial.print("v ");
Serial.print(currentSpeed);
 Serial.print(" ");
 Serial.print(targetSpeed);
 Serial.println("");
 counter = 0;
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.