I'm using a nano with a cytron md13s driver my code works with pwm pin 3 and dir pin 4. I want to drive a 2nd motor with a 2nd driver I but can't get this to run on any other pwm pins. This is driving a wind shield wiper diy servo, with a pot giving feedback. A 2nd pot is standing in for testing as gpsspeed.
Here's my code; thanks in advance.
#include "CytronMotorDriver.h"
// Configure the motor driver.
CytronMD speedmotor(PWM_DIR, 3, 4); // PWM = Pin 3, DIR = Pin 4.
float Setpoint1, Input1, Output1, Output1a;
float speed = 0; // variable from recorded path (will be used)
float gpsSpeed = 0; // passed from AOG via png
float speedmotorFeedback = 0; // current speedmotor position
void setup() {
pinMode(A0, INPUT); // pot input (gpsSpeed)
pinMode(A1, INPUT); // pot input (feedback)
Serial.begin(38400);
}
void loop() {
gpsSpeed = analogRead(A0); // change to brakemotorFeedback
speedmotorFeedback = analogRead(A1);
Setpoint1 = gpsSpeed * 0.2493;
Input1 = speedmotorFeedback * 0.2493;
Serial.print("Setpoint1 ");
Serial.print(Setpoint1);
Serial.print(" , ");
Serial.print("Input1 ");
Serial.print(Input1);
Serial.print(" , ");
if (Output1 < 0) {
Output1a = abs(Output1);
Output1 = (Setpoint1 - Input1);
Serial.print("Output1 <");
Serial.println(Output1);
}
else {
Output1 = (Setpoint1 - Input1);
Serial.print("Output1 >");
Serial.println(Output1);
}
speedmotor.setSpeed(Output1); // move motor
}
Presumably that's the working code. Where is the code with 2 motors and drivers and what exactly does that do? What is the specification of the motor it's driving, particularly current requirements?
Yes this is the working code. I Started this code out on pin 11 pwm and pin 12 dir. it won't turn the motor. I searching I found some code that was using 3 & 4 and it works. There is no since adding the 2nd motor code if I can't use other pins.
The Motor specs are wiper motor from the junk yard and it runs on 12 volt.
The CytronMotorDriver library is quite trivial. All it does is set the DIR pin HIGH/LOW and the PWM pin to 0-255. You should be able to use it on any of the PWM pins on the NANO.
The example is basically dividing the analog input (0-1023) by 4 to get 0-255. Not sure why you have a test for Output1 being negative and to the same thing as if it is not?
All those variables, other than Output1 can be local to loop(). You might want slow down how fast you update the PWM signal, like only a couple of times per second....
I just had a really embarrassing thought. Don't know for sure but I don't think I changed the wires when I changed pins in the code. I'll try it tomarr0w and let you know if I did have one of those dumb s--t attacks.
I was just looking at the test for pos or neg and it is a remnant from earlier testing I didn't remove. It wasn't doing anything.
The gpsspeed pot is a stand in for reading the speed in real life. The feedback pot is hooked to the shaft on the dc motor to give feedback. the error is calculated and sent out to the motor driver. It works really well. (when I wire it up to match the code.
I'm thinking of adding an averaging function on the speed pot which will slow it a little and also take out some flutter I'm getting and I might need to slow it even further.
Motors work on other pwm pins, I had changed motors and pins at the same time. I had a stray wire on the pot of that motor. Any way all my fault. I do appreciate your help.