Adding delay To the IF loop

Hey to all,

Hope you all doing good.

I am trying to controlling the DC Motor using analogwrite pwm(0 to 255). Below i had attached my project code.

In my case, i am able to control according with pwm. I need even to reduce the speed. Says, my PWM above 65 car moving smoothly as slow (According to my project its seems bit fast- i need to push little when vehicle initial at start) but i need even to reduce my Pwm. Once, if i reduce my PWM below 65, my car stopped completely (even i push initially ) obviously due to Load on my vehicle.

Is there any other solution to over come with this problem ? I thought like to add delay to my vehicle. Below i have a source code

I am using a Arduino Ethernet shield which is connected to Raspberry via Simulink (Rpi UDP). nothink like simply simulink send a boolean command to my arduino (If 1 = 1 My Pwm start)

Below in the code. can you suggest me How to add delay according to my Pwm, i am using a if else loop. If i add delay like this(As the result, its not executing)
So my question is. How to add delay to PWM ? and Is there any other alternate solution without delay so that my car move below 60 pwm?

 if (forward == 1)
    analogWrite(DCMotorPin, DCMotorPower);
delay(1000);
else
analogWrite(DCMotorPin, DCMotorPower);

My source code:

#include <SPI.h> //Load Spi Lib
#include <Ethernet.h> //Load ethernet lib
#include <EthernetUdp.h> //Load UDP lIB
#include <Servo.h> //Load servo lib

/* Pin Configurations */
int servoPin = 7,
    DCMotorPin = 5;
    

int DCMotorPower = 65; 


/* Packet Initial Conditions */
int forward = 0, 
    left = 0, 
    right = 0; 

Servo myservo;
int servoAngle = 90;

/* UDP Configurations */

byte mac[] = { 0xDE, 0xEB, 0xDE, 0xEB, 0xDE, 0xEB };   
IPAddress ip(169, 254, 27, 47); 
unsigned int localPort = 8888;  
byte RaspIP[] = {169, 254, 27, 46}; 
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  
EthernetUDP Udp; 

void setup() {

  Ethernet.begin(mac, ip); //Initialize Ethernet
  Udp.begin(localPort); 	//Initialize UDP
  
  Serial.begin(9600); //creating a serial monitor with bandrate of 9600 // Turn on Serial port
  
  //command for servo
  myservo.attach(servoPin);  //Initialize Servo
  myservo.write(servoAngle);   

  //command for DC Motor
  pinMode(DCMotorPin, OUTPUT);
  
}

void loop() {
  

  // Check for incoming Packets

  int packetSize = Udp.parsePacket();  

  if (packetSize) {
    IPAddress remote = Udp.remoteIP();
    if (remote == RaspIP) {
      Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); 

      forward = packetBuffer[0],
      right = packetBuffer[1],
      left = packetBuffer[2];
    }
  }

    // PWM to control the speed of car 
  if (forward == 1)
    analogWrite(DCMotorPin, DCMotorPower);
else
analogWrite(DCMotorPin, DCMotorPower);


  // Calculate Servo Orientation and write the desired value once
  if (right == 1 && left == 0)
    servoAngle = 60;
  else if (left == 1 && right == 0)
    servoAngle = 120;
  else
    servoAngle = 90;
  myservo.write(servoAngle);

 
  Serial.print("Forward: ");
  Serial.println(forward);
  Serial.print("Right: ");
  Serial.println(right);
  Serial.print("Left: ");
  Serial.println(left);
  Serial.print("Servo Angle: ");
  Serial.println(servoAngle);
  
  Serial.println();
  Serial.println("----");

}

Is there any other alternate solution without delay so that my car move below 60 pwm?

Gear the motor output down some more. I assume that it is already geared down, or is it ?

Adding delay To the IF loop

The if STATEMENT does not loop, so right off the bat, your post does not make sense.

The value that you need to write to the PWM pin to make the car start moving is a physical reality, related to gearing (I'm guessing none), weight, friction, tire design, etc. Adding delay()s anywhere will not change that.

Adding delay() anywhere will not affect the speed of the vehicle, since speed is controlled solely by the analogWrite() value.

UKHeliBob: Gear the motor output down some more. I assume that it is already geared down, or is it ?

Its an RC CAR. Its already gear downed (the motor) with Pinion and spur to differential and then to wheel.

PaulS: The if STATEMENT does not loop, so right off the bat, your post does not make sense.

The value that you need to write to the PWM pin to make the car start moving is a physical reality, related to gearing (I'm guessing none), weight, friction, tire design, etc. Adding delay()s anywhere will not change that.

Adding delay() anywhere will not affect the speed of the vehicle, since speed is controlled solely by the analogWrite() value.

Adding delay, so that car can to move like tic-tac. SO my aim of the project to reduce the speed of the car as i said below 65. Once if i use delay car can move slower

You're missing how analogWrite works. It will continuously send a signal, even during delay.

sterretje:
You’re missing how analogWrite works. It will continuously send a signal, even during delay.

Thanks for the reply. Yes You are true. Is there any other solution. without delay to move the car as tic-tac

Aimtohigh: Thanks for the reply. Yes You are true. Is there any other solution. without delay to move the car as tic-tac

Where is the motor getting its power from ?

Something doesn't look quite right to me:

 if (forward == 1)
    analogWrite(DCMotorPin, DCMotorPower);
else
analogWrite(DCMotorPin, DCMotorPower);
  if (right == 1 && left == 0)
    servoAngle = 60;
  else if (left == 1 && right == 0)
    servoAngle = 120;
  else
    servoAngle = 90;
  myservo.write(servoAngle);

UKHeliBob: Where is the motor getting its power from ?

From Simulink and raspberry pi support package via UDP protocol (sending a boolean command to arduino as '1')

How much current are you able to deliver in each UDP packet? 1A? 2A? 500 mA?