Skip to content

Instantly share code, notes, and snippets.

@Just-AndyE
Created January 25, 2020 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Just-AndyE/e4e12d96d92bfc5d12902a08207998d5 to your computer and use it in GitHub Desktop.
Save Just-AndyE/e4e12d96d92bfc5d12902a08207998d5 to your computer and use it in GitHub Desktop.
Тахометр для сверлильного станка на Arduino
/*
Programed 2020 by AndyE
apocrifa13@gmail.com
Based on code created 2016
by AlexGyver
AlexGyver Home Labs Inc.
*/
/*Типы экранов:
Тип Контроллер Тип соединения Описание
1 1602 I2C 16x2 LCD (стандартный шрифт)
2 1602 I2C 16x2 LCD (большие цифры) использована библиотека https://github.com/roman2712/LCD1602_bigNumbers_Arduino.git
3 1637 SPI 4x7 сегментов LED
4 TM74HC595 SPI 4x7 сегментов LED
5 1331 SPI OLED 0.93"
*/
#define SCREEN_TYPE 3
//Количество полюсов на оборот
#define POLES 1
//Для руссифицированных LCD экранов поставить 2, надписи на англицком - 1
#define LANG 1 //Пока не проверено и не реализовывалось. Задел на будущее.
//Пины SPI интерфейса
#if (SCREEN_TYPE == 3||SCREEN_TYPE == 4||SCREEN_TYPE == 5)
#define PIN_DC 5
#define PIN_SCK 6
#define PIN_CS 7
#define PIN_RES 8
#define PIN_DI 9
#endif
#include <Arduino.h>
//Подключение экрана
//16x2 LCD
#if (SCREEN_TYPE == 1||SCREEN_TYPE == 2)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C display(0x27, 16, 2);
#endif
#if (SCREEN_TYPE == 2)
#include "bigNumbers.h"
bigNumbers <LiquidCrystal_I2C> bigNumbersLcd(&display);
byte digit[4] = {0, 0, 0, 0};
#endif
//7-segment displays
#if (SCREEN_TYPE == 3)
#include <TM1637Display.h>
TM1637Display display(PIN_SCK, PIN_DI);
#endif
#if (SCREEN_TYPE == 4)
#include "TM74HC595Display.h"
TM74HC595Display display(PIN_SCK, PIN_DC, PIN_DI);
unsigned char LED_0F[29];
#endif
// OLED
#if (SCREEN_TYPE == 5)
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h>
#include <SPI.h>
const uint8_t OLED_pin_scl_sck = (int)PIN_SCK;
const uint8_t OLED_pin_sda_mosi = (int)PIN_DI;
const uint8_t OLED_pin_cs_ss = (int)PIN_CS;
const uint8_t OLED_pin_res_rst = (int)PIN_RES;
const uint8_t OLED_pin_dc_rs = (int)PIN_DC;
const uint16_t OLED_Color_Black = 0x0000;
const uint16_t OLED_Color_Blue = 0x001F;
const uint16_t OLED_Color_Red = 0xF800;
const uint16_t OLED_Color_Green = 0x07E0;
const uint16_t OLED_Color_Cyan = 0x07FF;
const uint16_t OLED_Color_Magenta = 0xF81F;
const uint16_t OLED_Color_Yellow = 0xFFE0;
const uint16_t OLED_Color_White = 0xFFFF;
// The colors we actually want to use
uint16_t OLED_Text_Color = OLED_Color_Red;
uint16_t OLED_Backround_Color = OLED_Color_Black;
Adafruit_SSD1331 display = Adafruit_SSD1331(
OLED_pin_cs_ss,
OLED_pin_dc_rs,
OLED_pin_sda_mosi,
OLED_pin_scl_sck,
OLED_pin_res_rst
);
bool isDisplayVisible = false;
int posX = 40;
int posY = 18;
#endif
#if (SCREEN_TYPE == 1||SCREEN_TYPE == 5)
int offsetX = 0;
#endif
unsigned long lastflash;
int RPM;
void setup() {
Serial.begin(9600); //открыть порт
attachInterrupt(0, sens, RISING); //подключить прерывание на 2 пин при повышении сигнала
pinMode(3, OUTPUT); //3 пин как выход питания для датчика Холла. Если датчик запитан от общего источника питания - можно удалить
digitalWrite(3, HIGH); //подать 5 вольт на 3 пин. Если датчик запитан от общего источника питания - можно удалить
#if (SCREEN_TYPE == 1||SCREEN_TYPE == 2)
display.init(); // initialize the lcd
display.backlight();
//display.clear();
#endif
#if (SCREEN_TYPE == 2)
bigNumbersLcd.intNumbers();
#endif
#if (SCREEN_TYPE == 3)
int k;
display.clear();
display.setBrightness(7); //задать яркость экрана
for(k=0; k <= 4; k++) {
display.showNumberDec(8, false, k);
delay(1000);
}
display.setBrightness(2);
#endif
#if (SCREEN_TYPE == 4)
LED_0F[0] = 0xC0; //0
LED_0F[1] = 0xF9; //1
LED_0F[2] = 0xA4; //2
LED_0F[3] = 0xB0; //3
LED_0F[4] = 0x99; //4
LED_0F[5] = 0x92; //5
LED_0F[6] = 0x82; //6
LED_0F[7] = 0xF8; //7
LED_0F[8] = 0x80; //8
LED_0F[9] = 0x90; //9
#endif
#if (SCREEN_TYPE == 5)
// initialise the SSD1331
display.begin();
display.setFont();
display.fillScreen(OLED_Backround_Color);
display.setTextColor(OLED_Text_Color);
display.setTextSize(3);
// the display is now on
isDisplayVisible = true;
#endif
}
void sens() {
RPM = 60 / ((float)(micros() - lastflash) / 1000000); //расчет
RPM = RPM / (int)POLES;
lastflash = micros(); //запомнить время последнего оборота
}
void loop() {
if ((micros() - lastflash) > 1000000) { //если сигнала нет больше секунды
RPM = 0; //считаем что RPM 0
}
Serial.println(RPM); //вывод в порт
#if (SCREEN_TYPE == 1)
if (RPM >= 1000) {
offsetX = 3;
}
else if (RPM >= 100){
offsetX = 2;
}
else if (RPM >= 10) {
offsetX = 1;
}
else {
offsetX = 0;
}
display.clear();
display.setCursor(3,0);
#if (LANG == 1)
display.print("Speed:");
#endif
#if (LANG == 2)
display.print("Обороты:");
#endif
display.setCursor(7-offsetX,1);
display.print(RPM);
display.setCursor(10,1);
#if (LANG == 1)
display.print("1/min");
#endif
#if (LANG == 2)
display.print("об/мин");
#endif
#endif
#if (SCREEN_TYPE == 2)
digit[0] = RPM / 1000;
digit[1] = RPM % 1000 / 100;
digit[2] = RPM % 100 / 10;
digit[3] = RPM % 10;
display.clear();
bigNumbersLcd.printNumber(digit[0], 0);
bigNumbersLcd.printNumber(digit[1], 4);
bigNumbersLcd.printNumber(digit[2], 8);
bigNumbersLcd.printNumber(digit[3], 12);
#endif
#if (SCREEN_TYPE == 3)
//display.clear();
display.showNumberDec(RPM, false); //вывод на дисплей
#endif
#if (SCREEN_TYPE == 4)
display.digit4(RPM, 50);
#endif
#if (SCREEN_TYPE == 5)
if (RPM >= 1000) {
offsetX = 30;
}
else if (RPM >= 100){
offsetX = 20;
}
else if (RPM >= 10) {
offsetX = 10;
}
else {
offsetX = 0;
}
display.fillScreen(OLED_Backround_Color);
display.setCursor(posX-offsetX, posY);
display.print(RPM);
#endif
#if (SCREEN_TYPE == 1||SCREEN_TYPE == 2||SCREEN_TYPE == 5)
delay(1000); //Частота обновления значения на экране
#else
delay(100);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment