Automatic Fish Feeder problem on lcd

Hi guys this is my first project trying to make a automatic fish feeder
that will feed the fish every 12 hours
in this time im using millis() cuz i dont have at hand a RTC (already buy on ebay)

my code works fine it does what i need to do but i get a estrange behavior on the lcd

when the LDR change status my lcd get estrange characters and keep showing it forever until i reset or power off the arduino

usig a
1 x arduino nano with a sensor shield
2 x relay shield
1 x LDR
1 x LCD 1602
1 x pushButton
1 x jr servo (ST47)

power with a 5v 1 amp wall regulator

the project object is to feed the fish every 12 hours , turning the filter off before the servo feed the fish and stay off for 25 min
it turn the lights on/off using a LDR
the push button will feed the fish immediate
and if the system feed the fish will move the servo to feed the fish for a extra shake

my problem is the lcd the estrange characters just mess all around, cuz i cant see if already feed the fish it only happen when the relay for the light turn on/off ,

cuz this is my first project im a noob im and doing something wrong but i cant find the problem

i try changing my wall regulator to 9v 2amps that didn’t work

this is my code and advice will be very appreciated

fishfeeder_v2.ino.ino (8.75 KB)

I've looked at your code and I have two possible fixes:

1.) The LCD might not be wired correctly

2.) Double check the number of digits in the variables that you are printing to the LCD. For instance, if you have a variable that has 10 digits and you write it to the 16x2 LCD beginning at location (5,1), then you are going to "overflow" the LCD by 5 digits. That will make the LCD flip out. This is most likely the problem considering the number of longs you are printing to the LCD.

Hi, I suspect some Electromagnetic Interference problem because the problem happens when switching some power device.

See some Arduino Power info HERE:

There is more on interference problems HERE:

Let us know what you find...

Thank you guys I will look in both of your subjection I will let youknowwhati find, I really appreciate your time in looking in my code

terryking228:
Hi, I suspect some Electromagnetic Interference problem because the problem happens when switching some power device.

See some Arduino Power info HERE:

There is more on interference problems HERE:

Let us know what you find...

EMI? I highly doubt the LCD is freaking out due to EMI.

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Can you post a picture of you project so we can see your component layout?
What voltage is your pump?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks… Tom… :slight_smile:

I will post a pic and a hand writing sketch

The problem with my code is a little bit long I'm not allow to up that size

Yesterday I change the position of all my components and the problem persists but this time is only for a few sec , in a couple of min I will upload my sketch and a few pics

Here are a few pics and my sketch

#include <Servo.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);
Servo MyServo;

int degreesOfRotation = 2;        // initial dregrees for servo arm
int servoPinNumber = 8;           // servo pin on arduino
int PowerHeadRelay = 9;                 // relay power head pin
int LightsRelay = 10;               // relay for lights pin 
int sensorPin = A0;               // select the input pin for ldr
int sensorValue = 0;              // variable to store the value coming from the sensor
int  buttonPin = 7;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button

float TimePowerHeadOff = 25 * 60000; // time the powerhead will stay off after feeding 25 minutes
float hoursBetweenMeals = 12;     // hours between meals
float mealDelayTime = 0;          // set time on 0
int repeatFeeding = 3;            // times the servo shake foos container
long endtime;                     // var 1
long now;                         // var 2  
long now2;                         // var 2 
long endtime2;                     // var 1 
long timeleft1;                     // var 1 
long timeleft2;                     // var 1 
long timeleft3;                     // var 1 
long timeleft4;                     // var 1 
long var4;                     // var 1 



void setup() {
   Serial.begin(9600);                           //Serial begin
   lcd.begin(16,2);
   lcd.clear();
   pinMode(buttonPin, INPUT);
   pinMode(PowerHeadRelay, OUTPUT);                    //set powerhead pin to output
   pinMode(LightsRelay, OUTPUT);                   //set lights pin to output
   MyServo.attach(servoPinNumber);               // attach servo
   MyServo.write(degreesOfRotation);             // degrees for servo arm
   mealDelayTime = hoursBetweenMeals * 3600000;  // multiply hoursbetweenmeals by 3600000 (hour) 
   delay(20);
}

void loop() {
      digitalWrite(PowerHeadRelay,HIGH);      // powerhead on 
      now = millis();                   // set time since on  
      endtime = now + mealDelayTime;    // now plus hours between meals
  

  while(now < endtime){    //start counter
    now = millis();
        sensorValue = analogRead(sensorPin);  //read the LDR 
        Serial.print("Luz:  ");
        if(sensorValue < 300){
          Serial.println("Prendidas");
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" Luz prendida");      
        }else{
          Serial.println("Apagadas");   
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" Luz apagada"); 
        }

        Serial.println("-------------------------");        
        Serial.print("Proxima comida en ");
        timeleft1 = endtime - now;
        timeleft2 = timeleft1 / 3600000;
        Serial.print(timeleft2);
        Serial.println(" Horas"); 
        lcd.setCursor(0,1);
        lcd.print("Proxima");         
        lcd.setCursor(8,1);
        lcd.print(timeleft2);      
        lcd.setCursor(11,1);
        lcd.print("H");           
        Serial.println("-------------------------");        
            if(sensorValue < 300){
              digitalWrite(LightsRelay,HIGH);   // turn light on
            }else{
              digitalWrite(LightsRelay,LOW);    // turn light on
            }
             buttonState = digitalRead(buttonPin);
             if (buttonState == HIGH) {
              Serial.println("-------------------------");
               Serial.println("Alimentacion Manual");
               lcd.clear();
        lcd.setCursor(2,0);
        lcd.print("Alimentacion"); 
        lcd.setCursor(5,1);
        lcd.print("Manual");                                  
              Serial.println("-------------------------");
         digitalWrite(PowerHeadRelay,LOW);  //turn off powerhead for 3 secons before start feeding
         delay(3000);               
                feedingMotion();
             }
        delay(1000);       
  }
         digitalWrite(PowerHeadRelay,LOW);  //turn off powerhead for 3 secons before start feeding
         delay(3000);

         feedingMotion();
  }



  void feedingMotion(){
    
    int y = 1;                             // feeding counter
   while(y <= repeatFeeding){               // start feeding
            Serial.print("Alimentando = ");
        Serial.println(y);
        lcd.clear();
        lcd.setCursor(1,0);
        lcd.print("Alimentando :"); 
        lcd.setCursor(15,0);
        lcd.print(y);   
        while(degreesOfRotation < 180) {
          MyServo.write(degreesOfRotation);
          delay(5);
          degreesOfRotation +=1;
        }
           while(degreesOfRotation > 0) {
          MyServo.write(degreesOfRotation);
          delay(10);
          degreesOfRotation -=1;
        } 
        while(degreesOfRotation < 20) {
          MyServo.write(degreesOfRotation);
          delay(5);
          degreesOfRotation +=1;
        }     
           while(degreesOfRotation > 0) {
          MyServo.write(degreesOfRotation);
          delay(10);
          degreesOfRotation -=1;
        } 
        while(degreesOfRotation < 20) {
          MyServo.write(degreesOfRotation);
          delay(5);
          degreesOfRotation +=1;
        }            
          while(degreesOfRotation > 0) {
          MyServo.write(degreesOfRotation);
          delay(15);
          degreesOfRotation -=1;
        } 
       y++;
   }
                   now2 = millis();                   // set time since on  
                    endtime2 = now2 + TimePowerHeadOff;    // now plus hours between meals

  while(now2 < endtime2){    //start counter
    now2 = millis();
        sensorValue = analogRead(sensorPin);  //read the LDR 
        Serial.print("Luz:  ");
 if(sensorValue < 300){
          Serial.println("Prendidas");
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" Luz prendida");      
        }else{
          Serial.println("Apagadas");   
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print(" Luz apagada"); 
        }
        
        Serial.println("-------------------------");    
        Serial.print("Filtro prende en ");
        timeleft3 = endtime2 - now2;
        timeleft4 = timeleft3 / 60000;
        Serial.print(timeleft4);
        Serial.println(" Min.");         
        Serial.println("-------------------------");          
        lcd.clear();
        lcd.print(" Filtro apagado"); 
        lcd.setCursor(4,1);
        lcd.print(timeleft4);  
        lcd.print(" min.");     
            if(sensorValue < 300){
              digitalWrite(LightsRelay,HIGH);   // turn light on
            }else{
              digitalWrite(LightsRelay,LOW);    // turn light on
            }
             buttonState = digitalRead(buttonPin);
             if (buttonState == HIGH) {
              Serial.println("-------------------------");
               Serial.println("Extra Alimentacion Manual");
              Serial.println("-------------------------"); 
        lcd.clear();
        lcd.setCursor(2,0);
        lcd.print("Extra Comida");
        
             while(degreesOfRotation < 180) {
          MyServo.write(degreesOfRotation);
          delay(5);
          degreesOfRotation +=1;
        }
           while(degreesOfRotation > 0) {
          MyServo.write(degreesOfRotation);
          delay(10);
          degreesOfRotation -=1;
        } 
        while(degreesOfRotation < 20) {
          MyServo.write(degreesOfRotation);
          delay(5);
          degreesOfRotation +=1;
        }     
           while(degreesOfRotation > 0) {
          MyServo.write(degreesOfRotation);
          delay(10);
          degreesOfRotation -=1;
        } 
        while(degreesOfRotation < 20) {
          MyServo.write(degreesOfRotation);
          delay(5);
          degreesOfRotation +=1;
        }            
          while(degreesOfRotation > 0) {
          MyServo.write(degreesOfRotation);
          delay(15);
          degreesOfRotation -=1;
        } 
             }
        delay(1000);       
  }                         
                   digitalWrite(PowerHeadRelay,HIGH);   //turn powerhead on after TimePowerHeadOff
  }

If you type the words " lcd strange characters relay" into the search box of the forum you will find that you are not alone.

Start with a capacitor across the power and ground of the lcd. There is the possible use of snubbers and filters for the relay.

it only happen when the relay for the light turn on/off ,

If all hardware solutions fail, it not unheard of to add an lcd.begin(16,2) and refreshed display code after the block of code which operates the problem relay. You will not be solving the root cause of the problem, but you will keep the display coherent.

Hi,
Thanks for the circuit and pics.

The light is fluorescent by the looks of it, it produces massive spikes when it starts.
You have the controller situated right on top of the fluorescent assy which contains a ballast coil and starter unit.
AND

it only happen when the relay for the light turn on/off ,

Move the control box away from the fluorescent light for a start.

Hope it helps. Tom.... :slight_smile:

Yes, I agree with terryking228 and TomGeorge. As a test, if you disconnect the fluorescent light at the NO and COM terminals, but leave everything else connected, the problem should go away.

If so, I would use an MOV across the relay contacts. What is the AC voltage being switched?

a029e56894a3d43302a5a16d5f7bf16e8ac3661c.jpg
Light is directly under the control box.

cattledog:
Start with a capacitor across the power and ground of the lcd. There is the possible use of snubbers and filters for the relay.

My first idea to solve that was to add a cap but didn’t try lol you answer got a lot of sence thanks mate will add to test one

TomGeorge:
Hi,
Thanks for the circuit and pics.

The light is fluorescent by the looks of it, it produces massive spikes when it starts.
You have the controller situated right on top of the fluorescent assy which contains a ballast coil and starter unit.
ANDMove the control box away from the fluorescent light for a start.

Hope it helps. Tom… :slight_smile:

NP mate,
thanks to look on my project , never though about that lol, guess I will get a rgb led strip to change those flouresent light that I really hate lol

dlloyd:
Yes, I agree with terryking228 and TomGeorge. As a test, if you disconnect the fluorescent light at the NO and COM terminals, but leave everything else connected, the problem should go away.

If so, I would use an MOV across the relay contacts. What is the AC voltage being switched?

Actually in my test with out the flouresent light never got that problem my first though was to add a cap to the LCD cuz even without the lights I notice a change in the background of the LCD as the relay kick in, I will change those lights tho.

TomGeorge:
Light is directly under the control box.

Yeah that was my bad, was the only place I think when design the mechanic placement (servo)
But thanks to all of you guys give me a lot of good ideas to solve this problem ,

I really appreciate your time and help guys , will

This project was a necessity cuz I was going out of town needed something fast lol
But thanks to this project and you guys I’m learning a lot of stuff , still need a RTC or build asap a ups lol

But thanks to all of you, this project will become a really frustrating but enjoyable project lol

Now I need I to build a 3d printer a CNC milling , stepper motor, Bluetooth, Ethernet lot of sensors and a comprehensive wife lol

Edit: sorry guys I guess I miss a question sorry using 120 v (mexico)