This is the source code for an Arduino sketch dv_ard.ino that interacts with DataView to allow up to 4-channels of DC-coupled AD conversion. Download this sketch to the Arudino board using the Arduino IDE before you start DataView.
Uncomment the line //#define TEST if you want to run the sketch in test mode. In this mode the board returns predefined waveforms to DataView, so that you can test the facility without wiring up an analogue data source. The waveforms are a full-scale sawtooth with a spike (channel 1), a rectified sine wave (channel 2), a uniform random number (channel 3) and a fixed number (channel 4). Look at the source code of the sketch for further details.
#include
//#define TEST
volatile unsigned int nChan = 1; // use volatile for shared variables
#ifdef TEST
volatile unsigned int nFlag = 0; // use volatile for shared variables
#endif
void setup() {
Timer1.initialize(500); // 2 kHz
Timer1.stop();
Timer1.attachInterrupt(SendNum);
Serial.begin(256000);
}
void loop() {
if (Serial.available())
{
char c = Serial.read();
Timer1.stop();
if (c >= '1' && c <= '4') // set number of channels (receive txt 1-4)
{
nChan = c - '0';
return;
}
Serial.write(4); // return flag indicating received start code
Serial.write(4);
delay(10);
#ifdef TEST
nFlag = 0;
#endif
switch (c) // set ADC rate with txt a-l, and start
{
case 'a':
Timer1.setPeriod(1000000);
break;
case 'b':
Timer1.setPeriod(500000);
break;
case 'c':
Timer1.setPeriod(200000);
break;
case 'd':
Timer1.setPeriod(100000);
break;
case 'e':
Timer1.setPeriod(50000);
break;
case 'f':
Timer1.setPeriod(20000);
break;
case 'g':
Timer1.setPeriod(10000);
break;
case 'h':
Timer1.setPeriod(5000);
break;
case 'i':
Timer1.setPeriod(2000);
break;
case 'j':
Timer1.setPeriod(1000);
break;
case 'k':
Timer1.setPeriod(500);
break;
case 'l':
Timer1.setPeriod(200);
break;
case 'z': // stop recording
default:
Timer1.stop();
#ifdef TEST
nFlag = 0;
#endif
break;
}
}
}
// interrupt routine
// read AD value and send as 2-byte binary
#ifdef TEST
void SendNum(void)
{
if (nFlag == 512)
{
nFlag = 1023;
Serial.write(lowByte(nFlag));
Serial.write(highByte(nFlag));
nFlag = 512;
}
else
{
Serial.write(lowByte(nFlag));
Serial.write(highByte(nFlag));
}
if (nChan > 1)
{
unsigned int v = 1000*abs(sin(nFlag*0.01745329));
Serial.write(lowByte(v));
Serial.write(highByte(v));
}
if (nChan > 2)
{
unsigned int v = 500;
Serial.write(lowByte(v));
Serial.write(highByte(v));
}
if (nChan > 3)
{
unsigned int v = random(200, 600);
Serial.write(lowByte(v));
Serial.write(highByte(v));
}
++nFlag;
nFlag %= 1024;
}
#else
void SendNum(void)
{
unsigned int v = analogRead(A0);
Serial.write(lowByte(v));
Serial.write(highByte(v));
if (nChan > 1)
{
unsigned int v = analogRead(A1);
Serial.write(lowByte(v));
Serial.write(highByte(v));
}
if (nChan > 2)
{
unsigned int v = analogRead(A2);
Serial.write(lowByte(v));
Serial.write(highByte(v));
}
if (nChan > 3)
{
unsigned int v = analogRead(A3);
Serial.write(lowByte(v));
Serial.write(highByte(v));
}
}
#endif