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);
}