Skip to content

Instantly share code, notes, and snippets.

@chawasit
Created April 19, 2020 19:16
Show Gist options
  • Select an option

  • Save chawasit/fe60cb48cdb3e168f324efe37f673bc1 to your computer and use it in GitHub Desktop.

Select an option

Save chawasit/fe60cb48cdb3e168f324efe37f673bc1 to your computer and use it in GitHub Desktop.
#include <max6675.h> // Modified version of Adafruit's max6675
#include "LedController.hpp" // LCD Control library
#include <ModbusRtu.h> // Modbus communication
#include <Smoothed.h> // Smooth sensor value
// basic setting
#define DEBUG false
#define LED_DIN 32
#define LED_CS 33
#define LED_CLK 25
#define THERMO_SCK 26
#define THERMO_CS 27
#define THERMO_SO 14
#define HALL_SIGNAL 35
#define LED 16
const unsigned long delaytime = 100;
/* holding register for modbus
* index 0 for temperature
* index 1 for drum speed
*/
uint16_t au4data[4] = {0, 0, 0, -1};
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus slave(1, Serial, 0); // this is slave @1 and RS-232 or USB-FTDI
LedController lc(LED_DIN, LED_CLK, LED_CS, 1);
MAX6675 thermocouple(THERMO_SCK, THERMO_CS, THERMO_SO);
Smoothed <double> temperature;
Smoothed <double> drumSpeed;
void setup() {
// Start serial and modbus
Serial.begin( 115200, SERIAL_8E1); // 115200 baud, 8-bits, even, 1-bit stop
slave.setTimeOut( 1000 );
Serial.flush();
slave.start();
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.activateAllSegments();
/* Set the brightness to a medium values */
lc.setIntensity(8);
/* and clear the display */
lc.clearMatrix();
// setup smoothed sensor data
temperature.begin(SMOOTHED_AVERAGE, 4);
drumSpeed.begin(SMOOTHED_AVERAGE, 4);
// LED
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
// Hall Sensor
pinMode(HALL_SIGNAL, INPUT);
}
double readTemperature() {
double value = thermocouple.readCelsius();
return value;
}
int readHallSensor() {
return analogRead(HALL_SIGNAL);
}
// temp drum
// led [7 6 5 4][3 2 1 0]
void renderTemperature(double temperature) {
int value = temperature * 10;
char a = '0' + (value / 1000);
char b = '0' + ((value % 1000) / 100);
char c = '0' + ((value % 100) / 10);
char d = '0' + value % 10;
a = a == '0' ? ' ': a;
b = (b == '0' && a == ' ') ? ' ': b;
// c = (c == '0' && b == ' ') ? ' ': c;
lc.setChar(0, 7, a, false);
lc.setChar(0, 6, b, false);
lc.setChar(0, 5, c, true);
lc.setChar(0, 4, d, false);
}
void renderDrumSpeed(double rpm) {
int value = rpm * 10;
char a = '0' + (value / 1000);
char b = '0' + ((value % 1000) / 100);
char c = '0' + ((value % 100) / 10);
char d = '0' + value % 10;
a = a == '0' ? ' ': a;
b = (b == '0' && a == ' ') ? ' ': b;
// c = (c == '0' && b == ' ') ? ' ': c;
lc.setChar(0, 3, a, false);
lc.setChar(0, 2, b, false);
lc.setChar(0, 1, c, true);
lc.setChar(0, 0, d, false);
}
unsigned int lastSerial = millis();
unsigned int lastHallSensorRead = millis();
unsigned int lastDetected = millis();
unsigned int lastReadSensor = millis();
unsigned int hallCount = 0;
const bool DETECTED = true;
const bool UNDETECTED = false;
bool hallState = UNDETECTED;
void loop() {
if (readHallSensor() < 200 and millis() - lastHallSensorRead > 25 and hallState == UNDETECTED) {
hallState = DETECTED;
double drumCurrentFrequency = 30000 / (millis() - lastDetected);
drumSpeed.add(drumCurrentFrequency);
lastHallSensorRead = millis();
lastDetected = millis();
} else if (readHallSensor() > 200 and millis() - lastHallSensorRead > 25) {
hallState = UNDETECTED;
lastHallSensorRead = millis();
}
if (millis() - lastReadSensor >= 250) {
lastReadSensor = millis();
temperature.add(readTemperature());
// display
renderTemperature(temperature.get());
renderDrumSpeed(drumSpeed.get());
}
// poll modbus
au4data[0] = (uint16_t) temperature.get();
au4data[1] = (uint16_t) drumSpeed.get();
slave.poll(au4data, 4);
digitalWrite(LED, HIGH);
delay(25);
digitalWrite(LED, LOW);
if (DEBUG and millis() - lastSerial >= 1000) {
lastSerial = millis();
Serial.print("Raw Temperature: ");
Serial.print(readTemperature());
Serial.print(", Raw Hall: ");
Serial.print(readHallSensor());
Serial.println();
Serial.print("Temperature: ");
Serial.print(temperature.get());
Serial.print(", Drum: ");
Serial.print(drumSpeed.get());
Serial.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment