Project

General

Profile

Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.09 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
/* This firmware supports as many analog ports as possible, all analog inputs,
13
 * four PWM outputs, and two with servo support.
14
 *
15
 * This example code is in the public domain.
16
 */
17
#include <Servo.h>
18
#include <Firmata.h>
19

    
20
/*==============================================================================
21
 * GLOBAL VARIABLES
22
 *============================================================================*/
23

    
24
/* servos */
25
Servo servo9, servo10; // one instance per pin
26
/* analog inputs */
27
int analogInputsToReport = 0; // bitwise array to store pin reporting
28
int analogPin = 0; // counter for reading analog pins
29
/* timer variables */
30
unsigned long currentMillis;     // store the current value from millis()
31
unsigned long previousMillis;    // for comparison with currentMillis
32

    
33

    
34
/*==============================================================================
35
 * FUNCTIONS                                                                
36
 *============================================================================*/
37

    
38
void analogWriteCallback(byte pin, int value)
39
{
40
    switch(pin) {
41
    case 9: servo9.write(value); break;
42
    case 10: servo10.write(value); break;
43
    case 3: 
44
    case 5: 
45
    case 6: 
46
    case 11: // PWM pins
47
        analogWrite(pin, value); 
48
        break;
49
    }
50
}
51
// -----------------------------------------------------------------------------
52
// sets bits in a bit array (int) to toggle the reporting of the analogIns
53
void reportAnalogCallback(byte pin, int value)
54
{
55
    if(value == 0) {
56
        analogInputsToReport = analogInputsToReport &~ (1 << pin);
57
    }
58
    else { // everything but 0 enables reporting of that pin
59
        analogInputsToReport = analogInputsToReport | (1 << pin);
60
    }
61
    // TODO: save status to EEPROM here, if changed
62
}
63

    
64
/*==============================================================================
65
 * SETUP()
66
 *============================================================================*/
67
void setup() 
68
{
69
    Firmata.setFirmwareVersion(0, 2);
70
    Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
71
    Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
72

    
73
    servo9.attach(9);
74
    servo10.attach(10);
75
    Firmata.begin(57600);
76
}
77

    
78
/*==============================================================================
79
 * LOOP()
80
 *============================================================================*/
81
void loop() 
82
{
83
    while(Firmata.available())
84
        Firmata.processInput();
85
    currentMillis = millis();
86
    if(currentMillis - previousMillis > 20) {  
87
        previousMillis += 20;                   // run this every 20ms
88
        for(analogPin=0;analogPin<TOTAL_ANALOG_PINS;analogPin++) {
89
            if( analogInputsToReport & (1 << analogPin) ) 
90
                Firmata.sendAnalog(analogPin, analogRead(analogPin));
91
        }
92
    }
93
}
94