make motor rev up gradually

Hi guys, below is the code for my bluetooth rc car. code works just fine. the only change i need to make is for the motor to gradually speed up to full speed instead of instant full speed when i hit forward button. the motor speed is set by velocity(which is set to full speed)

////beginning of code

char dataIn = 'S';
int pinLeft = 10;
int pinRight = 11;
int pinEnableLeftRight = 9;
int pinForward = 5;
int pinBack = 6;
int pinSpeedForwardBack = 3;
int pinfrontLights = 8;
int pinbackLights = 7;
char determinant;
char det;
int velocity = 255;

void setup()
{
Serial.begin(9600);
pinMode(pinLeft, OUTPUT);
pinMode(pinRight, OUTPUT);
pinMode(pinForward, OUTPUT);
pinMode(pinBack, OUTPUT);
pinMode(pinEnableLeftRight, OUTPUT);
pinMode(pinSpeedForwardBack, OUTPUT);
pinMode(pinfrontLights, OUTPUT);
pinMode(pinbackLights, OUTPUT);
}

void loop()
{
det = check();
while (det == 'F') //forward
{
digitalWrite(pinEnableLeftRight, HIGH);
analogWrite(pinSpeedForwardBack, velocity);
digitalWrite(pinLeft,HIGH);
digitalWrite(pinRight,HIGH);
digitalWrite(pinForward,LOW);
digitalWrite(pinBack,HIGH);
det = check();
}
while (det == 'B') //backward
{
digitalWrite(pinEnableLeftRight, HIGH);
analogWrite(pinSpeedForwardBack, velocity);
digitalWrite(pinLeft,HIGH);
digitalWrite(pinRight,HIGH);
digitalWrite(pinForward,HIGH);
digitalWrite(pinBack,LOW);
det = check();
}

while (det == 'L') //left
{
digitalWrite(pinEnableLeftRight, HIGH);
analogWrite(pinSpeedForwardBack, velocity);
digitalWrite(pinLeft,LOW);
digitalWrite(pinRight,HIGH);
digitalWrite(pinForward,HIGH);
digitalWrite(pinBack,HIGH);
det = check();
}
while (det == 'R') //right
{
digitalWrite(pinEnableLeftRight, HIGH);
analogWrite(pinSpeedForwardBack, velocity);
digitalWrite(pinLeft,HIGH);
digitalWrite(pinRight,LOW);
digitalWrite(pinForward,HIGH);
digitalWrite(pinBack,HIGH);
det = check();
}

while (det == 'I')
{
digitalWrite(pinEnableLeftRight,HIGH);
analogWrite(pinSpeedForwardBack, velocity);
digitalWrite(pinLeft,HIGH);
digitalWrite(pinRight,LOW);
digitalWrite(pinForward,LOW);
digitalWrite(pinBack,HIGH);
det = check();
}
while (det == 'J')
{
digitalWrite(pinEnableLeftRight, HIGH);
analogWrite(pinSpeedForwardBack, velocity);
digitalWrite(pinLeft,HIGH);
digitalWrite(pinRight,LOW);
digitalWrite(pinForward,HIGH);
digitalWrite(pinBack,LOW);
det = check();
}
while (det == 'G')
{
digitalWrite(pinEnableLeftRight, HIGH);
analogWrite(pinSpeedForwardBack, velocity);
digitalWrite(pinLeft,LOW);
digitalWrite(pinRight,HIGH);
digitalWrite(pinForward,LOW);
digitalWrite(pinBack,HIGH);
det = check();
}
while (det == 'H')
{
digitalWrite(pinEnableLeftRight, HIGH);
analogWrite(pinSpeedForwardBack, velocity);
digitalWrite(pinLeft,LOW);
digitalWrite(pinRight,HIGH);
digitalWrite(pinForward,HIGH);
digitalWrite(pinBack,LOW);
det = check();
}
while (det == 'S')
{
digitalWrite(pinEnableLeftRight, HIGH);
analogWrite(pinSpeedForwardBack, velocity);
digitalWrite(pinLeft,HIGH);
digitalWrite(pinRight,HIGH);
digitalWrite(pinForward,HIGH);
digitalWrite(pinBack,HIGH);
det = check();
}
while (det == 'U')
{
digitalWrite(pinfrontLights, HIGH);
det = check();
}
while (det == 'u')
{
digitalWrite(pinfrontLights, LOW);
det = check();
}
while (det == 'W')
{
digitalWrite(pinbackLights, HIGH);
det = check();
}
while (det == 'w')
{
digitalWrite(pinbackLights, LOW);
det = check();
}
}

int check()
{
if (Serial.available() > 0) //provjerava dostupnost serijske komunikacije.
{
dataIn = Serial.read(); //provijerava dolazni podatak i sprema u'dataIn'.
if (dataIn == 'F')
{
determinant = 'F';
}
else if (dataIn == 'B')
{
determinant = 'B';
}
else if (dataIn == 'L')
{
determinant = 'L';
}
else if (dataIn == 'R')
{
determinant = 'R';
}
else if (dataIn == 'I')
{
determinant = 'I';
}
else if (dataIn == 'J')
{
determinant = 'J';
}
else if (dataIn == 'G')
{
determinant = 'G';
}
else if (dataIn == 'H')
{
determinant = 'H';
}
else if (dataIn == 'S')
{
determinant = 'S';
}

else if (dataIn == 'U')
{
determinant = 'U';
}
else if (dataIn == 'u')
{
determinant = 'u';
}
else if (dataIn == 'W')
{
determinant = 'W';
}

else if (dataIn == 'w')
{
determinant = 'w';
}
}
return determinant;
}

////end of code

I found this code somewhere on the net, which looks very promising. but could anyone show me how to integrate this into the code above. maybe put in with the while loop above. thanks in advance.

void loop() {
for(int motorValue = 0 ; motorValue <= 255; motorValue +=5){
analogWrite(pinSpeedForwardBack, motorValue);
delay(100);
}

Put your analogWrite inside a for loop, and increment the value that you write with the for's increment.

sorry for the dumb question, so where do i put the for loop?. i spent 2hrs this morning, just cant make it work.

The example on this page is exactly what you want.

I'd put it here:

digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, velocity);  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
          digitalWrite(pinLeft,HIGH);
          digitalWrite(pinRight,HIGH);

(ie, wrap the analogWrite in the for... and of course do it a number of times, wherever you need it)

PLEASE use code tags :stuck_out_tongue_closed_eyes:

A more general approach, and one that works in the face of frequent changes in
desired speed is to have a target_speed and a separate actual_speed variable.

The target_speed is controlled by whatever code is responsible for controlling the
motor, but in loop you have code that limits the rate at which actual_speed changes
(it always moves towards target_speed but at a limited slew-rate.

Something like:

int target_speed = 0 ;
int actual_speed = 0 ;

unsigned long last_update = 0L ;

void loop ()
{
  if (millis () - last_update >= 10)  // run every 10ms
  {
    last_update += 10 ;
    if (target_speed > actual_speed)
      actual_speed ++ ;
    else if (target_speed < actual_speed)
      actual_speed -- ;
    analogWrite (pin, actual_speed) ;
  }
  .. other stuff, such as reading control inputs and changing target_speed
  ...
  ...
}

In this example code the actual speed can change at upto 100 steps
per second. Note use of BlinkWithoutDelay style, so delay() never holds
up the sketch.

I try the IF statement, still no luck. now even the steering is dead :frowning: Did I put the if statement in correctly?

char dataIn = 'S';        
int pinLeft = 10;     
int pinRight = 11;       
int pinEnableLeftRight = 9;
int pinForward = 5;
int pinBack = 6;
int pinSpeedForwardBack = 3;
int pinfrontLights = 8;
int pinbackLights = 7;
char determinant;         
char det;                 
int target_speed = 0;
int actual_speed = 0;
unsigned long last_update = 0L;

void setup()  
{        
Serial.begin(9600);  
pinMode(pinLeft, OUTPUT);
pinMode(pinRight, OUTPUT);
pinMode(pinForward, OUTPUT);
pinMode(pinBack, OUTPUT);
pinMode(pinEnableLeftRight, OUTPUT);
pinMode(pinSpeedForwardBack, OUTPUT);
pinMode(pinfrontLights, OUTPUT);
pinMode(pinbackLights, OUTPUT);
}

void loop()
{  
   if (millis () - last_update >= 3000)  // run every 10ms
  {
    last_update += 3000 ;
    if (target_speed > actual_speed)
      actual_speed ++ ;
    else if (target_speed < actual_speed)
      actual_speed -- ;
    analogWrite (pinSpeedForwardBack, actual_speed) ;
  }
    det = check();
      while (det == 'F')   //forward
      {      
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, target_speed); 
          digitalWrite(pinLeft,HIGH);
          digitalWrite(pinRight,HIGH);   
          digitalWrite(pinForward,LOW);
          digitalWrite(pinBack,HIGH);         
          det = check();           
      }   
      while (det == 'B')  //backward
      {     
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, target_speed);
          digitalWrite(pinLeft,HIGH);
          digitalWrite(pinRight,HIGH);   
          digitalWrite(pinForward,HIGH);
          digitalWrite(pinBack,LOW);         
          det = check();           
      }  

      while (det == 'L')   //left
      {      
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, target_speed);
          digitalWrite(pinLeft,LOW);
          digitalWrite(pinRight,HIGH);   
          digitalWrite(pinForward,HIGH);
          digitalWrite(pinBack,HIGH);        
          det = check();           
      }   
      while (det == 'R')   //right
      {     
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, target_speed); 
          digitalWrite(pinLeft,HIGH);
          digitalWrite(pinRight,LOW);   
          digitalWrite(pinForward,HIGH);
          digitalWrite(pinBack,HIGH);         
          det = check();           
      } 
      
      while (det == 'I')  
      {      
          digitalWrite(pinEnableLeftRight,HIGH);  
          analogWrite(pinSpeedForwardBack, target_speed);
          digitalWrite(pinLeft,HIGH);
          digitalWrite(pinRight,LOW);   
          digitalWrite(pinForward,LOW);
          digitalWrite(pinBack,HIGH);                 
          det = check();           
      }   
      while (det == 'J')  
      {       
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, target_speed);
          digitalWrite(pinLeft,HIGH);
          digitalWrite(pinRight,LOW);   
          digitalWrite(pinForward,HIGH);
          digitalWrite(pinBack,LOW);         
          det = check();           
      }           
      while (det == 'G')  
      {  
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, target_speed);
          digitalWrite(pinLeft,LOW);
          digitalWrite(pinRight,HIGH);   
          digitalWrite(pinForward,LOW);
          digitalWrite(pinBack,HIGH);           
          det = check();           
      }     
      while (det == 'H')  
      { 
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, target_speed);
          digitalWrite(pinLeft,LOW);
          digitalWrite(pinRight,HIGH);   
          digitalWrite(pinForward,HIGH);
          digitalWrite(pinBack,LOW);           
          det = check();                                               
      }    
      while (det == 'S')  
      { 
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, target_speed);
          digitalWrite(pinLeft,HIGH);
          digitalWrite(pinRight,HIGH);   
          digitalWrite(pinForward,HIGH);
          digitalWrite(pinBack,HIGH);        
          det = check();  
      }
      while (det == 'U')  
      { 
          digitalWrite(pinfrontLights, HIGH);           
          det = check();  
      }
      while (det == 'u')   
      { 
          digitalWrite(pinfrontLights, LOW);           
          det = check();  
      }
      while (det == 'W')   
      { 
          digitalWrite(pinbackLights, HIGH);           
          det = check();  
      }
      while (det == 'w')   
      { 
          digitalWrite(pinbackLights, LOW); 
          det = check();  
      }
}

int check()
{
  if (Serial.available() > 0)    //provjerava dostupnost serijske komunikacije. 
  {    
    dataIn = Serial.read();  //provijerava dolazni podatak i sprema u'dataIn'.
        if (dataIn == 'F') 
        {      
          determinant = 'F';
        }   
        else if (dataIn == 'B') 
        {  
          determinant = 'B';  
        }
        else if (dataIn == 'L')   
        {  
          determinant = 'L'; 
        }
        else if (dataIn == 'R')   
        {  
          determinant = 'R';
        }  
        else if (dataIn == 'I')   
        {  
          determinant = 'I';  
        }   
        else if (dataIn == 'J')   
        {   
          determinant = 'J'; 
        }           
        else if (dataIn == 'G')  
        { 
          determinant = 'G';  
        }     
        else if (dataIn == 'H')   
        { 
          determinant = 'H';  
        }    
        else if (dataIn == 'S')  
        {
          determinant = 'S';
        } 
      
        else if (dataIn == 'U')  
        {
          determinant = 'U'; 
        }
        else if (dataIn == 'u')  
        {
          determinant = 'u'; 
        }
        else if (dataIn == 'W')  
        {
          determinant = 'W'; 
        }
        
        else if (dataIn == 'w')  
        {
          determinant = 'w'; 
        }
  } 
return determinant; 
}

What part of "The target_speed is controlled by whatever code is responsible for controlling the
motor," did you find hard to understand?

The rest of the code only updates target_speed, no other calls to analogWrite, that's
just bypassing the entire mechanism!!!

Thanks for your reply MarkT, but I'm still having trouble implement your IF statement into the existing code. Could you just fix the code for me? Many thanks.

No because its not doing anything with the speed at all.

You need to update target_speed whenever you want the speed
to change, and not at any other time, and not call analogWrite()
except that one place in the loop, and you need the delay to be
more like 10ms than 3000ms otherwise it will be far far too slow
to respond.

Currently you don't appear to ever change the speed. You need to.
The mechanism will have to be generalised if you are changing direction
as well, so that when the speed changes sign the relevant direction
output pins are updated.

Basically all the output to the motor is done in the update section which
is controlled by the target_speed variable from elsewhere in the code.

Think of a ship's bridge and engine room - the speed telegraph in the
bridge is the target_speed variable - set by the commander on the bridge,
read by the engineers in the engine room who then adjust the engine throttle
and reversing gear.

thanks for your explanation, but how do I update target_speed?

How do you update any variable?