Problem - no distance showing up on LCD

Hello, I wanted to make a distance sensor with LEDs signaling how near to the sensor the object is and with the LCD to display the distance itself, but I ran into a problem, there's no distance showing up.

const int trigPin = 10;
const int echoPin = 9;
const int ledCount = 3;
int ledPins[]={3,5,6};
int brightness = 0;
float distance, duration;
#include <LiquidCrystal.h>
LiquidCrystal lcd(0, 1, 2, 4, 12, 13);

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  lcd.begin(16,2);
  lcd.print("Attalums cm:");
  
  for(int thisLed = 0; thisLed < ledCount; thisLed++)
  
  {
    pinMode(ledPins[thisLed], OUTPUT);
  }

}

void loop()
{
 digitalWrite(trigPin, LOW); // Lai trigPin būtu "attiestatīts" no iepriekšējām darbībām
 delayMicroseconds(2);

 digitalWrite(trigPin, HIGH); // Aktivizē ultraskaņas vilni, kas "atsitas" pret objektu,
 delayMicroseconds(10);       //un tiek uztrevrts ar sensoru, kas savienots ar echoPin

 digitalWrite(trigPin, LOW);
  
 duration = pulseIn(echoPin, HIGH); //pulseIn funkcija nolasa, cik ilgi echoPin ir HIGH (tikpat cik ilgs ir vilnis)
                                   //funkcija mērīs laiku, nobrīža, ka echoPin ir HIGH līdz LOW
 distance = (duration*034) / 2;//izmantosim formulu, lai aprēķinātu attālumu : s=t*v (dalīts ar 2, jo distance tiek veikta 2x)
                                   //skaņas ātrums ir 343 m/s = 0,0343 cm/miksrosekundes (jo tā mēra pulseIn)

 
  
 if(distance >= 222 && distance <=330)//atkarībā no attāluma:tas sadalīts 3 vienādās daļās
{
  analogWrite(A1, 0); //deg zaļā, brīdinošā gaisma, rezistora lielums tika apskatīts internetā
  analogWrite(A2, 0);
  analogWrite(A3, 255);
  delay(500);
  analogWrite(A1, 0);
  analogWrite(A2, 0);
  analogWrite(A3, 0);
  
  lcd.setCursor(0, 1); 
  lcd.print(distance); //izvada attālumu
  
  for(int thisLed = 0; thisLed < ledCount; thisLed=thisLed+1)//sarkano spuldzīšu degšanas ātrums un fade funkcija
  {
    for (brightness = 0; brightness <= 255; brightness += 15) 
    {
    analogWrite(ledPins[thisLed], brightness);
    delay(45); 
    }
  }
  
  for(int thisLed = ledCount-1; thisLed >= 0; thisLed=thisLed-1)
  {
    for (brightness = 255; brightness >= 0; brightness -= 15) 
    {
    analogWrite(ledPins[thisLed], brightness);
    delay(45);
    }
  }
  
  delay(500);
}
  
  
 else if(distance >=113  && distance <222)
{
  analogWrite(A1, 204); //deg dzeltenā, brīdinošā gaisma
  analogWrite(A2, 51);
  analogWrite(A3, 204);
  delay(300);
  analogWrite(A1, 0);
  analogWrite(A2, 0);
  analogWrite(A3, 0);
   
  lcd.setCursor(0, 1); 
  lcd.print(distance); //izvada attālumu
   
  for(int thisLed = 0; thisLed < ledCount; thisLed=thisLed+1)//sarkano spuldzīšu degšanas ātrums un fade funkcija
  {
    for (brightness = 0; brightness <= 255; brightness += 15) 
    {
    analogWrite(ledPins[thisLed], brightness);
    delay(30); 
    }
  }
  
  for(int thisLed = ledCount-1; thisLed >= 0; thisLed=thisLed-1)
  {
    for (brightness = 255; brightness >= 0; brightness -= 15) 
    {
    analogWrite(ledPins[thisLed], brightness);
    delay(30);
    }
  }
  
  delay(400);
}
   
  else if(distance >=3  && distance <113)
{
  analogWrite(A1, 255); //deg sarkanā, brīdinošā gaisma
  analogWrite(A2, 0);
  analogWrite(A3, 0);
  delay(200);
  analogWrite(A1, 0);
  analogWrite(A2, 0);
  analogWrite(A3, 0);
  
  lcd.setCursor(0, 1); 
  lcd.print(distance); //izvada attālumu
  
  for(int thisLed = 0; thisLed < ledCount; thisLed=thisLed+1)//sarkano spuldzīšu degšanas ātrums un fade funkcija
  {
    for (brightness = 0; brightness <= 255; brightness += 15) 
    {
    analogWrite(ledPins[thisLed], brightness);
    delay(10); 
    }
   }
  
  for(int thisLed = ledCount-1; thisLed >= 0; thisLed=thisLed-1)
  {
    for (brightness = 255; brightness >= 0; brightness -= 15) 
    {
    analogWrite(ledPins[thisLed], brightness);
    delay(10);
    }
  }
     
}
}

Can you print something (anything) on the display with a simpler program?

Can you print the distance to the serial port to check which section of the code should be doing something.

Did you use the leading zero on 034 for a reason - it tells the compiler it's an octal number.

Pins 0 and 1 are used to communicate with the PC. Try moving those two to some other I/O pins and retest.

analogWrite(A1, 0);
analogWrite(A2, 0);
analogWrite(A3, 0);

A0-A5 are analog IN only.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.