Project

General

Profile

Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / Firmata / examples / SimpleAnalogFirmata / SimpleAnalogFirmata.ino @ 58d82c77

History | View | Annotate | Download (1.17 KB)

1
/*
2
 * Firmata is a generic protocol for communicating with microcontrollers
3
 * from software on a host computer. It is intended to work with
4
 * any host computer software package.
5
 *
6
 * To download a host software package, please clink on the following link
7
 * to open the download page in your default browser.
8
 *
9
 * http://firmata.org/wiki/Download
10
 */
11

    
12
/* Supports as many analog inputs and analog PWM outputs as possible.
13
 *
14
 * This example code is in the public domain.
15
 */
16
#include <Firmata.h>
17

    
18
byte analogPin = 0;
19

    
20
void analogWriteCallback(byte pin, int value)
21
{
22
    if (IS_PIN_PWM(pin)) {
23
        pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
24
        analogWrite(PIN_TO_PWM(pin), value);
25
    }
26
}
27

    
28
void setup()
29
{
30
    Firmata.setFirmwareVersion(0, 1);
31
    Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
32
    Firmata.begin(57600);
33
}
34

    
35
void loop()
36
{
37
    while(Firmata.available()) {
38
        Firmata.processInput();
39
    }
40
    // do one analogRead per loop, so if PC is sending a lot of
41
    // analog write messages, we will only delay 1 analogRead
42
    Firmata.sendAnalog(analogPin, analogRead(analogPin)); 
43
    analogPin = analogPin + 1;
44
    if (analogPin >= TOTAL_ANALOG_PINS) analogPin = 0;
45
}
46