Help on my code!

arduinohelp:
Hi J-M-L, I think the goal of doing a digitalRead to see if the trigPin (sensor) is HIGH is basically when the sensor in the front of the "robot" hits, it should change directions and vice versa.

I just started coding in this language so I'm not quite sure where to start when doing this efficiently/correctly.

Thanks!

distance = (duration/2)/29.1;

Should give you the distance from the sensor to the nearest object. If it is below a certain value then stop and move backwards.

The subsequent state of the two digital pins is irrelevant, but seeing these comments

int trigPin = 4; //switch in front connected to pin 4
int echoPin =5; //switch in back connected to pin 5

I wonder if you have an ultrasonic sensor at all.

So, do you have an ultrasonic sensor or just switches ?