Well, I know there is something that needs to be taken out. The LCD menu seemed to have some code for the encoder. Wish someone would've commented on if all that is needed is the read_encoder() routine.
/*
Mega 2560 (Tools → Board)
*/
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define MOVECURSOR 9 // constants for indicating whether cursor should be redrawn
#define MOVELIST 10
#define ENC_PORT PINC // https://arduino.stackexchange.com/a/21296/47871
const byte totRows = 4; // total rows of LCD
const byte totCols = 20; // total columns of LCD
unsigned long timeoutTime = 0; // this is set and compared to millis to see when the user last did something.
const int menuTimeout = 10000; // time to timeout in a menu when user doesn't do anything.
const int a = 30; // Paragon // 30 mm = 1.2" = 0.1'
const int b = 61; // 0.2 foot paragon
const int c = 91; // 0.3 foot paragon
const int d = 122; // 0.4 foot paragon
const int e = 152; // 0.5 foot paragon
const int f = 183; // 0.6 foot paragon
const int g = 213; // 0.7 foot paragon
unsigned long startMillis;
unsigned long currentMillis;
unsigned long currentMillisA;
unsigned long currentMillisD;
unsigned long currentMillis1;
const unsigned long period = 1000;
const byte txPin3 = 14;
const byte rxPin3 = 15;
const byte txPin2 = 16;
const byte rxPin2 = 17;
const byte pinAnalog = 2;
const byte pinDigital = 3; // slide switch switch allows user to select (input) test
const byte pinOneWire = 4;
const byte oneWireBus = 5;
const byte pinAnalogW = 6; // write (output) to test datalogger
const byte pinDigitalW = 7; // ----------------
const byte pinOneWireW = 8; // ----------------
const byte pinA = 22; // 1st hardware interrupt: CLK
const byte pinB = 26; // 2nd hardware interrupt: DT
volatile int diffFromParagon = 0;
volatile int inch = 0;
// https://forum.arduino.cc/index.php?topic=418692.0 // volatile
volatile byte aFlag = 0; // indicates when expected rising edge: pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // (opposite direction to when aFlag is set for pinB)
volatile byte encoderPos = 0; // encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile byte oldEncPos = 0; // last encoder position value, compares to current reading (to know when to print to the serial monitor)
volatile byte reading = 0; // stores direct values read from interrupt pins before checking to see if moved a whole detent
LiquidCrystal lcd(52, 50, 49, 47, 43, 41); // Arduino pins. Creates object.
// LCD pin (RS enable D4 D5 D6 D7) parameters
OneWire oneWire(oneWireBus);
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire);
// Passes oneWireBus reference to Dallas Temperature.
void setup() {
startMillis = millis(); // initial start time
// LCD code:
Serial.begin(9600);
lcd.begin(20, 4);
// pinMode not needed for INPUT (default) operator
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
// encoder code:
pinMode(pinA, INPUT_PULLUP); // pulled HIGH to the logic voltage (5V or 3.3V for most cases)
pinMode(pinB, INPUT_PULLUP);
attachInterrupt(0,PinA,RISING); // interrupt, looking for rising edge signal and
attachInterrupt(1,PinB,RISING); // executing the "PinX" Interrupt Service Routine (below)
Serial1.begin(115200); // pin 18 (TX) & 19 (RX)
digitalWrite(pinB, HIGH);
// oneWire sensors:
sensors.begin();
}
void loop() {
currentMillis = millis(); // milliseconds since start
lcd.setCursor(0, 0);
lcd.print("Welcome.");
lcd.setCursor(0, 1);
lcd.print("Ready to make a selection.");
byte analogSelection = digitalRead(pinAnalog); // 2
byte digitalSelection = digitalRead(pinDigital); // 3
byte oneWireSelection = digitalRead(pinOneWire); // 4
if(analogSelection == HIGH)
{
currentMillisA = millis();
if(currentMillisA - startMillis > 1000*60) // one min.
{
lcd.clear();
lcd.setCursor(0, 0); // 1st line
lcd.print("Testing...");
lcd.setCursor(0, 1); // 2nd
lcd.print("Analog inputs");
lcd.setCursor(0, 2); // 3rd
lcd.print("Please wait.");
for(int pin = 0; pin < 8; pin++)
for(int s = 63; s <= 255; s += 64)
{ // PWM: 255 = 5V
analogWrite(pin, s); // sweeps 1.24, 2.5, 3.75 & 5V
delay(500);
}
}
}
else if(digitalSelection == HIGH)
{
currentMillisD = millis();
if(currentMillisD - startMillis > 1000*60) // one min.
basicMenu(); /////////////////////////////////////////////////// TEST CONTIUNITY FOR THE SENSOR ON PIN 4 TO CHEDFKCK!@!#@
}
else if(oneWireSelection == HIGH)
{
currentMillis1 = millis();
if(currentMillis1 - startMillis > 1000*60); // one min.
lcd.print("Testing temperatures...");
sensors.requestTemperatures(); // gets temperature readings
lcd.print("Temperature is: ");
lcd.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
// "0" refers to the first IC on the wire
// (You can have more than one DS18B20 on the same bus.)
}
else // multiple inputs toggled at once, error message:
lcd.print("Only select one test at a time.");
// https://forum.arduino.cc/index.php?topic=223286.0 Maybe multiple things can happen...
// Original encoder code (but it was incomple: had to read_encoder() routine
static byte counter = 0; //this variable will be changed by encoder input
char tmpdata;
tmpdata = read_encoder();
// New encoder code (might conflict with original above)
if(oldEncPos != encoderPos)
{
Serial1.println(encoderPos);
oldEncPos = encoderPos;
}
}
The rest of the code--the function definitions--is in the complete sketch attached.
sketch_jul17.ino (16.1 KB)