In need of help for fixing up program.

My Arduino-bot is equipped with one eye-like ultrasonic sensor, two infrared sensors, and 2 motors.
By the by, this is an assignment that is due tom, and I would really appreciate it if anyone could tweak this program. It’s meant to find a large wooden dowel on top of a circular wooden board.

Betsy.ino (1.54 KB)

//The Plan: 1)Use the front sensors to move if there is nothing
//there, continue moving. 2)If about to run off into the sunset,
//lower senors will detect the black tape and go backwards and
//make a 200 degree turn. Continue running forward. 3)Repeat

#include <NewPing.h>

#define TRIGGER_PIN  11
#define ECHO_PIN     7
#define MAX_DISTANCE 45

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

long randNumber;
//motorA
int dir1PinA = 2;
int dir2PinA = 3;
int speedPinA = 9;
//motorB
int dir1PinB = 4;
int dir2PinB = 5;
int speedPinB = 10;
int speed = 120;
unsigned long time;
int cm;

void setup() {
  Serial.begin(115200);
  analogWrite (speedPinA, speed);
  analogWrite (speedPinB, speed);

  pinMode(dir1PinA, OUTPUT);
  pinMode(dir2PinA, OUTPUT);
  pinMode(speedPinA, OUTPUT);
  pinMode(dir1PinB, OUTPUT);
  pinMode(dir2PinB, OUTPUT);
  pinMode(speedPinB, OUTPUT);

}
void loop() {
  delay(50);
  unsigned int uS = sonar.ping();
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);
  Serial.println("cm");
{
  digitalWrite (dir1PinA, HIGH);//Foward
    digitalWrite (dir2PinA, LOW);
    digitalWrite (dir1PinB, LOW);
    digitalWrite (dir2PinB, HIGH);
    delay (350);
    digitalWrite (dir1PinA, LOW); //Turn Around
    digitalWrite (dir2PinA, HIGH);
    digitalWrite (dir1PinB, LOW);
    digitalWrite (dir2PinB, HIGH);

  }
  if ((uS / US_ROUNDTRIP_CM > 40) && (uS / US_ROUNDTRIP_CM == 0))
  {
    digitalWrite (dir1PinA, HIGH); //Foward
    digitalWrite (dir2PinA, LOW);
    digitalWrite (dir1PinB, LOW);
    digitalWrite (dir2PinB, HIGH);

  }
}

Was there any problems or improvements needed?

pulsefire: Was there any problems or improvements needed?

I've no idea - I just wanted everyone to be able to see the code.

I think he was just putting it into code tags because you didn’t read the forum rules and didn’t post your sketch in code tags. Lots of people (particularly, anyone browsing forums on phone while bored) don’t or can’t view attached files.

Have you tried your code?
If not, why not?

Are there any issues?
If yes, please describe them.

Yes, I have tried to apply my code, but all it did was move forward continuously. I tried checking to see if the ultrasonic sonar was working, and yes it could read to 45 cm. The program was supposed to run like this: 1) If it locates anything within proximity, move towards it. 2) If nothing is located it will move forward for 3 seconds (approximately 5cm) and turn clockwise for 3 seconds.

if ((uS / US_ROUNDTRIP_CM > 40) && (uS / US_ROUNDTRIP_CM == 0)) If the sensors notice anything that is 40cm in front of it will move forward, also if anything is directly in front of it it will hit it instead of stopping.

if ((uS / US_ROUNDTRIP_CM > 40) && (uS / US_ROUNDTRIP_CM == 0))

Can you describe the circumstances where this would be true?