Attachment 'ADCNano3.ino'
Download 1 /*
2 ADCNano3
3 Reads on TWI/I2C request analog value
4 For Nano3, clock @ 16 MHz
5 Mac OS X device: /dev/tty.usbserial-A900FYDY
6
7 This example code is in the public domain.
8 2013-12-06 RR
9 */
10
11 #include <Wire.h>
12 #include "stdio.h"
13 //#include "stdlib.h"
14
15 // Pin 13 has an LED connected on most Arduino boards.
16 int led = 13; // on ATtiny25 (D)4, pin 3
17
18 int analogPin = 3; // AD8307
19
20 int aval_i = 0; // analog value input
21 int aval_s = 0; // analog sum
22 int aval = 0; // analog value
23 int achanin = 0; // analog channel in
24 char buff[30]; // for debug sprintf
25
26 #define avg_num 8 // average number, max 30
27 int avg_cnt = 0; // average count
28
29 //********************* send debug string to UART1
30 void com_print(char *daten) {
31 while( *daten) { Serial.write(*daten++); }
32 }
33
34 // function that executes whenever data is received from master
35 // this function is registered as an event, see setup()
36 void receiveEvent(int howMany) {
37 while(1 < Wire.available()) { // loop through all but the last
38 char c = Wire.read(); // receive byte as a character
39 //Serial.println(c); // print the character
40 }
41 achanin = Wire.read(); // receive byte as an integer
42 //Serial.println(achanin, DEC); // print the integer
43 }
44
45 // function that executes whenever data is requested by master
46 // this function is registered as an event, see setup()
47 // if you do not use the "length" parameter, you could not trasmit "00".
48 void requestEvent() {
49 byte bytes[2];
50 bytes[0] = aval >> 8; // upper byte
51 bytes[1] = aval & 0xFF; // lower byte
52 Wire.write(bytes, 2);
53 }
54
55 // the setup routine runs once when you press reset:
56 void setup() {
57 Serial.begin(38400); // for debug only
58 Wire.begin(5); // slave address
59 Wire.onReceive(receiveEvent); // register event
60 Wire.onRequest(requestEvent); // register event
61 pinMode(led, OUTPUT); // initialize the digital pin as an output.
62 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
63 delay(200); // wait for 100 ms
64 digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
65 delay(100);
66 //Serial.println("Hello");
67 }
68
69 // the loop routine runs over and over again forever:
70 void loop() {
71 //int pause_ms = 1000;
72
73 // start A/D convertion with a I2C master request
74 if (achanin == 2 || achanin ==3) {
75 analogPin = achanin;
76 achanin = 0; // ACK master command
77 // average voltage
78 aval_s = 0;
79 for (avg_cnt = 0; avg_cnt < avg_num; avg_cnt++) {
80 aval_i = analogRead(analogPin); // read the input pin
81 aval_s += aval_i;
82 }
83 aval = aval_s / avg_num;
84 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage)
85 digitalWrite(led, LOW); // EOC for convewrsion time measurement
86 //Serial.println(aval_3, DEC);
87 //sprintf(buff,"2 %d, 3 %d\r\n", aval_2, aval_3);
88 //com_print(buff);
89 }
90 //delay(pause_ms);
91 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.