Rotary-Dial/examples/dialtoserial/dialtoserial.ino

170 lines
3.5 KiB
Arduino
Raw Normal View History

2018-04-26 21:45:30 +00:00
#include <Homie.h>
#include <ArduinoOTA.h>
2018-04-26 16:10:33 +00:00
2018-04-26 21:10:18 +00:00
#include "RotaryDialer.h"
2018-04-26 16:10:33 +00:00
2018-04-26 21:10:18 +00:00
#define PIN_READY D6
#define PIN_PULSE D5
2018-04-26 21:45:30 +00:00
#define FW_NAME "esp-waehlscheibe"
#define FW_VERSION "1.0.0"
2018-07-03 21:12:19 +00:00
#define MINMAP 0
#define MAXMAP 500
2018-04-26 16:10:33 +00:00
2018-04-30 23:06:59 +00:00
HomieNode dialNode("dial", "dialnode");
2018-04-26 16:10:33 +00:00
RotaryDialer dialer = RotaryDialer(PIN_READY, PIN_PULSE);
2018-04-30 23:06:59 +00:00
bool meterHandler(const HomieRange& range, const String& value) {
Homie.getLogger() << "meter " << ": " << value << endl;
int iValue = value.toInt();
2018-07-03 21:12:19 +00:00
// if ((iValue >= 0) && (iValue <=9000))
if ((iValue >= 0) && (iValue <=30000))
2018-04-30 23:06:59 +00:00
{
setMeter(value.toInt());
dialNode.setProperty("meter").send(value);
return true;
} else {
return false;
}
}
bool numberHandler(const HomieRange& range, const String& value) {
Homie.getLogger() << "number " << ": " << value << endl;
int iValue = value.toInt();
2018-07-03 21:12:19 +00:00
if ((iValue >= 0) && (iValue <=10))
{
setNumber(value.toInt());
dialNode.setProperty("number").send(value);
return true;
} else {
return false;
}
}
2018-07-03 21:12:19 +00:00
bool rawHandler(const HomieRange& range, const String& value) {
Homie.getLogger() << "raw " << ": " << value << endl;
int iValue = value.toInt();
if ((iValue >= 0) && (iValue <=1024))
{
setRaw(value.toInt());
dialNode.setProperty("raw").send(value);
return true;
} else {
return false;
}
}
2018-04-26 16:10:33 +00:00
void setup() {
2018-04-26 21:10:18 +00:00
Serial.begin(115200);
2018-04-26 16:10:33 +00:00
dialer.setup();
2018-04-26 21:45:30 +00:00
Homie_setFirmware(FW_NAME, FW_VERSION);
Homie_setBrand(FW_NAME);
dialNode.advertise("dialed");
2018-04-30 23:06:59 +00:00
dialNode.advertise("meter").settable(meterHandler);
dialNode.advertise("number").settable(numberHandler);
2018-07-03 21:12:19 +00:00
dialNode.advertise("raw").settable(rawHandler);
2018-04-26 21:45:30 +00:00
Homie.setLoopFunction(loopHandler);
Homie.setup();
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.begin();
}
void loopHandler()
{
2018-04-26 16:10:33 +00:00
}
void loop() {
2018-04-26 21:45:30 +00:00
Homie.loop();
ArduinoOTA.handle();
2018-04-26 16:10:33 +00:00
if (dialer.update()) {
2018-04-26 21:45:30 +00:00
int ivalue = dialer.getNextNumber();
// Serial.println(dialer.getNextNumber());
dialNode.setProperty("dialed").send(String(ivalue));
setNumber(ivalue);
2018-04-26 16:10:33 +00:00
}
}
2018-04-30 23:06:59 +00:00
2018-07-03 21:12:19 +00:00
void setRaw(int pos){
analogWrite(D1,pos);
}
2018-04-30 23:06:59 +00:00
void setMeter(int pos){
2018-07-03 21:12:19 +00:00
// analogWrite(D1,map(pos,0,100,MINMAP,MAXMAP));
// Range: 0 - 9000
// Output: 0 - 495
if (pos<1000) {
analogWrite(D1,map(pos,0,1000,0,75));
return;
}
if (pos<2000) {
analogWrite(D1,map(pos,1000,2000,75,125));
return;
}
if (pos<3000) {
analogWrite(D1,map(pos,2000,3000,125,175));
return;
}
if (pos<4000) {
analogWrite(D1,map(pos,3000,4000,175,225));
return;
}
if (pos<5000) {
analogWrite(D1,map(pos,4000,5000,225,270));
return;
}
if (pos<6000) {
analogWrite(D1,map(pos,5000,6000,270,330));
return;
}
if (pos<7000) {
analogWrite(D1,map(pos,6000,7000,330,380));
return;
}
if (pos<8000) {
analogWrite(D1,map(pos,7000,8000,380,427));
return;
}
if (pos<9000) {
analogWrite(D1,map(pos,8000,9000,427,495));
return;
}
analogWrite(D1,495);
2018-04-30 23:06:59 +00:00
}
void setNumber(int pos){
2018-07-03 21:12:19 +00:00
// analogWrite(D1,map(pos,0,9,MINMAP,MAXMAP));
switch (pos){
case 0:
analogWrite(D1,0);
break;
case 1:
analogWrite(D1,75);
break;
case 2:
analogWrite(D1,125);
break;
case 3:
analogWrite(D1,175);
break;
case 4:
analogWrite(D1,225);
break;
case 5:
analogWrite(D1,270);
break;
case 6:
analogWrite(D1,330);
break;
case 7:
analogWrite(D1,380);
break;
case 8:
analogWrite(D1,427);
break;
case 9:
analogWrite(D1,495);
break;
}
}