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);
}
}