Hi.
Why do i get menuValue = 2 on startup?
I belive the problem is on the last bit of the code, on the button counter.
/*
*/
//--------Delays------------------
const int MinBpm = 2; //Minste antall blopp før "Ferdig"
const int Startdose = 5000; //millisek pumpe går på startdosen
const int sensorInterval = 300; // millisek mellom hver sensormåling
const int SensorValue = 900; // Sensorverdi for og telling
//---------- Display-------------------
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// --------LAYOUT---------------
enum PinAssignments {
encoderPinA = 2, // rigth
encoderPinB = 3, // left
};
const int manualAuto = 10;
const int buzzer = 13;
const int vortHeat = 11;
const int vortChiller = 12;
const int vortPump = 5;
const int vortVent = 6;
const int fermHeatHigh = 7;
const int fermHeatLow = 8;
const int fermPump = 9;
const int fermVent = A3;
const int bloppSensor = A0;
const int fermTemp = A1;
const int vortTemp = A2;
const int rotaryButton = 4;
//DISPLAY: SDA - ANALOG Pin 4, SCL - ANALOG pin 5
//--------VARIABLES (will change)------------------
int menuValue = 0; //variabel to navigate menu.
int mashTempSet = 0; // variable to store the value read
int mashTemp = 0; // variable to store the value read
int mashTimeSet = 0; // variable to store the value read
int mashTime = 0; // variable to store the value read
int boilTimeSet = 0; // variable to store the value read
int boilTime = 0; // variable to store the value read
int boilEffect = 0; // variable to store the value read
float fermTempSet = 0; // variable to store the value read
float fermTempVal = 0; // variable to store the value read
int bloppSensorState = 0;
int bloppCounter = 0; // counter for the number of bobbels
int ButtonState = 0; // current state of the sensor
int LastButtonState = 0; // previous state of the sensor
boolean runMainMenu = false;
boolean runYeastMenu = false;
boolean runBrewMenu = false;
boolean runSerile = false;
boolean runMash = false;
boolean runBoil = false;
boolean runBlopp = false;
boolean runFermenter = false;
boolean runCleane = false;
boolean runManual = false;
boolean cursorMain = false;
boolean updateSensors = false;
volatile unsigned int encoderPos = 0; // a counter for the dial
unsigned int lastReportedPos = 1; // change management
static boolean rotating=false; // debounce management
// interrupt service routine vars
boolean A_set = false;
boolean B_set = false;
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long bloppTime = 0;
unsigned long Valvetime = 0;
unsigned long previousMillis = 0; //
unsigned long previousSensorButtonMillis = 0; //
void setup() {
lcd.begin(20, 4); //change to (16,2) if using a 16x2 display
// ------- Quick 3 blinks of backlight -------------
for (int i = 0; i < 3; i++)
{
lcd.clear();
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on
lcd.setCursor(4, 0); //Start at character 4 on line 0
lcd.print("Starter opp!");
lcd.setCursor(2, 1);
lcd.print("AutoFerm");
lcd.setCursor(11, 1);
lcd.print("V0,11a");
lcd.setCursor(5, 3);
lcd.print("BeerPal AS");
delay(3000);
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
// turn on pullup resistors
digitalWrite(encoderPinA, HIGH);
digitalWrite(encoderPinB, HIGH);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, doEncoderB, CHANGE);
pinMode(vortPump, OUTPUT);
pinMode(fermPump, OUTPUT);
pinMode(fermHeatLow, OUTPUT);
pinMode(fermHeatHigh, OUTPUT);
pinMode(vortChiller, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(rotaryButton, INPUT_PULLUP);
// pinMode(fermTemp, INPUT); ????????????????????????
//pinMode(vortTemp, INPUT); ??????????????????????????
Serial.begin(9600);
if(digitalRead(manualAuto) == LOW )
{
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("CLEANING MENU");
lcd.setCursor(1, 1);
lcd.print("Run Quick Cleaning");
lcd.setCursor(1, 2);
lcd.print("Run Sterilization");
lcd.setCursor(1, 3);
lcd.print("Back to main menu");
menuValue = 1;
}
else
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("MANUAL MODE");
lcd.setCursor(1, 1);
lcd.print("Mash SET: ACT:");
lcd.setCursor(1, 2);
lcd.print("Ferm SET: ACT:");
lcd.setCursor(1, 3);
lcd.print("Blopps: PerMin:");
menuValue = 100;
}
}
void loop() {
currentMillis = millis();
rotating = true; // reset the debouncer
if (lastReportedPos != encoderPos) {
Serial.print("Index:");
Serial.println(encoderPos, DEC);
lastReportedPos = encoderPos;
}
runMenu();
}
// Interrupt on A changing state
void doEncoderA(){
// debounce
if ( rotating ) delay (1); // wait a little until the bouncing is done
// Test transition, did things really change?
if( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
encoderPos += 1;
rotating = false; // no more debouncing until loop() hits again
}
}
// Interrupt on B changing state, same as A above
void doEncoderB(){
if ( rotating ) delay (1);
if( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter - 1 if B leads A
if( B_set && !A_set )
encoderPos -= 1;
rotating = false;
}
}
void runMenu() {
if(runMainMenu == true) {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("AUTO MAIN MENU");
lcd.setCursor(1, 1);
lcd.print("Yeast propagation");
lcd.setCursor(1, 2);
lcd.print("Brewing");
lcd.setCursor(1, 3);
lcd.print("Cleaning");
menuValue = 1;
runMainMenu = false;
}
if((menuValue == 2) && (0 == (encoderPos % 3)))
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("YEAST PROPAGATION");
lcd.setCursor(1, 1);
lcd.print("Run propagation");
lcd.setCursor(1, 2);
lcd.print("Settings");
lcd.setCursor(1, 3);
lcd.print("Back to main menu");
menuValue = 10;
}
if((menuValue == 2) && (1 == (encoderPos % 3)))
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("BREWING MENU");
lcd.setCursor(1, 1);
lcd.print("Run Brew");
lcd.setCursor(1, 2);
lcd.print("Settings");
lcd.setCursor(1, 3);
lcd.print("Back to main menu");
menuValue = 20;
}
if((menuValue == 2) && (2 == (encoderPos % 3)))
{
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("CLEANING MENU");
lcd.setCursor(1, 1);
lcd.print("Run Quick Cleaning");
lcd.setCursor(1, 2);
lcd.print("Run Sterilization");
lcd.setCursor(1, 3);
lcd.print("Back to main menu");
menuValue = 30;
}
if((menuValue == 11) && (2 == (encoderPos % 3)))
{
runMainMenu = true;
}
if((menuValue == 21) && (2 == (encoderPos % 3)))
{
runMainMenu = true;
}
if((menuValue == 31) && (2 == (encoderPos % 3)))
{
runMainMenu = true;
}
if((menuValue <=100)){
cursorMain = true;
}
else
{cursorMain = false;}
if(0 == (encoderPos % 3) && (cursorMain == true)){
lcd.setCursor(0, 1);
lcd.print(">");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(" ");
}
if(1 == (encoderPos % 3) && (cursorMain == true)){
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(">");
lcd.setCursor(0, 3);
lcd.print(" ");
}
if(2 == (encoderPos % 3) && (cursorMain == true)){
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(">");
}
if (millis() - previousSensorButtonMillis >= sensorInterval) {
if (digitalRead(rotaryButton) == LOW) {
ButtonState = ! ButtonState; // this changes it to LOW if it was HIGH
// and to HIGH if it was LOW
previousSensorButtonMillis += sensorInterval;
}
}
ButtonState = digitalRead(rotaryButton);
if (ButtonState != LastButtonState) {
if (ButtonState == HIGH) {
menuValue++;
Serial.print("menuValue ");
Serial.println(menuValue);
}
LastButtonState = ButtonState;
}
}