Just purchased my first Arduino and have decided on an ambitious first project!
I'm going to build a 8x Relay Timer controller with a 20x4 i2c LCD and Rotary Encoder with push button as an interface.
This will form the basis of a sprinkler timer project with 8 individual timed outputs but could be used for anything....
So far I've acquired the following parts:
1- Arduino (ATmega168)
2- 20x4 LCD w/ i2c interface
3- Pansonic rotary encoder with push button (EVQ-WTEF2515B)
I've also ordered (still to arrive in the mail!):
4- 8 Channel 5V relay module board
5- I2C RTC DS1307 AT24C32 Real Time Clock module
6- PCF8574 IO Expansion Board [I2C-bus to 8-bit parallel]
--
I'm hoping to come up with a intuitive menu system using the rotary encoder to allow a user to configure each timer's settings.
See the photos below for my progress so far.
Here's my test code to get the LCD and Encoder up and running:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // Define a 20x4 LCD with address of 0x27
#define ENC_A 8 // Define the Rotary Encoder Pins
#define ENC_B 9
#define ENC_SW 10 // Define the Rotary Encoder Switch
#define ENC_PORT PINB //The port that the rotary encoder is on
// Variable for the button's current state.
// button_mode = 1 when the button is up, and 0 when the button is pressed.
// This variable is 'static' and persists because it is declared outside of a function.
int button_mode = 1;
void setup()
{
/* Setup encoder pins as inputs */
pinMode(ENC_A, INPUT);
digitalWrite(ENC_A, HIGH);
pinMode(ENC_B, INPUT);
digitalWrite(ENC_B, HIGH);
digitalWrite(ENC_SW, HIGH);
delay(500);
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin (115200);
Serial.println("Start");
lcd.setCursor(0, 0);
lcd.println("Counter value: ");
lcd.setCursor(0, 1);
lcd.println("Button: Released");
}
void loop()
{
static uint8_t counter = 0; //this variable will be changed by encoder input
int8_t tmpdata;
/**/
tmpdata = read_encoder();
if( tmpdata ) {
lcd.setCursor(15, 0);
lcd.println(counter, DEC);
counter += tmpdata;
}
// Check Digital ENC_S to see if it's pressed.
if ((digitalRead(ENC_SW) == LOW) && (button_mode == 1))
{
// Button was up before, but is pressed now. Set the button to pressed
// and print to LCD
button_mode = 0; // Button is pressed.
lcd.setCursor(8, 1);
lcd.println("Pressed");
}
else if ((digitalRead(ENC_SW) == HIGH) && (button_mode == 0))
{
// Button was down before, but is released now. Set the button to
// released and print to LCD.
button_mode = 1; // Button is released.
lcd.setCursor(8, 1);
lcd.println("Released");
}
}
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
static uint8_t old_AB = 0;
/**/
old_AB <<= 2; //remember previous state
old_AB |= ( ENC_PORT & 0x03 ); //add current state
return ( enc_states[( old_AB & 0x0f )]);
}
Any suggestions or advice would be greatly appreciated!!!
A quick question - I seem to be getting 2 vertical bars followed by 3 vertical bars in the last two segments of each line on the LCD.
Can someone let me know what may be causing these??
(Refer to the attached pictures)
Stay tuned for more updates