Your program is "linear," meaning, one event/device completes before another event/device starts. This becomes "blocking" to sensors and motors, as every device must wait for all the other devices to complete before beginning with the first device again.
Your project needs to (1) line sense, (2) object detect, (3) move a servo, (4) move motors, but they all do not need the same amount of time to complete their task. The line sensing might need to happen every 100ms. The object detection might only need to happen every 500ms. The servo movement only happens after object detection, and motor drive happens continuously, reacting to line sensing and object detection, and probably to move toward a destination.
diagram.json for wokwi
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": -22.6,
"left": -86.4,
"attrs": { "color": "green", "xray": "1" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": -116.4,
"left": 93,
"rotate": 90,
"attrs": { "color": "green" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": -97.2,
"left": 93,
"rotate": 90,
"attrs": { "color": "white" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": -78,
"left": 93,
"rotate": 90,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led4",
"top": -116.4,
"left": 20.6,
"rotate": 270,
"attrs": { "color": "green", "flip": "1" }
},
{
"type": "wokwi-led",
"id": "led5",
"top": -97.2,
"left": 20.6,
"rotate": 270,
"attrs": { "color": "white", "flip": "1" }
},
{
"type": "wokwi-led",
"id": "led6",
"top": -78,
"left": 20.6,
"rotate": 270,
"attrs": { "color": "red", "flip": "1" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": -70.6,
"left": -86.4,
"attrs": { "color": "green", "xray": "1" }
},
{
"type": "wokwi-pushbutton",
"id": "btn3",
"top": -118.6,
"left": -86.4,
"attrs": { "color": "green", "xray": "1" }
},
{
"type": "wokwi-hc-sr04",
"id": "ultrasonic1",
"top": -84.9,
"left": 139.9,
"attrs": { "distance": "148" }
},
{ "type": "wokwi-servo", "id": "servo1", "top": -184.4, "left": 144, "attrs": {} }
],
"connections": [
[ "nano:GND.2", "led3:C", "black", [ "v-43.2", "h-38.9", "v-19.2" ] ],
[ "led3:C", "led2:C", "green", [ "h-9.6", "v-18.8" ] ],
[ "led2:C", "led1:C", "green", [ "h-9.6", "v-18.8" ] ],
[ "led1:C", "led4:C", "green", [ "h0" ] ],
[ "led4:C", "led5:C", "green", [ "h9.6", "v0.4" ] ],
[ "led5:C", "led6:C", "green", [ "h9.6", "v19.6" ] ],
[ "nano:5", "led5:A", "green", [ "v-33.6", "h-10.1", "v-38.4" ] ],
[ "nano:6", "led2:A", "green", [ "v-72", "h-0.5" ] ],
[ "nano:7", "led4:A", "green", [ "v-33.6", "h9.1", "v-57.6" ] ],
[ "nano:8", "led6:A", "green", [ "v-33.6", "h18.7", "v-19.2" ] ],
[ "nano:GND.2", "btn2:2.r", "black", [ "v0" ] ],
[ "btn2:2.r", "btn1:2.r", "green", [ "h9.8", "v19.4" ] ],
[ "btn2:2.r", "btn3:2.r", "green", [ "h9.8", "v-47.8" ] ],
[ "nano:10", "btn3:1.r", "green", [ "v-33.6", "h-38.9", "v-76.8" ] ],
[ "nano:3", "btn2:1.r", "green", [ "v-33.6", "h-106.1", "v-28.8" ] ],
[ "nano:2", "btn1:1.r", "green", [ "v-33.6", "h-115.7", "v19.2" ] ],
[ "ultrasonic1:VCC", "nano:5V.2", "red", [ "v0" ] ],
[ "ultrasonic1:TRIG", "nano:A5", "green", [ "v67.2", "h-125.2" ] ],
[ "ultrasonic1:ECHO", "nano:A4", "green", [ "v76.8", "h-154.4" ] ],
[ "ultrasonic1:GND", "nano:GND.3", "black", [ "v0" ] ],
[ "nano:9", "led3:A", "green", [ "v-33.6", "h28.3", "v-19.2" ] ],
[ "nano:11", "led1:A", "green", [ "v-33.6", "h47.5", "v-57.6" ] ],
[ "nano:4", "servo1:PWM", "green", [ "v-33.6", "h37.9", "v-76.8" ] ],
[ "nano:GND.3", "servo1:GND", "black", [ "v-4.8", "h-19.7", "v-153.6" ] ],
[ "nano:5V.2", "servo1:V+", "red", [ "v4.8", "h-19.7", "v-163.2" ] ]
],
"dependencies": {}
}