Marine Tank PowerHead Arduino (Advice)

Hello,

This is my First time here so go easy on me

Im Trying to complete a program for my Arduino that allows me to manage my pumps. The program is pretty much where I want it to be, It currently runs a 16x2 LCD and displays system modes selected by a push button!

I wanted to add the ability to use a rotary encoder to slide through the modes rather than using a button. Ive got my code to see the encoder and print out its digital state over serial but no matter what i try im unable to get the High of the encoder to trigger my push button (pin7)

I think i may be going about it the wrong way trying to use the encoders state to trigger a pin rather than telling the Arduino that a change of state in the encoder means trigger the next (if loop)

I look forward to any information you could help me with

Please feel free to use my program if its any use to you!

#define AIR 11
#define BUTTON 7
int val = 0;
int old_val = 0;
int state = 0;
int led = 12;
int newled = 12;
int encoder0PinA = 8;
int encoder0PinB = 9;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 4, A3, A2, A1, A0);

const byte WAVECONTROLPOWERHE = 13 ;
const byte LEFTPH = 13 ;
const byte RIGHTPH = 11 ;
const byte BIGPH = 10 ;
const unsigned long WAVECONTROLPOWERHEinterval = 1000UL ;
const unsigned long RIGHTPHinterval = 3000UL ;
const unsigned long LEFTPHinterval = 4030UL ;
const unsigned long BIGPHinterval = 2060UL ;

unsigned long WAVECONTROLPOWERHEtimer = 0 ;
unsigned long RIGHTPHtimer = 0 ;
unsigned long LEFTPHtimer = 0 ;
unsigned long BIGPHtimer = 0 ;

int WAVECONTROLPOWERHEState = LOW ;
int RIGHTPHState = LOW ;
int BIGPHState = LOW ;
int LEFTPHState = LOW ;

void setup() { 

  lcd.begin(16, 2);
   pinMode (encoder0PinA,INPUT);
   pinMode (encoder0PinB,INPUT);
   Serial.begin (9600);
   pinMode (WAVECONTROLPOWERHE, OUTPUT);
   pinMode (RIGHTPH, OUTPUT);
   pinMode (AIR, OUTPUT);
   pinMode (BIGPH, OUTPUT);
   pinMode (LEFTPH, OUTPUT);
   pinMode (BUTTON, INPUT);
  
  WAVECONTROLPOWERHEtimer = millis () ;
  RIGHTPHtimer = millis () ;
  BIGPHtimer = millis () ;
  LEFTPHtimer = millis () ;
}
void loop() {
    n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     if (digitalRead(encoder0PinB) == LOW) {
       digitalWrite(BUTTON, HIGH);
     } else {
       digitalWrite(BUTTON, HIGH);
     }
     Serial.print (encoder0Pos);
     Serial.print ("/");
     digitalWrite(BUTTON, HIGH);
   } 
   encoder0PinALast = n;
   val = digitalRead (BUTTON);
   if((val == HIGH) && (old_val == LOW))
      {
    newled = led - 1; 
     if (newled == 7) { newled = 12; } 
    led = newled;
    delay(10);
   }
 old_val = val;
  if (led == 12)
  {
    lcd.setCursor(0, 1);
    lcd.print ("  Tank Feeding  ");
    lcd.setCursor(1, 0);
    lcd.print ("All Pumps Stop");
    digitalWrite (RIGHTPH, LOW);
    digitalWrite (AIR, LOW);
    digitalWrite (BIGPH, LOW);
    digitalWrite (LEFTPH, LOW);
    digitalWrite (BUTTON, LOW);
    }
if (led == 11)
  {
  lcd.setCursor(0, 0);
  lcd.print ("  Random Mode!  ");
 if ( (millis () - LEFTPHtimer) >= LEFTPHinterval ) {
   if (LEFTPHState == LOW)
     LEFTPHState = HIGH ;
   else
     LEFTPHState = LOW ;
   digitalWrite (LEFTPH, LEFTPHState ) ;
  lcd.setCursor(13, 1);
  lcd.print (LEFTPHState);
   LEFTPHtimer = millis () ;
 }
 if ( (millis () - RIGHTPHtimer) >= RIGHTPHinterval ) {
   if (RIGHTPHState == LOW)
     RIGHTPHState = HIGH ;
   else
     RIGHTPHState = LOW ;
   digitalWrite (RIGHTPH, RIGHTPHState ) ;
   lcd.setCursor(0 , 1 );
   lcd.print ("PUMPS ACTIVE ");
   lcd.setCursor(14, 1);
   lcd.print (RIGHTPHState);
   RIGHTPHtimer = millis ()  ;
 }
if ( (millis () - BIGPHtimer) >= BIGPHinterval ) {
   if (BIGPHState == LOW)
     BIGPHState = HIGH ;
   else
     BIGPHState = LOW ;
   digitalWrite (BIGPH, BIGPHState ) ;
   lcd.setCursor(15, 1);
   lcd.print (BIGPHState);
   BIGPHtimer = millis ()  ;
 }
  }
  if (led == 10)
  {
  lcd.setCursor(0, 0);
  lcd.print ("Ocean Wave Mode!");
 if ( (millis () - WAVECONTROLPOWERHEtimer) >= WAVECONTROLPOWERHEinterval ) {
   if (WAVECONTROLPOWERHEState == LOW) 
  {
    WAVECONTROLPOWERHEState = HIGH;
    RIGHTPHState = LOW;
  }
  else
  {
    WAVECONTROLPOWERHEState = LOW ;
    RIGHTPHState = HIGH ;
  }
   digitalWrite (WAVECONTROLPOWERHE, WAVECONTROLPOWERHEState ) ;
   digitalWrite (RIGHTPH, RIGHTPHState ) ;
   lcd.setCursor(0, 1);
   lcd.print ("Wave Maker   -  ");
   lcd.setCursor(12, 1);
   lcd.print (WAVECONTROLPOWERHEState);
   lcd.setCursor(14, 1);
   lcd.print (RIGHTPHState);
   WAVECONTROLPOWERHEtimer = millis () ;
 }
 }
  if (led == 9)
  {
    lcd.setCursor(0,0);
    lcd.print (" ALL TANK PUMPS ");
    lcd.setCursor(0, 1);
    lcd.print ("SET TO RUN FULL!");
  digitalWrite (RIGHTPH, HIGH);
  digitalWrite (AIR, HIGH);
  digitalWrite (BIGPH, HIGH);
  digitalWrite (LEFTPH, HIGH);
  digitalWrite (BUTTON, HIGH);
  } 
    if (led == 8)
  {
  lcd.setCursor(0, 0);
  lcd.print (" Low Power Mode ");
  lcd.setCursor(0, 1);
  lcd.print (" For Aclamation ");
  digitalWrite (RIGHTPH, LOW);
  digitalWrite (AIR, LOW);
  digitalWrite (BIGPH, HIGH);
  digitalWrite (LEFTPH, LOW);
 } 
  }

This is where I am struggling to implement the encoder

oid loop() {
    n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     if (digitalRead(encoder0PinB) == LOW) {
       digitalWrite(BUTTON, HIGH);
     } else {
       digitalWrite(BUTTON, HIGH);
     }
     Serial.print (encoder0Pos);
     Serial.print ("/");
     digitalWrite(BUTTON, HIGH);
   }

Hello.

Just to update you i have been working on the code and still seem to be having the same problem.

Heres my updated code!

Many thanks

#define AIR 11
#define BUTTON 7
int val = 0;
  int old_val = 0;
  int state = 0;
  int led = 12;
  int newled = 12;
  int encoder0PinA = 8;
  int encoder0PinB = 9;
  int encoder0Pos = 0;
  int encoder0PinALast = LOW;
  int n = LOW;

  #include <LiquidCrystal.h>
  LiquidCrystal lcd(2, 4, A3, A2, A1, A0);

  const byte WAVECONTROLPOWERHE = 13 ;
  const byte GRAVELPUMP = 13 ;
  const byte RIGHTPH = 11 ;
  const byte LEFTPH = 10 ;
  const unsigned long WAVECONTROLPOWERHEinterval = 1000UL ;
  const unsigned long RIGHTPHinterval = 12000UL ;
  const unsigned long GRAVELPUMPinterval = 44030UL ;
  const unsigned long LEFTPHinterval = 7060UL ;

  unsigned long WAVECONTROLPOWERHEtimer = 0 ;
  unsigned long RIGHTPHtimer = 0 ;
  unsigned long GRAVELPUMPtimer = 0 ;
  unsigned long LEFTPHtimer = 0 ;

  int WAVECONTROLPOWERHEState = LOW ;
  int RIGHTPHState = LOW ;
  int LEFTPHState = LOW ;
  int GRAVELPUMPState = LOW ;


void setup() { 

    lcd.begin(16, 2);
    pinMode (encoder0PinA,INPUT);
    pinMode (encoder0PinB,INPUT);
    Serial.begin (9600);

    pinMode (WAVECONTROLPOWERHE, OUTPUT);
    pinMode (RIGHTPH, OUTPUT);
    pinMode (AIR, OUTPUT);
    pinMode (LEFTPH, OUTPUT);
    pinMode (GRAVELPUMP, OUTPUT);
    pinMode (BUTTON, INPUT);
    
    WAVECONTROLPOWERHEtimer = millis () ;
    RIGHTPHtimer = millis () ;
    LEFTPHtimer = millis () ;
    GRAVELPUMPtimer = millis () ;

  }

  void loop() {

     n = digitalRead(encoder0PinA);
     if ((encoder0PinALast == LOW) && (n == HIGH)) {
       if (digitalRead(encoder0PinB) == LOW) {
         digitalWrite(BUTTON, HIGH);
       } else {
         digitalWrite(BUTTON, HIGH);
       }
       Serial.print (encoder0Pos);
       Serial.print ("/");
       digitalWrite(BUTTON, HIGH);
     } 
     encoder0PinALast = n;

     val = digitalRead (BUTTON);
     if((val == HIGH) && (old_val == LOW))
     
     {
      newled = led - 1; 
       if (newled == 7) { newled = 12; } 
      led = newled;
      delay(10);
     }
      old_val = val;
     if (led == 12)
    {
      lcd.setCursor(1, 0);
      lcd.print ("  Feeding Mode  ");
      lcd.setCursor(0, 1);
      lcd.print ("Pumps Auto Start");
      digitalWrite (RIGHTPH, LOW);
      digitalWrite (AIR, LOW);
      digitalWrite (LEFTPH, LOW);
      digitalWrite (GRAVELPUMP, LOW);
      digitalWrite (BUTTON, LOW);
      delay(100000);
      
      void software_Reset(); // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile ("  jmp 0"); 

} 
      }
  if (led == 11)
    {
    lcd.setCursor(0, 0);
    lcd.print ("  Random Mode!  ");
   if ( (millis () - GRAVELPUMPtimer) >= GRAVELPUMPinterval ) {
     if (GRAVELPUMPState == LOW)
       GRAVELPUMPState = HIGH ;
     else
       GRAVELPUMPState = LOW ;
     digitalWrite (GRAVELPUMP, GRAVELPUMPState ) ;
    lcd.setCursor(13, 1);
    lcd.print (GRAVELPUMPState);
     GRAVELPUMPtimer = millis () ;
   }
   if ( (millis () - RIGHTPHtimer) >= RIGHTPHinterval ) {
     if (RIGHTPHState == LOW)
       RIGHTPHState = HIGH ;
     else
       RIGHTPHState = LOW ;
     digitalWrite (RIGHTPH, RIGHTPHState ) ;

     lcd.setCursor(0 , 1 );
     lcd.print ("PUMPS ACTIVE ");
     lcd.setCursor(14, 1);
     lcd.print (RIGHTPHState);
     RIGHTPHtimer = millis ()  ;
   }
  if ( (millis () - LEFTPHtimer) >= LEFTPHinterval ) {
     if (LEFTPHState == LOW)
       LEFTPHState = HIGH ;
     else
       LEFTPHState = LOW ;
     digitalWrite (LEFTPH, LEFTPHState ) ;
     lcd.setCursor(15, 1);
     lcd.print (LEFTPHState);
     LEFTPHtimer = millis ()  ;
   }
    }
     if (led == 10)
    {
    lcd.setCursor(0, 0);
    lcd.print ("Ocean Wave Mode!");
   if ( (millis () - WAVECONTROLPOWERHEtimer) >= WAVECONTROLPOWERHEinterval ) {
     if (WAVECONTROLPOWERHEState == LOW)
     {
      WAVECONTROLPOWERHEState = HIGH;
      RIGHTPHState = LOW;
    }
    else
    {
      WAVECONTROLPOWERHEState = LOW ;
      RIGHTPHState = HIGH ;
    }
    digitalWrite (WAVECONTROLPOWERHE, WAVECONTROLPOWERHEState ) ;
    digitalWrite (RIGHTPH, RIGHTPHState ) ;
    lcd.setCursor(0, 1);
    lcd.print ("Wave Maker   -  ");
    lcd.setCursor(12, 1);
    lcd.print (WAVECONTROLPOWERHEState);
    lcd.setCursor(14, 1);
    lcd.print (RIGHTPHState);
    WAVECONTROLPOWERHEtimer = millis () ;
   }
    }
      if (led == 9)
    {
      lcd.setCursor(0,0);
      lcd.print (" ALL TANK PUMPS ");
      lcd.setCursor(0, 1);
      lcd.print ("SET TO RUN FULL!");
    digitalWrite (RIGHTPH, HIGH);
    digitalWrite (AIR, HIGH);
    digitalWrite (LEFTPH, HIGH);
    digitalWrite (GRAVELPUMP, HIGH);
    digitalWrite (BUTTON, HIGH);
    } 
      if (led == 8)
    {
      lcd.setCursor(0, 0);
    lcd.print (" Low Power Mode ");
    lcd.setCursor(0, 1);
    lcd.print (" For Aclamation ");
    digitalWrite (RIGHTPH, LOW);
    digitalWrite (AIR, LOW);
    digitalWrite (LEFTPH, HIGH);
    digitalWrite (GRAVELPUMP, LOW);
   } 
    }

You have posted a lot of code and none of it is in code tags

so it looks like this and is easy to work with

Please go back and modify your posts. The button like a scroll with <> is for code.

You have a short piece of code in loop() - can you make that into a complete compilable program and post that. It will be much easier to give advice without the clutter of the rest of the project.

My guess is that you need to amend your button driven code so that pressing buttons changes the value of a variable and the rest of the code acts on that value.

Then you can add on code that uses the encoder to change the same variable.

...R

Thank you for your reply! I do appreciate it.

Unfortunately I am unable to provide you with the information you need as I am extremely noob with the Arduino!

I do understand what your saying but i don't know how to correctly format the code to make it conform, unfortunately I created this script in a text file from a few examples and pasted it in to the Arduino software!

I think what your saying about the system using a value for each item rather than an if statement would fit my needs better, I think my main problem is i have been using the code with a push button on Pin 7 for a long time and just decided id like to use an encoder rather than creating the code with it from day one!

I will have a look about keeping values and see if this helps me.

One of the reasons the script like I did is because I was struggling to run a pump pattern for a period of time without using a delay in the code as it blocked me switching modes (All pumps ON) / OFF / Pulse

Thank you again for your input!

:slight_smile:

Rusjp:
i don't know how to correctly format the code to make it conform

All you have to do is select the block of code in the Forum editor and click the code button - the button that looks like a scroll with <> on it. If you hover over the button you will see the prompt "code".

Read How to use the forum - it is there to help you get the most from the Forum.

The demo several things at a time illustrates how to manage time without using delay().

Unfortunately I am unable to provide you with the information you need as I am extremely noob with the Arduino!

I don't know what you mean by this as the only thing I asked you to do was build the short code into a full program.

When you present your code so I can study it easily I will try to help further.

...R

Thank you for your time