Last active
August 29, 2015 13:58
-
-
Save Hardolaf/10325758 to your computer and use it in GitHub Desktop.
Arduino Yun 40 to 50 channel data acquisition system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
40 to 50 channel data acquisition | |
TODO: Detection of optional channels 41 to 50 | |
TODO: Collect only when data is incoming | |
created 09 April 2014 | |
by Joseph Warner | |
*/ | |
#include <stdio.h> | |
#include <digitalWriteFast.h> | |
// Define the pins on the MUX from lsb | |
#define MUX_PIN_1 2 | |
#define MUX_PIN_2 3 | |
#define MUX_PIN_3 4 | |
#define MUX_PIN_4 5 | |
#define MUX_PIN_5 6 | |
#define MUX_PIN_6 7 | |
// Define the ADC pins from LSB | |
#define ADC_PIN_1 8 | |
#define ADC_PIN_2 9 | |
#define ADC_PIN_3 10 | |
#define ADC_PIN_4 11 | |
// Defines the channels available and their addresses (offset by -1) | |
#define REQUIRED_CHANNELS 39 | |
#define CHANNEL_41 40 | |
#define CHANNEL_42 41 | |
#define CHANNEL_43 42 | |
#define CHANNEL_44 43 | |
#define CHANNEL_45 44 | |
#define CHANNEL_46 45 | |
#define CHANNEL_47 46 | |
#define CHANNEL_48 47 | |
#define CHANNEL_49 48 | |
#define CHANNEL_50 49 | |
// Current channel we're getting data from | |
unsigned int addr = 0x0; | |
// Time of the collection period (us) | |
unsigned int collectionPeriod = 100*1000; | |
// Array of the 10 optional channels describing if they are or are not | |
// in use. | |
unsigned int channels[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
void setup() { | |
// Set MUX pins as output pins | |
pinModeFast(MUX_PIN_1, OUTPUT); | |
pinModeFast(MUX_PIN_2, OUTPUT); | |
pinModeFast(MUX_PIN_3, OUTPUT); | |
pinModeFast(MUX_PIN_4, OUTPUT); | |
pinModeFast(MUX_PIN_5, OUTPUT); | |
pinModeFast(MUX_PIN_6, OUTPUT); | |
// Set ADC pins as input pins | |
pinModeFast(ADC_PIN_1, INPUT); | |
pinModeFast(ADC_PIN_2, INPUT); | |
pinModeFast(ADC_PIN_3, INPUT); | |
pinModeFast(ADC_PIN_4, INPUT); | |
// Begin serial access | |
Serial.begin(9600); | |
while(!Serial); | |
Serial.println("Serial data logger started\n"); | |
} | |
void loop() { | |
unsigned int i = 0; | |
unsigned int data = 0; | |
char dataOutputBuffer[20]; | |
// Reset addr to 0 | |
addr = 0; | |
writeAddrToMux(); | |
//Determine the channels to use (1-40 + which optional?) | |
detectChannelsInUse(); | |
// Start timing | |
unsigned long startTime = micros(); | |
unsigned long currentTime = micros() - startTime; | |
while (currentTime > collectionPeriod) { | |
// Format the data from this collection cycle | |
sprintf(dataOutputBuffer, "%08u;%06x;%04x",\ | |
currentTime, addr, getDataFromAdc()); | |
// Have MUX move to the next address to allow time for hardware changes | |
nextAddrToMux(); | |
Serial.println(dataOutputBuffer); | |
currentTime = micros() - startTime; | |
} | |
} | |
// Gets data from the ADC | |
unsigned int getDataFromAdc() { | |
unsigned int data = 0x0; | |
data ^= digitalReadFast(ADC_PIN_1); | |
data ^= (digitalReadFast(ADC_PIN_2) << 1); | |
data ^= (digitalReadFast(ADC_PIN_3) << 2); | |
data ^= (digitalReadFast(ADC_PIN_4) << 3); | |
return data; | |
} | |
// Determine which channels are connected and enable data collection | |
// from them. | |
void detectChannelsInUse() { | |
// TODO make functional. Currently placeholder. | |
channels[0] = 0; | |
channels[1] = 0; | |
channels[2] = 0; | |
channels[3] = 0; | |
channels[4] = 0; | |
channels[5] = 0; | |
channels[6] = 0; | |
channels[7] = 0; | |
channels[8] = 0; | |
channels[9] = 0; | |
} | |
// Increments the MUX to the next address | |
void nextAddrToMux() { | |
if (addr < REQUIRED_CHANNELS) { | |
addr++; | |
} else if(channels[0] == 1 && addr < CHANNEL_41) { | |
addr = CHANNEL_41; | |
} else if(channels[1] == 1 && addr < CHANNEL_42) { | |
addr = CHANNEL_42; | |
} else if(channels[2] == 1 && addr < CHANNEL_43) { | |
addr = CHANNEL_43; | |
} else if(channels[3] == 1 && addr < CHANNEL_44) { | |
addr = CHANNEL_44; | |
} else if(channels[4] == 1 && addr < CHANNEL_45) { | |
addr = CHANNEL_45; | |
} else if(channels[5] == 1 && addr < CHANNEL_46) { | |
addr = CHANNEL_46; | |
} else if(channels[6] == 1 && addr < CHANNEL_47) { | |
addr = CHANNEL_47; | |
} else if(channels[7] == 1 && addr < CHANNEL_48) { | |
addr = CHANNEL_48; | |
} else if(channels[8] == 1 && addr < CHANNEL_49) { | |
addr = CHANNEL_49; | |
} else if(channels[9] == 1 && addr < CHANNEL_50) { | |
addr = CHANNEL_50; | |
} else { | |
addr = 0x0; | |
} | |
writeAddrToMux(); | |
} | |
// Writes the current addr to the MUX | |
void writeAddrToMux() { | |
digitalWriteFast(MUX_PIN_1, addr & 1); | |
digitalWriteFast(MUX_PIN_2, addr & (1 << 1)); | |
digitalWriteFast(MUX_PIN_3, addr & (1 << 2)); | |
digitalWriteFast(MUX_PIN_4, addr & (1 << 3)); | |
digitalWriteFast(MUX_PIN_5, addr & (1 << 4)); | |
digitalWriteFast(MUX_PIN_6, addr & (1 << 5)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment