Hey, sorry for the trouble you're having!
Your wiring looks good to me, thanks for the pictures and schematic.
I've found that about 1 in 200 ultrasonics are defective, so it's possible you got a bad one. But let's try a few debugging steps first.
1. to start, try uploading just the ultrasonic code to the robot to make debugging easier:
const int serialPeriod = 250; // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;
const int loopPeriod = 20; // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay = 0;
// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int ultrasonic2EchoPin = 9;
int ultrasonic2Distance;
int ultrasonic2Duration;
void setup()
{
Serial.begin(9600);
// ultrasonic sensor pin configurations
pinMode(ultrasonic2TrigPin, OUTPUT);
pinMode(ultrasonic2EchoPin, INPUT);
}
void loop()
{
debugOutput(); // prints debugging messages to the serial console
if(millis() - timeLoopDelay >= loopPeriod)
{
readUltrasonicSensors(); // read and store the measured distances
timeLoopDelay = millis();
}
}
void readUltrasonicSensors()
{
// ultrasonic 2
digitalWrite(ultrasonic2TrigPin, HIGH);
delayMicroseconds(10); // must keep the trig pin high for at least 10us
digitalWrite(ultrasonic2TrigPin, LOW);
ultrasonic2Duration = pulseIn(ultrasonic2EchoPin, HIGH);
ultrasonic2Distance = (ultrasonic2Duration/2)/29;
}
void debugOutput()
{
if((millis() - timeSerialDelay) > serialPeriod)
{
Serial.print("ultrasonic2Distance: ");
Serial.print(ultrasonic2Distance);
Serial.print("cm: ");
Serial.println();
timeSerialDelay = millis();
}
}
2. run the above program and check the output. If it still doesn't work, continue..
3. do you have a multimeter? If so, check the voltage across the VCC & GND pins on the ultrasonic to verify it's getting 5V.
4. it's hard to verify the trig & echo wires are correct from the picture, so just for kicks try swapping them (after testing, put them back to how you think they should be)
5. sometimes the jumper wires break, so try replacing all of the wires to the ultrasonic (including power, IF you weren't able to check with a multimeter)
6. go back to step 4! (but afterwords jump to step 7, else you'll get stuck in a loop
)
7. this probably should have been like step 1, but try using new batteries..
8. we'll cross this bridge if/when we get here 