Hi everyone,
I've been trying to get a project to work that involves an LCD display, rotary encoder and now an SD card.
I've managed to get the LCD display and rotary encoder to work successfully but when I add the SD module the rotary encoder doesn't work correctly, the buttonPin will work but the rotary encoder part stops working.
#include <time.h>
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(43,41,39,37,35,33);
#define outputA 51
#define outputB 49
#define buttonPin 47
char lcdmsg1[] = "Bioreactor LCD "; //printed at startup
char lcdmsg2[] = "v3.0"; // printed at startup
char lcdmsg3[] = "Initialising... ";
char lcdmsg4[] = "Ready ";
char mainMenuR1[] = "A ";
char mainMenuR2[] = "B ";
char mainMenuR3[] = "C ";
char mainMenuR4[] = "D ";
void setup() {
lcd.begin(16,2); //set up the size of the LCD screen (16 characters by 2 lines)
lcd.print(lcdmsg1);
lcd.setCursor(0,1); // move to the second line of the LCD
lcd.print(lcdmsg2);
delay(1000);
lcd.setCursor(0,1);
lcd.print(lcdmsg3);
//LCD Screen setup
pinMode(outputA,INPUT);
pinMode(outputB,INPUT);
pinMode(buttonPin,INPUT_PULLUP);
pinMode(LEDPin,OUTPUT);
Serial.begin(9600);
Serial.println("Initialisiation begun...");
aLastState = digitalRead(outputA); // get initial value of outputA
if(!SD.begin(53)) { //SD card not recognised
Serial.println("SD Card not recognised");
SD_err=true;
lcd.setCursor(0,1);
lcd.print(SDerror);
for (int i = 0; i<10; i++){
digitalWrite(DIODES_PIN_A, HIGH);
digitalWrite(LEDPin, LOW);
delay(1000);
digitalWrite(DIODES_PIN_A, LOW);
digitalWrite(LEDPin, HIGH);
}
}
else{ //Declare SD card is OK
Serial.println("Wiring is correct and SD card is initialised correctly");
lcd.setCursor(0,1);
lcd.print(lcdmsg5);
delay(500);
}
void loop() {
// put your main code here, to run repeatedly:
/*
*
*/
drawMenu(); // Displays the current selections
rotaryMenu(); // Checks for alterations to the rotary encoder and updates accordingly
ButtonTest(); // Cycles through all the button permeatations and implements accordingly.
}
void rotaryMenu(){
aState = digitalRead(outputA); //read the state of outputA
buttonPressed=digitalRead(buttonPin); //check if buttonPin has been pressed
if(aState != aLastState){ //If outputA has changed then decide which way it has moved
if (digitalRead(outputB) != aState){ // Case for CW movement
counter++;
down=true; //set variable to move down a selection in the menu
}
else{ //Case for CCW movement
counter --;
up=true; //set variable to move up a selection in the menu
}
/* Code for testing the RotaryEncoder works
lcd.setCursor(0,1); //set the position of the column and row you are printing to
// NB both col and row start with */
/*Serial.print("OutputA: ");
Serial.println(outputA);
Serial.print("OutputB: ");
Serial.println(outputB);
Serial.print("Position: ");
Serial.println(counter);
//lcd.print(" ");
lcd.print("Counter: ");
lcd.print(counter);
lcd.print(" "); // Gets rid of any trailing numbers on the display.
*/
}
int LEDcurrent = digitalRead(LEDPin);
/*Serial.print("LEDcurrent: ");
Serial.println(LEDcurrent);
Serial.print("Button Pressed: ");
Serial.println(buttonPressed);
*/
//Button Pressed
if(buttonPressed == LOW){
middle=true; //output to select buttonPressed functions elsewhere
if(LEDcurrent == HIGH){ //Operate LED if attached
//turn LED off
digitalWrite(LEDPin,LOW);
delay(500); //add delay for user pressing
}
if(LEDcurrent == LOW){ //Reverse of above LED operation (if attached)
//turn LED on
digitalWrite(LEDPin, HIGH);
delay(500); //add delay for user pressing
}
}
aLastState=aState; //set the comparison state to the current state
}
void drawMenu(){
/* Main menu
*
*/
if (page == 1){
lcd.setCursor(0,1);
if(menuitem==1){
lcd.print(mainMenuR1);
}
if(menuitem==2){
lcd.print(mainMenuR2);
}
if(menuitem==3){
lcd.print(mainMenuR3);
}
if(menuitem==4){
lcd.print(mainMenuR4);
}
}
void ButtonTest(){
/*Up
*
*/
//Main Menu
if (up && page == 1){
up=false;
menuitem--;
if(menuitem==0){ // catch all to stop at the top of the menu
menuitem=1;
}
}
/* Down
*
*/
// Main Menu
if(down && page == 1){
down =false;
menuitem++;
if(menuitem ==5){ //catch all to stop at the bottom of the menu
menuitem--;
}
}
/* Button Pressed
*
*/
if (middle){
middle = false;
//Main Menu
if (page == 1 && menuitem == 1){ //Enter batch culture submenu
page++;
delay(debounceTime); // Pause to stop double tapping and flicking between menus
Serial.print("Page: ");
Serial.println(page);
Serial.print("MenuItem: ");
Serial.println(menuitem);
Serial.print("Frame: ");
Serial.println(frame);
}
I'm sure it's something really simple that I'm missing but can anyone give me some help?
Thanks