hey
I am next to useless at coding... I have just figured out functions so yeah.
anyway I was thinking about making a nano or micro motor shield. the code will include a rotary encoder to get precise movements and also allow me to adjust PWM based on weight etc. I have a basic copy of the code and schematic etc.
I just need an idea on how to change the code to not burn out the motor and slowly increase the PWM to help when heavier weights are on the end. I was thinking about adding more increasing the PWM incrementally until the device moves. I was also thinking about making a travel speed variable so that when the device moves it moves "x" steps per second and when motor meets that speed it retains the info, when the weight lowers it will also check to maintain speed for accuracy....
so any ideas?
I am next to useless at coding... I have just figured out functions so yeah.
anyway I was thinking about making a nano or micro motor shield. the code will include a rotary encoder to get precise movements and also allow me to adjust PWM based on weight etc. I have a basic copy of the code and schematic etc.
I just need an idea on how to change the code to not burn out the motor and slowly increase the PWM to help when heavier weights are on the end. I was thinking about adding more increasing the PWM incrementally until the device moves. I was also thinking about making a travel speed variable so that when the device moves it moves "x" steps per second and when motor meets that speed it retains the info, when the weight lowers it will also check to maintain speed for accuracy....
so any ideas?
Code:
#define InA1 10 // INA motor pin
#define InB1 11 // INB motor pin
#define PWM1 6 // PWM motor pin
#define encodPinA1 3 // encoder A pin
#define encodPinB1 8 // encoder B pin
#define LOOPTIME 100 // PID loop time
#define FORWARD 1 // direction of rotation
#define BACKWARD 2 // direction of rotation
unsigned long lastMilli = 0; // loop timing
unsigned long lastMilliPrint = 0; // loop timing
long count = 0; // rotation counter
long countInit;
long tickNumber = 0;
boolean run = false; // motor moves
void setup() {
pinMode(InA1, OUTPUT);
pinMode(InB1, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(encodPinA1, INPUT);
pinMode(encodPinB1, INPUT);
digitalWrite(encodPinA1, HIGH); // turn on pullup resistor
digitalWrite(encodPinB1, HIGH);
attachInterrupt(1, rencoder, FALLING);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
moveMotor(FORWARD, 50, 464*2); // direction, PWM, ticks number
delay(3000);
moveMotor(BACKWARD, 50, 464*2); // 464=360°
delay(3000);
}
void moveMotor(int direction, int PWM_val, long tick) {
countInit = count; // abs(count)
tickNumber = tick;
Serial.print(tickNumber);
delay(300);
Serial.print(countInit);
delay(300);
if(direction==FORWARD) motorForward(PWM_val);
else if(direction==BACKWARD) motorBackward(PWM_val);
}
void rencoder() { // pulse and direction, direct port reading to save cycles
if (PINB & 0b00000001) count++; // if(digitalRead(encodPinB1)==HIGH) count_r ++;
else count--; // if (digitalRead(encodPinB1)==LOW) count_r --;
if(run)
if((abs(abs(count)-abs(countInit))) >= tickNumber) motorBrake();
}
void motorForward(int PWM_val) {
analogWrite(PWM1, PWM_val);
digitalWrite(InA1, LOW);
digitalWrite(InB1, HIGH);
run = true;
}
void motorBackward(int PWM_val) {
analogWrite(PWM1, PWM_val);
digitalWrite(InA1, HIGH);
digitalWrite(InB1, LOW);
run = true;
}
void motorBrake() {
analogWrite(PWM1, 0);
digitalWrite(InA1, HIGH);
digitalWrite(InB1, HIGH);
run = false;
}
Attachments
-
111.4 KB Views: 10
-
330.3 KB Views: 9