aboutsummaryrefslogtreecommitdiff
path: root/libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.pde
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.pde')
-rw-r--r--libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.pde35
1 files changed, 35 insertions, 0 deletions
diff --git a/libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.pde b/libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.pde
new file mode 100644
index 0000000..b505b33
--- /dev/null
+++ b/libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.pde
@@ -0,0 +1,35 @@
+/* Supports as many analog inputs and analog PWM outputs as possible.
+ *
+ * This example code is in the public domain.
+ */
+#include <Firmata.h>
+
+byte analogPin = 0;
+
+void analogWriteCallback(byte pin, int value)
+{
+ if (IS_PIN_PWM(pin)) {
+ pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
+ analogWrite(PIN_TO_PWM(pin), value);
+ }
+}
+
+void setup()
+{
+ Firmata.setFirmwareVersion(0, 1);
+ Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
+ Firmata.begin(57600);
+}
+
+void loop()
+{
+ while(Firmata.available()) {
+ Firmata.processInput();
+ }
+ // do one analogRead per loop, so if PC is sending a lot of
+ // analog write messages, we will only delay 1 analogRead
+ Firmata.sendAnalog(analogPin, analogRead(analogPin));
+ analogPin = analogPin + 1;
+ if (analogPin >= TOTAL_ANALOG_PINS) analogPin = 0;
+}
+