Low voltage motor driver - ramp and pot code

Hi,
I could use some help.

  1. I am struggling to code correctly, ramping the motors up and down.

  2. I would like to connect a pot and set the variable speed by the pot.

I am using a Arduno Mega 2560 r3 and the motors work fine at a set speed. I want to vary the speed, first ramping them up and down, and then hooking up a pot and setting the speed by turning the pot.

I can ramp the motors when using a “for” statement with the 4 byte commands inside it. However I can’t get it to work with the “for” statement only on the speed byte command. Finally, how would I modify the code to accept a pot value? Would placing an “analogRead” command for the 4th byte work?

Thank you,
Scott

Here is my code so far:

nt reset = 9; // reset pin, low to reset.  HI to enable
int speed = 0;

void setup()
{ Serial1.begin(9600);
  
 
  pinMode(reset, OUTPUT);
  digitalWrite(reset,LOW);
  delay(5);  
  digitalWrite(reset,HIGH);
  delay(5);
  
}

void loop()
{
  for(int i = 0x00; i <0x7F; i++) //ramp up using the for statement here.  It works.
    {
    Serial1.write(0x80);
    Serial1.write(0x00);
    Serial1.write(0x05);
    Serial1.write(i);
       delay(100);
    }
    
   Serial1.write(0x80);  //ramping down
   Serial1.write(0x00);
   Serial1.write(0x05);
   for(int i=0x7F; i > 0; i--)  // ramp down using the for statement here. Only speed byte  changes. this does not work!
   {
     Serial1.write(i);
     delay(100);
   }
  
}

Hello, Scott.

You need to send the full 4-byte command every time in order to change the speed. You should move the 3 calls to Serial1.write() into the for loop:

   for(int i=0x7F; i > 0; i--)  
   {
     Serial1.write(0x80);  
     Serial1.write(0x00);
     Serial1.write(0x05);
     Serial1.write(i);
     delay(100);
   }

To use a potentiometer to control the speed, I recommend reading the analog voltages and scaling them to your desired speed.

- Jeremy