Skip to content

Commit aa4ee8e

Browse files
committed
Implement AdaEncoder and ooPinChangeInt libraries
1 parent 7cf6341 commit aa4ee8e

File tree

2 files changed

+247
-232
lines changed

2 files changed

+247
-232
lines changed

encoder_test/encoder_test.ino

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// #include <EEPROM.h>
2-
// #include <Wire.h>
3-
// #include <RTClib.h>
4-
// RTC_DS1307 rtc;
5-
#include <Encoder.h> //https://www.pjrc.com/teensy/td_libs_Encoder.html
6-
7-
Encoder mainRot(A1,A0);
8-
const byte rotCount = 4; //values per detent. We will set each encoder to a "resting" value of rotCount, and sense moves between detents when the rot value reaches 0 (left) or rotCount*2 (right).
9-
byte mainRotLast = -999;
2+
#include <Wire.h>
3+
#include <RTClib.h>
4+
RTC_DS1307 rtc;
5+
#include <ooPinChangeInt.h>
6+
#include <AdaEncoder.h>
7+
8+
AdaEncoder mainRot = AdaEncoder('a',A1,A0);
9+
1010
word val = 500;
1111

1212
// Display formatting
@@ -36,14 +36,14 @@ void decToBin(bool binVal[], byte i);
3636
////////// Main code control //////////
3737

3838
void setup(){
39-
//Serial.begin(57600);
40-
//Wire.begin();
41-
//rtc.begin();
42-
//if(!rtc.isrunning()) rtc.adjust(DateTime(2017,1,1,0,0,0)); //TODO test
39+
Serial.begin(57600);
40+
Wire.begin();
41+
rtc.begin();
42+
if(!rtc.isrunning()) rtc.adjust(DateTime(2017,1,1,0,0,0)); //TODO test
4343
initOutputs();
4444
initInputs();
4545

46-
mainRot.write(rotCount);
46+
//mainRot.write(rotCount);
4747

4848
editDisplay(val,0,3,false);
4949

@@ -54,26 +54,11 @@ void setup(){
5454

5555
void loop(){
5656
//Things done every "clock cycle"
57-
//checkRTC(); //if clock has ticked, decrement timer if running, and updateDisplay
58-
//checkInputs(); //if inputs have changed, this will do things + updateDisplay as needed
57+
checkRTC(); //if clock has ticked, decrement timer if running, and updateDisplay
58+
checkInputs(); //if inputs have changed, this will do things + updateDisplay as needed
59+
60+
//long mainRotNew = mainRot.read();
5961

60-
long mainRotNew = mainRot.read();
61-
if(mainRotNew != mainRotLast){
62-
if(mainRotNew <= 0) { //down one detent
63-
val--;
64-
editDisplay(val,0,3,false);
65-
mainRotLast = rotCount;
66-
mainRot.write(rotCount);
67-
} else if(mainRotNew >= rotCount*2) {
68-
val++;
69-
editDisplay(val,0,3,false);
70-
mainRotLast = rotCount;
71-
mainRot.write(rotCount);
72-
} else {
73-
mainRotLast = mainRotNew;
74-
}
75-
editDisplay(mainRotLast,4,5,false);
76-
}
7762

7863
//doSetHold(); //if inputs have been held, this will do more things + updateDisplay as needed
7964
cycleDisplay(); //keeps the display hardware multiplexing cycle going
@@ -97,6 +82,36 @@ void initInputs(){
9782
//if(altAdjType==2) checkRot(altAdjA,altAdjB,altRotLast,false);
9883
}
9984

85+
void checkInputs(){
86+
AdaEncoder *thisEncoder=NULL;
87+
thisEncoder = AdaEncoder::genie();
88+
if(thisEncoder!=NULL) {
89+
int8_t clicks = thisEncoder->query();
90+
Serial.print(thisEncoder->getID()); Serial.print(':'); Serial.println(clicks);
91+
//editDisplay(clicks,4,5,false);
92+
//thisEncoder->getID();
93+
val += clicks;
94+
editDisplay(val,0,3,false);
95+
}
96+
}
97+
98+
////////// Clock ticking and timed event triggering //////////
99+
unsigned long rtcPollLast = 0; //maybe don't poll the RTC every loop? would that be good?
100+
byte rtcSecLast = 61;
101+
void checkRTC(){
102+
//Checks for new time-of-day second; decrements timer; checks for timed events;
103+
//updates display for running time or date.
104+
if(rtcPollLast<millis()+50) { //check every 1/20th of a second
105+
rtcPollLast=millis();
106+
//Check for timeouts based on millis
107+
//Update things based on RTC
108+
DateTime now = rtc.now();
109+
if(rtcSecLast != now.second()) {
110+
editDisplay(now.second(), 4, 5, true); //seconds
111+
}
112+
}
113+
}
114+
100115
void editDisplay(word n, byte posStart, byte posEnd, bool leadingZeros){
101116
//Splits n into digits, sets them into displayNext in places posSt-posEnd (inclusive), with or without leading zeros
102117
//If there are blank places (on the left of a non-leading-zero number), uses value 15 to blank tube

0 commit comments

Comments
 (0)