Prueba esta versión con rampa
#define STEP_RAMPA 10UL // 10 mseg UL es unsigned long
#define BRAKEVCC 0
#define CW 1
#define CCW 2
#define BRAKEGND 3
#define CS_THRESHOLD 100
/* VNH2SP30 pin definitions
xxx[0] controls '1' outputs
xxx[1] controls '2' outputs */
const byte inApin[2] = {7, 4}; // INA: Clockwise input
const byte inBpin[2] = {8, 9}; // INB: Counter-clockwise input
const byte pwmpin[2] = {5, 6}; // PWM input
const byte cspin[2] = {2, 3}; // CS: Current sense ANALOG input
byte enpin[2] = {0, 1}; // EN: Status of switches output (Analog pin)
const byte statpin = 13;
byte estado = 0, estadoAnt = 10;
int sentido = CW, sentidoAnt = 0;
int rampa = 0, rampaAnt = -1;
unsigned long start, start_step;
void setup()
{
Serial.begin(9600);
delay(3000);
Serial.println("Comanzando a mover el cabrestante.");
pinMode(statpin, OUTPUT);
// Initialize digital pins as outputs
for (int i=0; i<2; i++)
{
pinMode(inApin[i], OUTPUT);
pinMode(inBpin[i], OUTPUT);
pinMode(pwmpin[i], OUTPUT);
}
// Initialize braked
for (int i=0; i<2; i++)
{
digitalWrite(inApin[i], LOW);
digitalWrite(inBpin[i], LOW);
}
// motorGo(0, CW, 1023);
// motorGo(1, CCW, 1023);
}
void loop() {
if (millis() - start_step > STEP_RAMPA) {
if (rampa != rampaAnt || estado != estadoAnt || sentido != sentidoAnt) {
print_status();
}
rampaAnt = rampa;
estadoAnt = estado;
sentidoAnt = sentido;
if (sentido == CW) {
rampa++;
if (rampa > 255)
rampa = 255;
}
else {
rampa--;
if (rampa < 0)
rampa = 0;
}
start_step = millis();
}
switch (estado) {
case 0: motorGo(0, CW, 255);
motorGo(1, CCW, rampa);
if (rampa == 255) {// termino el estado
start = millis();
estado = 1;
}
break;
case 1: if (millis() - start > 10000UL) {
Serial.println("Superamos 10 seg");
estado = 2;
sentido = CCW;
print_status();
start = millis();
}
break;
case 2: motorGo(0, CCW, 255);
motorGo(1, CW, rampa);
if (rampa == 0) {// termino el estado
estado = 3;
start = millis();
}
break;
case 3: if (millis() - start > 6000UL) {
Serial.println("Superamos 6 seg");
print_status();
estado = 4;
start = millis();
}
break;
case 4: Serial.println("Motor parado");
motorGo(0, CCW, 0); // motor parado.
motorGo(1, CW, 0);
estado = 5;
break;
case 5: break;
}
if ((analogRead(cspin[0]) < CS_THRESHOLD) && (analogRead(cspin[1]) < CS_THRESHOLD))
digitalWrite(statpin, HIGH);
}
void motorOff(int motor)
{
// Initialize braked
for (int i=0; i<2; i++)
{
digitalWrite(inApin[i], LOW);
digitalWrite(inBpin[i], LOW);
}
analogWrite(pwmpin[motor], 0);
}
/* motorGo() will set a motor going in a specific direction
the motor will continue going in that direction, at that speed
until told to do otherwise.
motor: this should be either 0 or 1, will selet which of the two
motors to be controlled
direct: Should be between 0 and 3, with the following result
0: Brake to VCC
1: Clockwise
2: CounterClockwise
3: Brake to GND
pwm: should be a value between ? and 1023, higher the number, the faster
it'll go
*/
void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)
{
if (motor <= 1) {
if (direct <=4) {
// Set inA[motor]
if (direct <=1)
digitalWrite(inApin[motor], HIGH);
else
digitalWrite(inApin[motor], LOW);
// Set inB[motor]
if ((direct==0)||(direct==2))
digitalWrite(inBpin[motor], HIGH);
else
digitalWrite(inBpin[motor], LOW);
analogWrite(pwmpin[motor], pwm);
}
}
}
void print_status() {
char buffer[30];
sprintf(buffer, "%03d Sentido = %3s Estado = %d", rampa, (2-sentido)?"CW":"CCW", estado);
Serial.println(buffer);
}
En el monitor serie veras el avance del código.
El paso es de 10 msegu para poder llegar a 2550 mseg cuando casi complete una vuelt.
Lo mismo la desaceleracion.