Hello, so i am trying to make a simple project. I am using an arduino uno and i want to use ultrasonic sensor to judge the distance and project it on 16*2 LCD. Based on the reading of the distance, there is displacement in the servo motor and change in colour in RGB light. At my first try, i couldn't even make the LCD light up. I have changed my ultrasonic sensor, my breadboard and my connecting wires. Further i have checked all the hardware bits and they work fine perfectly on their own. Which makes me believe there is a problem with my code which is not letting me achieve the desired results. Could someone help? The images attached are the only 2 readings i get.
I have deliberately put all the commands to servo in comments. I wanted to focus on the sensor and LCD and make it work.
#include <Servo.h> //include servo library
#include <LiquidCrystal.h> //include LCD library
/*
int pos = 0; //Variable to store the servo position
int servoPin = 9; //Servo is hooked to pin 9
int servoDelay = 25; //25 milliseconds delay after each servo write
Servo mainMotor; //Assigning a name to the servo motor.
float servoAngle; //Angle we would want the servo to move
*/
LiquidCrystal LCD(10, 9, 5, 4, 3, 2); //Define RS, E, DB4, DB5, DB6 and DB7
int trigPin = 13; //Pin that is responsible for sending out the wave (Digital)
int echoPin = 6; //Pin that is responsible for recieveing the wave (Analog)
float pingTime; // Distance taken by the sound wave to hit the target and come back
float targetDistance; //Distance of the target from the sensor
int soundSpeed = 33400; //Speed of sound in cm/micros
int redPin = 13; //Assigning the red pin to pin 13 of arduino
int greenPin = 12; //Assigning the green pin to the pin 12 of arduino
int bluePin = 7; //Assigning the blue pin to the pin 7 of arduino
int rBrightness; //analog value for the brightness of red LED
int gBrightness; //analog value for the brightness of green LED
int bBrightness; //analog value for the brightness of blue LED
void setup() {
// mainMotor.attach(servoPin); //Telling the arduino to fix the servo mainMotor with the pin assigned as sevoPin.
// pinMode(servoPin, OUTPUT); //Declaring that the servoPin is an output
Serial.begin(9600);
LCD.begin(16,2); //Defining the size of the LCD. In this case, 16 columns and 2 rows.
LCD.setCursor(0,0); //Placing the cursor on the first column of the first row.
pinMode(trigPin, OUTPUT); // Telling arduino trigPin is an output
pinMode(echoPin, INPUT); //Telling arduino that echoPin is an input
pinMode(redPin, OUTPUT); //Telling arduino redPin is an output
pinMode(bluePin, OUTPUT); //Telling arduino bluePin is an output
pinMode(greenPin, OUTPUT); //Telling arduino greenPin is an output
LCD.print("Object is- ");
}
void loop() {
digitalWrite(trigPin, LOW); //a pulse is set to low
delayMicroseconds(2000); //wait
digitalWrite(trigPin, HIGH); //the pulse is generated from the trigPin
delayMicroseconds(1500); //wait
digitalWrite(trigPin, LOW); //the pulse wave is switched off again
delayMicroseconds(10); //wait
pingTime = pulseIn(echoPin, HIGH); //time taken is equal to the time taken by echoPin to recieve the pulse signal sent by trigPin.
pingTime = pingTime/1000000; //conversion from microseconds to milliseconds
targetDistance = soundSpeed*pingTime; //calculating distance
targetDistance = targetDistance/2; //Calculating distance in cm.
Serial.println(pingTime);
if (targetDistance > 35) { //if the distance is more than 35cm
LCD.setCursor(12,0); //setting the cursor location
LCD.print("far");
LCD.setCursor(0,1);
LCD.print(targetDistance);
LCD.print(" ");
LCD.print("cm");
delay(1000); //wait
gBrightness = 8*targetDistance-230; //by plotting graph where Y axis is LED brightness and X axis is targetDistance.
digitalWrite(redPin, LOW);
digitalWrite(greenPin, gBrightness);
analogWrite(bluePin, LOW);
// servoAngle = 0;
}
if (targetDistance < 35 && targetDistance > 15 ) {
LCD.setCursor(12,0);
LCD.print("arnd");
LCD.setCursor(0,1);
LCD.print(targetDistance);
LCD.print(" ");
LCD.print("cm");
delay(1000);
bBrightness = 10*targetDistance-100;
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
analogWrite(bluePin, bBrightness);
// servoAngle = 0;
}
if (targetDistance < 15) {
LCD.setCursor(12,0);
LCD.print(" ");
LCD.setCursor(12,0);
LCD.print("near");
LCD.setCursor(0,1);
LCD.print(targetDistance);
LCD.print(" ");
LCD.print("cm");
delay(1000);
rBrightness = (13.3*targetDistance) + 50;
digitalWrite(redPin, rBrightness);
digitalWrite(greenPin, LOW);
analogWrite(bluePin, LOW);
// servoAngle = 12*targetDistance - 10; //Relation between movement of servo and target distance.
}
}


