Skip to content

Commit

Permalink
rotary encoder (broken)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-wild committed Jun 9, 2019
1 parent 802f0e7 commit 12fad7a
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions proofing_box.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@
#include "DHT.h"
#include "SoftwareSerial.h"

const int pin_enco_1 = 2; // 2/3 are special pins. the encoder must be here
const int pin_enco_2 = 3;
const int pin_enco_sw = 4; // encoder switch
const int pin_disp_tx = 8; // pin that the display RX is connected to.
const int pin_disp_rx = 7; // needed to create the software serial. doesn't need physical connection
const int baud_rate = 9600; // set correct baud rate. default of s7s is 9600
const int disp_brightness = 255;
const int pin_dht = 2; // pin that the DHT is connected to
const int pin_dht = 12; // pin that the DHT is connected to
const int dht_type = DHT22; // DHT 22 (AM2302)
const int update_freq = 2000; // time to wait between loops

char buff[10]; // display output buffer
volatile int enco_prev = 0; // previous encoder direction
volatile long enco_value = 25; // the current encoder value
int target_temp;

SoftwareSerial disp(pin_disp_rx, pin_disp_tx); // the display

Expand All @@ -28,15 +34,37 @@ void setup() {
clearDisplay();
setBrightness(disp_brightness);
delay(100);
disp.print("HELO");
disp.write("JELO");

// init dht
dht.begin();

// init encoder
pinMode(pin_enco_1, INPUT);
pinMode(pin_enco_2, INPUT);
pinMode(pin_enco_sw, INPUT);

digitalWrite(pin_enco_1, HIGH); // turn pullup resistor on
digitalWrite(pin_enco_2, HIGH); // turn pullup resistor on
digitalWrite(pin_enco_sw, HIGH); // turn pullup resistor on

attachInterrupt(0, updateEncoder, CHANGE); // call updateEncoder() when interrupt 0 (pin 2) or 1 (pin 3) change
attachInterrupt(1, updateEncoder, CHANGE);


// init system
setTargetTemperature(enco_value);
}

void loop() {
delay(update_freq);


if (enco_value != target_temp) {
// encoder was rotated
setTargetTemperature(enco_value);

}


float humid = dht.readHumidity();
float temp = dht.readTemperature(false);

Expand All @@ -49,6 +77,8 @@ void loop() {
float heat_index = dht.computeHeatIndex(temp, humid, false);

writeTemperature(heat_index);

delay(update_freq);
}

void writeTemperature(float temp) {
Expand All @@ -59,6 +89,14 @@ void writeTemperature(float temp) {
disp.print(buff);
}

void setTargetTemperature(int target) {
target_temp = target;
setDecimals(0b010000);
sprintf(buff, "T %2d", target_temp);
disp.print(buff);

delay(2000);
}

void clearDisplay() {
disp.write(0x76); // clear command byte
Expand All @@ -73,3 +111,17 @@ void setDecimals(byte decimals) {
disp.write(0x77); // decimal command byte
disp.write(decimals); // (Apos)(Colon)(Digit 4)(Digit 3)(Digit2)(Digit1)
}

void updateEncoder() {
int msb = digitalRead(pin_enco_1);
int lsb = digitalRead(pin_enco_2);

int enco = (msb << 1) | lsb; // convert the 2 pin value to single number
int sum = (enco_prev << 2) | enco; // compare to the previous value

// add or subtract based on rotation
if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) enco_value++;
if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) enco_value--;

enco_prev = enco; // save for next time
}

0 comments on commit 12fad7a

Please sign in to comment.