Why do I get this error in the Ultrassonic context? - arduino

I have installed multiple libraries from ultrassonic and the last one I installed was " ultrasonic-master library". Can it be a library issue?
I have this code using ultrasonic sensor but I'm getting the 'unsigned int Ultrasonic::timing()' is private within this context
What can I try to fix this? Any help would be appreciated.
#include <Ultrasonic.h>
const int IN4 = 6;
const int IN3 = 7;
const int IN2 = 5;
const int IN1 = 4;
const int ENA = 3;
const int ENB = 2;
const int echoPin = 8; //digtal pin used for HC-SR04 ECHO in order to receive the signal
const int trigPin = 9; //digtal pin used for HC-SR04 ECHO in order to send the signal
Ultrasonic ultrasonic(trigPin, echoPin); //initializing the arduino pins
int distance;
String result;
int velocity = 0;
//on or off
boolean go = true;
float dist_cm = distance;
void setup() {
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
analogWrite(ENA, 145);
analogWrite(ENB, 145);
}
void loop() {
float distance = cmMsec();
dist_cm = distance;
if (dist_cm <= 10) {
decisao();
}
delay(100);
}
float cmMsec() {
float cmMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
return (cmMsec);
delay(10);
}
void go_forward()
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, velocity);
analogWrite(ENB, velocity);
}
void freio() {
digitalWrite(IN4, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN1, LOW);
analogWrite(ENA, velocity);
analogWrite(ENB, velocity);
}
void decisao() {
// this block is responsible for change the speed of the car when the distance goes UP
if (go == true) {
if (distance > 30) {
while (velocity < 120) {
go_forward();
velocity = velocity + 10;
delay(50);
}
}
if (velocity >= 120) {
while (distance >= 60 && distance <= 70) {
siga_em_frente();
velocity = velocity + 10;
delay(50);
}
}
if (distance < 70) {
while (velocity < 145) {
go_forward();
velocity = velocity + 10;
delay(50);
}
}
if (velocity >= 145) {
while (distance >= 90 && distance <= 100) {
go_forward();
velocity = velocity + 10;
delay(50);
}
}
if (distance < 100) {
while (velocity < 220) {
go_forward();
velocity = velocity + 10;
delay(50);
}
}
}
delay(10000);
// this block is responsible for change the speed of the car when the distance goes UP
if (go == true) {
if (distance >= 90 && distance <= 100) {
while (velocity <= 145) {
go_forward();
velocity = velocity - 10;
delay(50);
}
}
if (velocity > 140 && velocity <= 145) {
while (distance >= 70 && velocity <= 60) {
go_forward();
velocity = velocity - 10;
delay(50);
}
}
if (distance <= 60 && velocity <= 30) {
while (velocity <= 120) {
go_forward();
velocity = velocity - 10;
delay(50);
}
}
if (velocity > 110 && velocity <= 120) {
while (distance < 30) {
velocity = velocity - 10;
delay(50);
}
}
if (distance < 30) {
if (velocity > 5 && velocity <= 0) {
go = !go;
}
}
}
if (go == true) {
break();
}
}

unsigned int Ultrasonic::timing()
Is a private method. Within the Ultrasonic class that method is declared as private. That means it may only be called by the Ultrasonic class itself. It is not intended to be used by you.
In order to use it it would have to be public. So you would need to edit the class.

Related

ESP32-WROOM-32 PWD with millis

Beginner in Arduino and ESP-32 needs help.
Hello together,
I’m using the Pololu - VNH5019 Motor Driver Carrier to control a 12v motor with an ESP32.
In the following sketch i can speed up and speed down the ramp with delay();.
I tried to archiv the same result with millis(), but until now i could not make it.
What i am missing in my code.
Thanks in advance.
#define MOTOR_IN1 27
#define MOTOR_IN2 16
#define PWMPIN 14
#define frequency 40000
#define resolutionbit 8
const unsigned long eventInterval = 30;
unsigned long previousTime = 0;
void setup() {
pinMode(MOTOR_IN1, OUTPUT);
pinMode(MOTOR_IN2, OUTPUT);
ledcAttachPin(PWMPIN, 0); // assign the speed control PWM pin to a channel
ledcSetup(0, frequency, resolutionbit);
}
void loop() {
//with_delay();
with_millis();
}
//------------------------------------------
void with_delay() {
// set direction
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
// ramp speed up
for (int i = 0; i <= 255; i++) {
ledcWrite(0, i);
delay(30);
}
// ramp speed down
for (int i = 255; i >= 0; i--) {
ledcWrite(0, i);
delay(30);
}
}
//-------------------------------------------
void with_millis() {
unsigned long currentTime = millis();
if (currentTime - previousTime >= eventInterval) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
for (int i = 0; i <= 255; i++) {
ledcWrite(0, i);
previousTime = currentTime;
}
}
if (currentTime - previousTime >= eventInterval) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
for (int i = 255; i >= 0; i--) {
ledcWrite(0, i);
previousTime = currentTime;
}
}
}
Your problem is that the program gets stuck in the for loop.
You need to also create direction variable so the program that knows which if statement to execute.
You need to create some other logic that will increase the i variable without stopping the whole program.
The code:
//Initialize the i variable globaly:
int i = 0;
bool direction = 0;
//Your function:
void with_millis() {
unsigned long currentTime = millis();
if ((currentTime - previousTime >= eventInterval) && direction == true) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
i++;
if (i <= 255) {
ledcWrite(0, i);
previousTime = currentTime;
} elif (i > 255) {
i = 0;
direction = false;
}
}
if ((currentTime - previousTime >= eventInterval) && direction == false) {
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, HIGH);
i++;
if (i <= 255) {
ledcWrite(0, i);
previousTime = currentTime;
} elif (i > 255) {
i = 0;
direction = true;
}
}
}

Arduino: Multiplexing 4 Digit 7 Segment Display

I am relatively new to Arduino, so do not mind minor mistakes!
I have written the following code to multiplex a 4 digit 7 segment display with my Arduino Uno. Now I have made the mistake, so that when calling the showDigit () function, although the correct numbers are displayed, but not on the mentioned digit from the parameters. The named digit (here the 0 = first digit) turns off and the remaining ones indicate the number. Would anyone have the solution to the problem that when calling showDigit (1,0) on the first digit, the number 1 is used?
const int numeral[10] = {
//ABCDEFG /dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B00111110, // 6
B11100000, // 7
B11111110, // 8
B11100110, // 9
};
// pins for decimal point and each segment
// DP,G,F,E,D,C,B,A
const int segmentPins[] = { 2,7,4,5,6,3,8,13 };
const int nbrDigits= 4; // the number of digits in the LED display
//dig 0 1 2 3
const int digitPins[nbrDigits] = { 9, 10, 11, 12};
void setup()
{
for(int i=0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT); // set segment and DP pins to output
}
for(int i=0; i < nbrDigits; i++) {
pinMode(digitPins[i], OUTPUT);
}
}
void loop()
{
showDigit(1,0);
}
void showDigit( int number, int digit)
{
digitalWrite( digitPins[digit], HIGH );
for(int segment = 1; segment < 8; segment++) {
boolean isBitSet = bitRead(numeral[number], segment);
// isBitSet will be true if given bit is 1
// isBitSet = ! isBitSet; // Code Option*
// uncomment the above Code Option line for common anode display
digitalWrite( segmentPins[segment], isBitSet);
}
delay(5);
digitalWrite( digitPins[digit], LOW );
}
void showNumber( int number)
{
if(number == 0) {
showDigit( 0, nbrDigits-1) ; // display 0 in the rightmost digit
} else {
// display the value corresponding to each digit
// leftmost digit is 0, rightmost is one less than the number of places
for( int digit = nbrDigits-1; digit >= 0; digit--) {
if(number > 0) {
showDigit( number % 10, digit) ;
number = number / 10;
}
}
}
}
How about a function that will print out a whole 4 digit number instead of all that? I made this for a project I had, I used the binary value 10 to do an empty digit and I forced a dot on the 3rd digit. But you can see what approach I've taken to multiplex 4 numbers.
void writeNumber(int number)
{
int frameRate = 5;
byte pattern;
int cientos = 10;
int decimos = 10;
int singular = 0;
int decimal = 0;
if (number < 10000) {
if (number > 999) {
cientos = abs(number / 1000);
number = number - (cientos * 1000);
}
if (number > 99) {
decimos = abs(number / 100);
number = number - (decimos * 100);
} else {
if (cientos != 10) { decimos = 0; }
}
if (number > 9) {
singular = abs(number / 10);
number = number - (singular * 10);
}
if (number > 0) {
decimal = abs(number);
}
}
digitalWrite(digit[0], HIGH);
digitalWrite(digit[1], HIGH);
digitalWrite(digit[2], HIGH);
digitalWrite(digit[3], HIGH);
pattern = digit_pattern[cientos];
digitalWrite(digit_clock_pin, LOW);
shiftOut(data_pin, bit_clock_pin, LSBFIRST, ~pattern);
digitalWrite(digit_clock_pin, HIGH);
digitalWrite(digit[3], LOW);
delay(frameRate);
digitalWrite(digit[0], HIGH);
digitalWrite(digit[1], HIGH);
digitalWrite(digit[2], HIGH);
digitalWrite(digit[3], HIGH);
pattern = digit_pattern[decimos];
digitalWrite(digit_clock_pin, LOW);
shiftOut(data_pin, bit_clock_pin, LSBFIRST, ~pattern);
digitalWrite(digit_clock_pin, HIGH);
digitalWrite(digit[2], LOW);
delay(frameRate);
digitalWrite(digit[0], HIGH);
digitalWrite(digit[1], HIGH);
digitalWrite(digit[2], HIGH);
digitalWrite(digit[3], HIGH);
pattern = digit_pattern[singular];
pattern = pattern - B10000000;
digitalWrite(digit_clock_pin, LOW);
shiftOut(data_pin, bit_clock_pin, LSBFIRST, ~pattern);
digitalWrite(digit_clock_pin, HIGH);
digitalWrite(digit[1], LOW);
delay(frameRate);
digitalWrite(digit[0], HIGH);
digitalWrite(digit[1], HIGH);
digitalWrite(digit[2], HIGH);
digitalWrite(digit[3], HIGH);
pattern = digit_pattern[decimal];
digitalWrite(digit_clock_pin, LOW);
shiftOut(data_pin, bit_clock_pin, LSBFIRST, ~pattern);
digitalWrite(digit_clock_pin, HIGH);
digitalWrite(digit[0], LOW);
delay(frameRate);
}

Running a DC motor in linear sinusoidal motion with arduino, controlling hertz and direction change

After some searching around online I am able to run a 2-wire DC motor in a sinusoidal manner. It is a deconstructed printer carriage driven with an Arduino Uno and L298N motor driver. I'm using a potentiometer to control frequency. The code works well but I need help with a few things.
Is there an easy way to control the frequency it runs at? Possibly like setting it to 1.5, 1.6, 1.7,... etc, hertz, and have the sine function run motor? I am currently measuring the hertz but I am not able to get it to run accurately at intervals.
Is there a more elegant way to control the reversing of direction? I am reversing when the sin wave reaches its minimum. But at lower frequencies it will stay at minimum for a few samples and sometimes my method doesn't work... I am currently counting the samples at certain speeds and hard coding that count to reverse.
#define enA 9
#define in1 6
#define in2 7
#define INTERVAL 1000 // time between reads
int val;
int dir;
int count;
unsigned long lastRead = 0;
int frequency = 0;
int times = 0;
int sample_count = 0;
int sensorPin = 0; // The potentiometer is connected to
// analog pin 0
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
// Set initial rotation direction
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
Serial.begin(9600);
}
void reverseMotor(int pwnOutput) {
/* reserse the motor direction
uses the pwm outpur from the sine function
*/
if (dir == 0) {
Serial.println("LEFT <<<<<<<<<<<<<<<<<<<<<<<<<");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
dir = 1;
} else if (dir == 1) {
Serial.println("RIGHT >>>>>>>>>>>>>>>>>>>>>>>>");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
dir = 0;
}
analogWrite(enA, pwnOutput);
sample_count++;
}
void runMotor(int pwnOutput) {
analogWrite(enA, pwnOutput);
sample_count++;
}
void loop() {
static uint16_t phase;
int sensorValue;
int adjusting_speed;
int reverse_fix;
sensorValue = analogRead(sensorPin);
phase = (map(sensorValue, 0, 1023, 1, 256));
uint16_t sin_wave[phase];
for (int16_t i = 0; i < (phase); i++) {
float angle = TWO_PI * i / phase;
int16_t val = sin(angle) * (1024 - 1);
int pwmOutput;
int dir;
val += 1024;
sin_wave[i] = val;
pwmOutput = map(val, 0, 2047, 100, 255); // mapping higher than
// 0 to ensure motor is
// still moving,
Serial.println(pwmOutput);
//Serial.println(phase);
//Serial.println(val);
if (pwmOutput != 100) {
runMotor(pwmOutput);
count = 0;
} else {
count++;
// THERE HAS GOT TO BE A BETTER WAY TO DO THIS
if (phase >= 1 && phase <= 32) {
reverse_fix = 1;
}
if (phase >= 33 && phase <= 54) {
reverse_fix = 2;
}
if (phase >= 55 && phase <= 72) {
reverse_fix = 3;
}
if (phase >= 73 && phase <= 91) {
reverse_fix = 4;
}
if (phase >= 92 && phase <= 109) {
reverse_fix = 5;
}
if (phase >= 110 && phase <= 127) {
reverse_fix = 6;
}
if (phase >= 128 && phase <= 156) {
reverse_fix = 7;
}
if (phase >= 157 && phase <= 174) {
reverse_fix = 8;
}
if (phase >= 175 && phase <= 193) {
reverse_fix = 9;
}
if (phase >= 194 && phase <= 207) {
reverse_fix = 10;
}
if (phase >= 208 && phase <= 233) {
reverse_fix = 11;
}
if (phase >= 234 && phase <= 256) {
reverse_fix = 12;
}
if (count >= reverse_fix) {
reverseMotor(pwmOutput);
}
}
}
if (millis() - lastRead >= INTERVAL) { // if INTERVAL has passed
/*
Counting and measuring hertz
*/
val = frequency;
times++;
float vals = val / times;
float hertz = float(sample_count) / float(phase);
lastRead = millis();
Serial.println();
Serial.println("Hz:");
Serial.println(hertz);
Serial.println();
sample_count = 0;
}
}
I am new to Arduino, so any help or advice is appreciated.

Arduino lcd screen showing broken characters at random times

So I've been working on an arduino project for an escape room puzzle. It's essentially a bomb with a timer on an lcd screen. After all of the elements of the bomb are solved the timer stops and the lcd screen displays that the bomb has been defused and gives the group a clue to the next puzzle. 9 times out of 10 it works perfectly. But every once in a while when it is supposed to display that the bomb is defused the lcd screen just shows random broken characters. I haven't had any luck diagnosing the problem. Hoping somebody here might have an idea.
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Tone.h>
#define pound 14
Tone tone1;
int Scount = 0;
int Mcount = 0;
int Hcount = 1;
int DefuseTimer = 0;
long secMillis = 0;
long interval = 1000;
char password[6] = "594432";
int currentLength = 0;
int i = 0;
char entered[6];
int ledPin = 23;
int ledPin2 = 25;
int ledPin3 = 27;
int ledPin4 = 29;
int ledPin5 = 31;
int ledPin6 = 34;
const int plugin1 = 44;
const int plugin2 = 46;
const int plugin3 = 48;
const int plugin4 = 50;
const int plugin5 = 52;
int plugin1State = 0;
int plugin2State = 0;
int plugin3State = 0;
int plugin4State = 0;
int plugin5State = 0;
const int switch1 = 37;
int switch1State = 0;
const int key1 = 40;
int key1State = 0;
int puzzle1 = 0;
int puzzle2 = 0;
int puzzle3 = 0;
int solved = 0;
LiquidCrystal lcd(7, 8, 10, 11, 12, 13);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {A4, A5, A6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(plugin1, INPUT);
pinMode(plugin2, INPUT);
pinMode(plugin3, INPUT);
pinMode(plugin4, INPUT);
pinMode(plugin5, INPUT);
digitalWrite(plugin1, HIGH);
digitalWrite(plugin2, HIGH);
digitalWrite(plugin3, HIGH);
digitalWrite(plugin4, HIGH);
digitalWrite(plugin5, HIGH);
pinMode(switch1, INPUT);
digitalWrite(switch1, HIGH);
pinMode(key1, INPUT_PULLUP);
digitalWrite(key1, HIGH);
tone1.begin(9);
lcd.begin(16, 2);
Serial.begin(9600);
lcd.clear();
lcd.setCursor(0, 0);
tone1.play(NOTE_E6, 200);
delay(3000);
lcd.clear();
currentLength = 0;
}
void loop()
{
timer();
plugin1State = digitalRead(plugin1);
plugin2State = digitalRead(plugin2);
plugin3State = digitalRead(plugin3);
plugin4State = digitalRead(plugin4);
plugin5State = digitalRead(plugin5);
if (plugin1State == LOW && plugin2State == LOW && plugin3State == LOW && plugin4State == LOW && plugin5State == LOW)
{
puzzle1 = 1;
}
if (puzzle1 == 1)
{
digitalWrite(ledPin4, HIGH);
switch1State = digitalRead(switch1);
if (switch1State == LOW)
{
puzzle2 = 1;
}
}
if (puzzle2 == 1)
{
digitalWrite(ledPin5, HIGH);
key1State = digitalRead(key1);
if (key1State == LOW)
{
puzzle3 = 1;
}
if (key1State == HIGH)
{
digitalWrite(ledPin6, LOW);
}
}
if (puzzle3 == 1)
{
digitalWrite(ledPin6, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
while (currentLength < 6)
{
timer();
char key2 = keypad.getKey();
if (key2 == "#")
{
currentLength = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
}
else if (key2 != NO_KEY)
{
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
lcd.print(key2);
entered[currentLength] = key2;
currentLength++;
tone1.play(NOTE_C6, 200);
delay(100);
lcd.noCursor();
lcd.setCursor(currentLength + 6, 0);
lcd.print("*");
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
}
}
if (currentLength == 6)
{
if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3] && entered[4] == password[4] && entered[5] == password[5])
{
solved = 1;
while (solved == 1)
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("BOMB 1 DEFUSED");
currentLength = 0;
digitalWrite(ledPin3, HIGH);
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("RELEASE");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("TOXIC GAS");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("CLUE: %&#$#");
delay(6000);
}
}
else
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Wrong Password!");
delay(1500);
currentLength = 0;
}
}
}
}
void timer()
{
if (Hcount <= 0)
{
if ( Mcount < 0 )
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("The Bomb Has ");
lcd.setCursor (0, 1);
lcd.print("Exploded!");
while (Mcount < 0)
{
digitalWrite(ledPin, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
}
}
}
lcd.setCursor (0, 1); // sets cursor to 2nd line
lcd.print ("Timer:");
if (Hcount >= 10)
{
lcd.setCursor (7, 1);
lcd.print (Hcount);
}
if (Hcount < 10)
{
lcd.setCursor (7, 1);
lcd.write ("0");
lcd.setCursor (8, 1);
lcd.print (Hcount);
}
lcd.print (":");
if (Mcount >= 10)
{
lcd.setCursor (10, 1);
lcd.print (Mcount);
}
if (Mcount < 10)
{
lcd.setCursor (10, 1);
lcd.write ("0");
lcd.setCursor (11, 1);
lcd.print (Mcount);
}
lcd.print (":");
if (Scount >= 10)
{
lcd.setCursor (13, 1);
lcd.print (Scount);
}
if (Scount < 10)
{
lcd.setCursor (13, 1);
lcd.write ("0");
lcd.setCursor (14, 1);
lcd.print (Scount);
}
if (Hcount < 0)
{
Hcount = 0;
}
if (Mcount < 0)
{
Hcount --;
Mcount = 59;
}
if (Scount < 1) // if 60 do this operation
{
Mcount --; // add 1 to Mcount
Scount = 59; // reset Scount
}
if (Scount > 0) // do this oper. 59 times
{
unsigned long currentMillis = millis();
if (currentMillis - secMillis > interval)
{
tone1.play(NOTE_G5, 200);
secMillis = currentMillis;
Scount --; // add 1 to Scount
digitalWrite(ledPin2, HIGH); // sets the LED on
delay(10); // waits for a second
digitalWrite(ledPin2, LOW); // sets the LED off
delay(10); // waits for a second
//lcd.clear();
}
}
}
That would most likely be a an electrical issues either with your circuit of your LCD. The fact that a simple code like yours (not trying to insult you in any way) works 9/10 of times means that there is probably nothing wrong with the code (I've seen none).
For quick check, try reinstalling the IDE. That might update the Arduino libraries that get downloaded with the IDE. I doubt that it would solve the issue but that is an quick free easy way to try
I would personnaly suggest to disconnect everything, and reconnecting them. And if it doesn't work, then return that LCD and get yourself a new one.
I had the same issue when I used a non shielded 1m ribbon cable for the 16x2 LCD. The main board with the MCU had to be placed further away from the display. It showed random character for any electromagnetic interference. For example, turning on a fluorescent tube, starting an electric screwdriver, relay switches etc. Shortening the ribon cable solved the problem.
Anyway, the LCD is sensitive to electromagnetic interference. Keep it away from these sources.

Rectangle pattern using arduino Mega

I am using an Arduino Mega to implement a Rectangle pattern movement of a device which mean the length and breadth should decrease each time it reaches the end edges. I have written the code but it is not working
//servo header
#include <Servo.h>
#include <Math.h>
Servo myservo;//servo Object
//motor pin config
int m1r = 9;
int m1f = 10;
int m2f = 12;
int m2r = 11;
//infrared counter
int ir1val=0;//pulse count from irval
int ir2val=0;//pulse count from irval
int len=1;
int bred=2;
//Different sensors used
int temp;
int buzz = 8;
const float radpulse = 0.05;//1 pulse = 0.05 meter
double pulsecount1;//pulse counter;
double pulsecount2;//pulse counter;
int pc1;//integer
int pc2;
void setup() {
// initialize the Motor pin as an output:
pinMode(m1f, OUTPUT);
pinMode(m1r, OUTPUT);
pinMode(m2f, OUTPUT);
pinMode(m2r, OUTPUT);
pinMode(buzz,OUTPUT);
myservo.attach(A0);//BLDC motor attachment
Serial.begin(9600);// standard serial bandwidth of all modules
}
void loop()
{
pulsecount1=len/radpulse;
pc1=(int)round(pulsecount1);
pulsecount2=bred/radpulse;
pc2=(int)round(pulsecount2);
pc1=3;
pc2=2;
myservo.write(30);
delay(5000);
labelL1:
if((pc2 == 0)&&(pc1 == 0))
{
goto Stop;
}
while(pc1 > ir1val)
{
temp=((5.0*analogRead(A3)*100)/1024);
if(temp >60)
{
// goto hibernate;
}
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
digitalWrite(m2f,HIGH);
digitalWrite(m1f,HIGH);
//digitalWrite(m2f,HIGH);
if(digitalRead(A2))
{
ir1val++;
}
}
if(pc1 == ir1val)
{
/*if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
pc1--;
ir1val=0;
digitalWrite(m1f, HIGH);
digitalWrite(m2f,LOW);
delay(5000);
goto labelB1;
}
labelB1:
while(pc2 != ir1val)
{
temp=((5.0*analogRead(A3)*100)/1024);
if(temp >60)
{
// goto label2;
}
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
digitalWrite(m1f,HIGH);
digitalWrite(m2f,HIGH);
if(digitalRead(A2))
{
ir2val++;
}
}
if(pc2 == ir2val)
{
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
pc2=pc2--;
ir2val=0;
digitalWrite(m1f, HIGH);
digitalWrite(m2f,LOW);
delay(5000);
goto labelL1;
}
Stop:
myservo.write(0);// stop servo
digitalWrite(m1f,LOW);// stop motor
digitalWrite(m2f,LOW);//stop motor
digitalWrite(buzz,HIGH);//Buzzer
delay(5000);
goto Stop;
/*
hibernate:
temp=((5.0*analogRead(A2)*100)/1024);
if(temp >40)
{
delay(1000);
goto hibernate;
}
*/}

Resources