Просто термометр на двух 7 сегментных индикаторах и аккумуляторе 18650

Нет ответов
andycat
andycat аватар
Онлайн
Зарегистрирован: 07.09.2017

Были комплектующие/настроение, собрал себе тупо термометр для контроля температуры браги.

И да, я в курсе, что просто термометр на AliExpress стоит раз в 10 дешевле :)

#include <OneWire.h>
#include <DallasTemperature.h>
#include "s7led.h"

#define ONE_WIRE_BUS A3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

unsigned long currentMillis;
bool flGetTemp = true;
unsigned long timerGetTemp;
#define periodGetTemp 1000UL
byte currTemp;
byte currPoint = 0;

void setup() {
  // put your setup code here, to run once:
  sensors.begin();
}

void loop() {
  process7led(); delay(4); currentMillis = millis();
  // put your main code here, to run repeatedly:
  if ((currentMillis - timerGetTemp) >= periodGetTemp) {
    timerGetTemp = currentMillis;
    if (currPoint) {
      setPoint0(false);
      setPoint1(true);
    } else {
      setPoint0(true);
      setPoint1(false);
    }
    currPoint = !currPoint;
    // get temp
    if (flGetTemp) {
      sensors.requestTemperatures();
      flGetTemp = false;
    } else {
      currTemp = (sensors.getTempCByIndex(0) + 0.7); // calibrate sensor
      currTemp = constrain(currTemp, 0, 99);
      show7LedData(currTemp / 10, currTemp % 10);
      flGetTemp = true;
    }
  }
}


//--s7led.h--
#include <arduino.h>

/*#define segm_a_pin 6 //PD6
#define segm_b_pin 7 //PD7
#define segm_c_pin 2 //PD2
#define segm_d_pin 1 //PD1
#define segm_e_pin 0 //PD0
#define segm_f_pin 5 //PD5
#define segm_g_pin 4 //PD4
#define segm_dp_pin 3 //PD3*/

#define razr_0_pin 8 //PB0
#define razr_1_pin 9 //PB1

#define commonRazrLevel HIGH

void show7LedData(byte inR0, byte inR1);
void process7led();
void setPoint0(bool fl);
void setPoint1(bool fl);
//--s7led.cpp--
#include "s7led.h"

byte LED_SG[10] = {0xE7, 0x84, 0xD3, 0xD6, 0xB4, 0x76, 0x77, 0xC4, 0xF7, 0xF6}; //, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E, 0xC2, 0x89, 0xF9, 0xF1, 0xC3, 0xA9, 0xC0, 0x8C, 0x98, 0x92, 0xC1, 0x91, 0xFE, 0xBF, 0xFF, 0x9C, 0xAB};
// 0123456789AbCdEFGHIJLnOPqSUY_- (null) (degree) (mm)

byte _show_razr0 = 0;
byte _show_razr1 = 0;
byte _curr_razr = 0;
byte _curr_segm = 0;
byte _point0;
byte _point1;
unsigned long _last_razr;

void show7LedData(byte inR0, byte inR1) {
  _show_razr0 = inR0;
  _show_razr1 = inR1;
}

void process7led() {
  // set segments
  byte dataPort;
  if (_curr_razr) {
    dataPort = LED_SG[_show_razr1];
    if (_point1) dataPort |= 0x08;
  } else {
    dataPort = LED_SG[_show_razr0];
    if (_point0) dataPort |= 0x08;
  }
  DDRD = dataPort;
  if (commonRazrLevel == HIGH) PORTD = ~dataPort; else PORTD = dataPort;
  if (_curr_razr) {
    digitalWrite(razr_0_pin, LOW);
    digitalWrite(razr_1_pin, commonRazrLevel);
    pinMode(razr_0_pin, INPUT);
    pinMode(razr_1_pin, OUTPUT);
  } else {
    digitalWrite(razr_0_pin, commonRazrLevel);
    digitalWrite(razr_1_pin, LOW);
    pinMode(razr_0_pin, OUTPUT);
    pinMode(razr_1_pin, INPUT);
  }
  _curr_razr = !_curr_razr;
}

void setPoint0(bool fl) {
  if (fl) _point0 = 1; else _point0 = 0;
}

void setPoint1(bool fl) {
  if (fl) _point1 = 1; else _point1 = 0;
}