Can someone please tell me what's going on with my LCD screen and how I can fix this issues below?
I have the following:
- Arduino Uno R3 w/ ATMEGA 328P-PU
- 16x2 LCD
- TowerPro MicroServo SG90
- DPDT Slide Switch w/ 120 ohm resistor
Objective: Read the sensor value (If switch is on or off), and move a servo accordingly while displaying switch state on LCD.
Issue: LCD display seems to receive random data during program run (see image). All starts up well, and as soon as a few seconds have lapsed in the void loop(), the screen becomes garbled. Also, the switch value (while debugging) gets a zero reading while HIGH, acting accordingly as if the switch was off. The servo motor runs as it should, but gets random pulses every now and again where it starts to buzz, but doesn't turn.
Images: Three images below.
Schematic:
Coding:
#include <LiquidCrystal.h>
#include <Servo.h>
const int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int oldPos = 0;
int pos = 0;
int oldStat = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo servo;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor (0, 0);
lcd.print("Starting up FSS!");
delay(700);
lcd.clear();
lcd.print("Init SERVO...");
delay(500);
servo.attach(9);
lcd.clear();
lcd.print("SERVO Success!");
delay(400);
lcd.clear();
lcd.print("Testing SERVO...");
servo.write(0);
delay(200);
servo.write(90);
delay(200);
servo.write(45);
delay(300);
lcd.clear();
lcd.print("SERVO Running!");
lcd.clear();
lcd.print("Select AUX Source");
pinMode(ledPin, OUTPUT);
}
void loop() {
lcd.clear();
lcd.setCursor (0, 0);
lcd.print("Select AUX Src");
lcd.setCursor(0, 1);
sensorValue = analogRead(sensorPin);
Serial.print("Sensor: ");
Serial.println(sensorValue);
if(sensorValue == 0){
//if(sensorValue = oldStat){
oldStat = sensorValue;
digitalWrite(ledPin, LOW);
lcd.print("Switch is OFF");
pos = 45;
Serial.println("Switch is OFF");
//}
} else{
digitalWrite(ledPin, HIGH);
lcd.print("Switch is ON");
pos = 120;
Serial.println("Switch is ON");
}
if (pos != oldPos){
servo.write(pos);
}
oldPos = pos;
delay (700);
}