HC-SR05 doesn't work well on robot

Hello. I am trying to build a robot that has a lot of things on it. It has an I2C lcd display, an ultrasonic sensor, light sensor etc. But for some reason, when I connect the ultrasonic sensor to the robot power supply(which is 12 V), the distance that is display on the i2c lcd display is not correct. For example, if the real distance is 23 cm, the distance shown will be 17 cm. But when I try the same program, but i connect the arduino with the pc, the distance shown is much better. Can the robot power supply be the problem, or am i just crazy?

Here is my code for the robot(the code is not yet finished):

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,16,2);

#include <IRremote.h>
int RecPin = 10;
IRrecv irrecv(RecPin);
decode_results results;

int ENA = 6;
int ENB = 5;
int in1 = 8;
int in2 = 7;
int in3 = 4;
int in4 = 2;

int speedA1 = 186;
int speedA2 = 197;
int speedA3 = 215;

int speedB1 = 198;
int speedB2 = 235;
int speedB3 = 255;

int pot = A0;
int potValue;

int button = 1;
int buttonValue = 0;
int buttonHits = 1;

int rightSwitchSide = 9;
int rightSwitchSideValue;

int echo = 12;
int trig = 11;

long duration;
int distance;

void setup() {
  pinMode(button, INPUT);
  pinMode(rightSwitchSide, INPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(pot, INPUT);
  pinMode(echo, INPUT);
  pinMode(trig, OUTPUT);
  
  irrecv.enableIRIn();
  irrecv.blink13(true);


  lcd.init();
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("STINGRAY 1");
  delay(2000);
  lcd.setCursor(0,1);
  lcd.print("MADE BY LOVRO J.");
  delay(3000);
  lcd.clear();
}


void loop() {
  buttonValue = digitalRead(button);
  rightSwitchSideValue = digitalRead(rightSwitchSide);
  potValue = analogRead(pot);
  lcds();
  IRcommunication();
  ultrasonic();
 
}


void lcds(){
  if(rightSwitchSideValue == HIGH){
    lcd.init();
    lcd.backlight();
    if(buttonValue == HIGH){
      buttonHits = buttonHits + 1;
      delay(20);
    }
    if(buttonHits == 1){
      lcd.setCursor(1,0);
      lcd.print("Distance away:");
      lcd.setCursor(5,1);
      lcd.print(distance);
      lcd.print(" cm"); 

      if(distance > 450){
      lcd.clear();
      lcd.setCursor(1,0);
      lcd.print("Distance away");
      lcd.setCursor(5,1);
      lcd.print("-------");
      
        
      }


      
    }
    else if(buttonHits == 2){
      lcd.setCursor(1,0);
      lcd.print("Friends");
    }
    else if(buttonHits == 3){
      lcd.setCursor(1,0);
      lcd.print("Mates");
      
    }
    else if(buttonHits > 3){
      buttonHits = 1;
    }
  }
  else{
    lcd.noDisplay();
    lcd.noBacklight();
  }
}



void IRcommunication(){
  if(irrecv.decode(&results)){
    
    switch(results.value){
      case 0xFE30CF: 
      digitalWrite(in1, 1);
      digitalWrite(in2, 0);
      digitalWrite(in3, 1);
      digitalWrite(in4, 0);
      break;
      
      case 0xFEA18A6B: 
      digitalWrite(in1, 1);
      digitalWrite(in2, 0);
      digitalWrite(in3, 1);
      digitalWrite(in4, 0);
      break;

      

  
      case 0xFEF00F:
      digitalWrite(in1, 1);
      digitalWrite(in2, 0);
      digitalWrite(in3, 0);
      digitalWrite(in4, 1);
      break;

      case 0x2E2C2641:
      digitalWrite(in1, 1);
      digitalWrite(in2, 0);
      digitalWrite(in3, 0);
      digitalWrite(in4, 1);
      break;


      



      case 0xFE708F:
      digitalWrite(in1, 0);
      digitalWrite(in2, 1);
      digitalWrite(in3, 1);
      digitalWrite(in4, 0);
      break;

      case 0xAC4ED407:
      digitalWrite(in1, 0);
      digitalWrite(in2, 1);
      digitalWrite(in3, 1);
      digitalWrite(in4, 0);
      break;


      


      case 0xFEB04F:
      digitalWrite(in1, 0);
      digitalWrite(in2, 1);
      digitalWrite(in3, 0);
      digitalWrite(in4, 1);
      break;

      case 0xB563859:
      digitalWrite(in1, 0);
      digitalWrite(in2, 1);
      digitalWrite(in3, 0);
      digitalWrite(in4, 1);
      break;


      
      
      case 0xFE08F7:
      digitalWrite(in1, 0);
      digitalWrite(in2, 0);
      digitalWrite(in3, 0);
      digitalWrite(in4, 0);
      break;

      case 0xACD20847:
      digitalWrite(in1, 0);
      digitalWrite(in2, 0);
      digitalWrite(in3, 0);
      digitalWrite(in4, 0);
      break;

    }
    
    irrecv.resume();
  }
  

  if(potValue <= 341){
    analogWrite(ENA, speedA1);
    analogWrite(ENB, speedB1);
  }
  else if(potValue > 341 and potValue <= 682){
    analogWrite(ENA, speedA2);
    analogWrite(ENB, speedB2);
  }
  else{
    analogWrite(ENA, speedA3);
    analogWrite(ENB, speedB3);
    
  }
}


void ultrasonic(){
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;
  delay(1000);

  
}

Thanks in advance.

Lovro

SO, it works on 5 volts, but does not work on 12 volts. Don't you think a schematic of how you have all this wired up is more important than a bit of code, which doesn't care about voltage.

Paul

I mean, it all works ok, but it is now showing the correct distance. It makes no sense to me that the connections would be why it is like that.

i meant that it is not showing the right distance

You have two setups and one gives correct results, the other doesn't. You say that the hardware and code are the same so the only difference between them is how they are powered.

So to work out what is going on we need DETAILS about how they are powered. E.g. what exactly is the 12V "robot power supply", what other components are there and how is everything connected etc.

If I have to guess the problem may be associated with powering things via USB (so probably no motors) or powering things via the unnamed motor driver that you seem to have. But with no real information it's just a random guess.

Steve

Lovro2:
I mean, it all works ok, but it is now showing the correct distance. It makes no sense to me that the connections would be why it is like that.

If you're adamant the problem is not with the connections as they is are not the same between the two situations (different power supply!), then why are you so sure the problem is with the code which is the same between the two situations?