-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino-base.ino
113 lines (92 loc) · 2.99 KB
/
arduino-base.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "Libraries/AnalogInput.h"
#include "Libraries/Button.h"
#include "Libraries/FlipflopTimer.h"
#include "Libraries/SerialController.hpp"
#include "Libraries/Timer.h"
SerialController serialController;
long baudRate = 115200;
AnalogInput analogInput1;
Button button1;
FlipflopTimer flipflopTimer1;
Timer timer1;
// Pin assignments
#define analogInput1Pin A0
#define button1Pin 2
#define ledPin 3
#define pwmOutputPin 5
void setup() {
// Enables/disables debug messaging from ArduinoJson
boolean arduinoJsonDebug = false;
// Ensure Serial Port is open and ready to communicate
serialController.setup(baudRate, &onParse);
// For every sketch, we need to set up our IO
// Setup digital pins and default modes as needed, analog inputs are setup by default
pinMode(ledPin, OUTPUT);
pinMode(pwmOutputPin, OUTPUT);
// ANALOG INPUTS
// Parameter 1: pin location
// Parameter 2: enable averaging to get a less constant stream of data
boolean enableAverager = true;
// Parameter 3: the number of samples to average
int averagerSampleRate = 10;
// Parameter 4: enable lowpass filter for Averager to further smooth value
boolean enableLowPass = false;
// Parameter 5: callback
analogInput1.setup(analogInput1Pin, enableAverager, averagerSampleRate, enableLowPass, [](int analogInputValue) {
serialController.sendMessage("analog-input1", analogInputValue);
});
// DIGITAL INPUTS
// Parameter 1: pin location
// Parameter 2: callback
button1.setup(button1Pin, [](int state) {
if (state) {
serialController.sendMessage("button1-press", 1);
if (timer1.isRunning() == false) {
serialController.sendMessage("isTimerRunning", 1);
timer1.start();
}
else {
serialController.sendMessage("postpone", 1);
timer1.postpone(3000);
}
}
});
flipflopTimer1.setup([](boolean flipflopValue) {
digitalWrite(ledPin, flipflopValue);
}, 1000, 500);
timer1.setup([](boolean running, boolean ended, unsigned long timeElapsed) {
if (running == true) {
serialController.sendMessage("timeout-running", timeElapsed);
}
if (ended == true) {
serialController.sendMessage("timeout-ended", timeElapsed);
}
}, 3000);
}
void loop() {
analogInput1.update();
button1.update();
flipflopTimer1.update();
serialController.update();
timer1.update();
}
void onParse(char* message, char* value) {
if (strcmp(message, "led") == 0) {
// Turn-on led
digitalWrite(ledPin, atoi(value));
}
else if (strcmp(message, "pwm-output") == 0 && value >= 0) {
// Set pwm value to pwm pin
analogWrite(pwmOutputPin, atoi(value));
serialController.sendMessage("pwm-set", value);
}
else if (strcmp(message, "pot-rotation") == 0) {
serialController.sendMessage(message, analogInput1.readValue());
}
else if (strcmp(message, "wake-arduino") == 0 && atoi(value) == 1) {
serialController.sendMessage("arduino-ready", 1);
}
else {
serialController.sendMessage("unknown-command", 1);
}
}