Add uOLED-96-G1 C Sample Code.
[frickel.git] / projects / geeknamensschilder_c / hardware / libraries / Firmata / examples / AnalogFirmata / AnalogFirmata.pde
blobab83726898837a2f3ec9c5f3a96c8b53e51fe591
1 /* This firmware supports as many analog ports as possible, all analog inputs,
2  * four PWM outputs, and two with servo support.
3  *
4  * This example code is in the public domain.
5  */
6 #include <Firmata.h>
7 #include <Servo.h>
9 /*==============================================================================
10  * GLOBAL VARIABLES
11  *============================================================================*/
13 /* servos */
14 Servo servo9, servo10; // one instance per pin
15 /* analog inputs */
16 int analogInputsToReport = 0; // bitwise array to store pin reporting
17 int analogPin = 0; // counter for reading analog pins
18 /* timer variables */
19 unsigned long currentMillis;     // store the current value from millis()
20 unsigned long nextExecuteMillis; // for comparison with currentMillis
23 /*==============================================================================
24  * FUNCTIONS                                                                
25  *============================================================================*/
27 void analogWriteCallback(byte pin, int value)
29     switch(pin) {
30     case 9: servo9.write(value); break;
31     case 10: servo10.write(value); break;
32     case 3: 
33     case 5: 
34     case 6: 
35     case 11: // PWM pins
36         analogWrite(pin, value); 
37         break;
38     }
40 // -----------------------------------------------------------------------------
41 // sets bits in a bit array (int) to toggle the reporting of the analogIns
42 void reportAnalogCallback(byte pin, int value)
44     if(value == 0) {
45         analogInputsToReport = analogInputsToReport &~ (1 << pin);
46     }
47     else { // everything but 0 enables reporting of that pin
48         analogInputsToReport = analogInputsToReport | (1 << pin);
49     }
50     // TODO: save status to EEPROM here, if changed
53 /*==============================================================================
54  * SETUP()
55  *============================================================================*/
56 void setup() 
58     Firmata.setFirmwareVersion(0, 2);
59     Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
60     Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
62     servo9.attach(9);
63     servo10.attach(10);
64     Firmata.begin(57600);
67 /*==============================================================================
68  * LOOP()
69  *============================================================================*/
70 void loop() 
72     while(Firmata.available())
73         Firmata.processInput();
74     currentMillis = millis();
75     if(currentMillis > nextExecuteMillis) {  
76         nextExecuteMillis = currentMillis + 19; // run this every 20ms
77         for(analogPin=0;analogPin<TOTAL_ANALOG_PINS;analogPin++) {
78             if( analogInputsToReport & (1 << analogPin) ) 
79                 Firmata.sendAnalog(analogPin, analogRead(analogPin));
80         }
81     }