Upload New File
This commit is contained in:
773
TruehemeV2.1.ino
Normal file
773
TruehemeV2.1.ino
Normal file
@@ -0,0 +1,773 @@
|
|||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/////////////////////////////////////////////////////////////////////TRUEHEME CODE V0.1.4 - BETA Version (To be Tested)/////////////////////////////////////////////////////////////////////
|
||||||
|
//////////NEW Changes: All LED warmup function Added; Limit Switch added;EEPROM storage of DAC values; Stray light sensing; Turing on any one LED///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#include "ADS1X15.h"
|
||||||
|
#include <EEPROM.h>
|
||||||
|
#include <SPI.h>
|
||||||
|
#include <Wire.h>
|
||||||
|
/////////////////////////////////////////////////////////////////////PLEASE UPDATE DEVICE ID BEFORE UPLOADING TO ANY DEVICE/////////////////////////////////////////////////////////////////
|
||||||
|
String Device_ID = "SNS HCV-00X-6XXX SNE"; // Device ID SNS HPP1-000-5001 SNE
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
const int LED_available[] = {1,2,3,4}; // List of LEDs available. All Variables with 4 columns (other than ones controlling the LED turning ON sequence) must be filled in this order
|
||||||
|
int LED_sequence[] = {1,2,3,4}; // LED turning ON sequence
|
||||||
|
int Detector_sequence[] = {1,1,1,1}; // Detector sequence (if hardware supports dual pathlength)
|
||||||
|
int LED_dac[] = {1792,2304,2048,1920}; // Preset DAC values for the device
|
||||||
|
int LED_sample_size[] = {50,50,50,50}; // No. of ADC readings to use for Average ADC calculation
|
||||||
|
int LED_DS_threshold[] = {22000,18000,18000,22000}; // DAC sweep upperlimit for each LED
|
||||||
|
int LED_settling_time[] = {20000,20000,20000,20000}; // Settling time given to each LED
|
||||||
|
int ADC_reading_delay[] = {0,0,0,0}; // Delay for each ADC reading while averaging
|
||||||
|
int led_warmup_DAC[] = {3000,3000,3000,3000}; // DAC value at which all the LEDs will be powered during LED warmup
|
||||||
|
int DAC_Sweep_start_limit[] = {1000,1000,1000,1000}; // Lower limit for DAC sweep
|
||||||
|
int DAC_Sweep_end_limit[] = {4000,4000,4000,4000}; // Upper limit for DAC sweep
|
||||||
|
int DacSweep_gain_combinations[] = {0,0,0,0}; // Gain to be used for DAC sweep
|
||||||
|
|
||||||
|
int incr=8; // DAC sweep increment
|
||||||
|
int del=1000; // Delay between each interval of the DAC sweep
|
||||||
|
int appmode=1; // if =1, it removes most of the unwanted serial prints
|
||||||
|
int raw_ADC_print=0; // if =1, it will serialprint raw ADC values (even during LED warmup and settling)
|
||||||
|
int timestamp_printer=0; // if =1, will print timestamp at regular intervals
|
||||||
|
int limit_switch_active=0; // If there is a limit switch in the device, make it 1
|
||||||
|
|
||||||
|
int Blank_gain_combinations[] = {0,0,0,0}; // Programmable gain to be used while blanking
|
||||||
|
int Sample_gain_combinations[5][4] = {{0,0,0,0},{1,1,1,1},{2,2,2,2},{4,4,4,4},{8,8,8,8}}; // Programmable gain to be used while taking sample readings
|
||||||
|
|
||||||
|
///////////////////////////////////////////DO NOT CHANGE ANY VALUES BELOW //////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
const int PIN_CS_DAC1 = 10; // Chip select pin for DAC 1
|
||||||
|
const int PIN_CS_DAC2 = 9; // Chip select pin for DAC 2
|
||||||
|
const int GAIN_1 = 0x1; // DAC 1X gain (output between 0 - 2.048 Volts)
|
||||||
|
const int GAIN_2 = 0x0; // DAC 2X gain (output between 0 - 4.096 Volts)
|
||||||
|
|
||||||
|
const int PD1G1 = 6; // Programmable gain resistor _______ X gain
|
||||||
|
const int PD1G2 = 8; // Programmable gain resistor _______ X gain
|
||||||
|
const int PD2G1 = 5; // Programmable gain resistor _______ X gain
|
||||||
|
const int PD2G2 = 7; // Programmable gain resistor _______ X gain
|
||||||
|
|
||||||
|
const int LEDR = A2; // Red indicator LED
|
||||||
|
const int LEDB = A0; // Blue indicator LED
|
||||||
|
const int LEDG = A1; // Green indicator LED
|
||||||
|
|
||||||
|
const int Limit_Switch = 2; // Limit switch for cuvette sensing
|
||||||
|
const int Buzzer_pin = 3; // Buzzer connection for alert
|
||||||
|
|
||||||
|
ADS1115 ADS(0x48); //
|
||||||
|
const char compile_date[] = __DATE__ " " __TIME__; // Stores date and time when the firmware was uploaded on the arduino
|
||||||
|
String Firmware_location_Auto =__FILE__; // Stores location of the firmware on the computer used for uploading
|
||||||
|
String Firmware_version_Auto =__FILE__; // Stores the firmware version uploaded
|
||||||
|
///////////////////////////////////////////DO NOT CHANGE ANY VALUES ABOVE //////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
int DAC_lower_limit[] = {0,0,0,0}; //
|
||||||
|
int DAC_upper_limit[] = {4096,4096,4096,4096}; //
|
||||||
|
unsigned long long intensity_storage = 0; //
|
||||||
|
int temp_counter = 0; //
|
||||||
|
const int detector_settling_time = 2000; //
|
||||||
|
const int detector_settling_time_DACsweep = 1000;
|
||||||
|
int initialRGBstate = 0;
|
||||||
|
|
||||||
|
float average_readings = 0;
|
||||||
|
float avg_blank[] = {0,0,0,0};
|
||||||
|
float avg_sample[] = {0,0,0,0};
|
||||||
|
int LED_on_status[] = {0,0,0,0};
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Serial.begin(9600);
|
||||||
|
pinMode(PIN_CS_DAC1, OUTPUT);
|
||||||
|
pinMode(PIN_CS_DAC2, OUTPUT);
|
||||||
|
pinMode(PD1G1, OUTPUT);
|
||||||
|
pinMode(PD1G2, OUTPUT);
|
||||||
|
pinMode(PD2G1, OUTPUT);
|
||||||
|
pinMode(PD2G2, OUTPUT);
|
||||||
|
pinMode(LEDR, OUTPUT);
|
||||||
|
pinMode(LEDG, OUTPUT);
|
||||||
|
pinMode(LEDB, OUTPUT);
|
||||||
|
pinMode(Limit_Switch, INPUT);
|
||||||
|
pinMode(Buzzer_pin, OUTPUT);
|
||||||
|
digitalWrite(LEDR, LOW);
|
||||||
|
digitalWrite(LEDG, HIGH);
|
||||||
|
digitalWrite(LEDB, HIGH);
|
||||||
|
SPI.begin();
|
||||||
|
SPI.setClockDivider(SPI_CLOCK_DIV2);
|
||||||
|
ADS.begin();
|
||||||
|
if(appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print("Process: Setup End; Timestamp: ");Serial.println(millis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void buzzer(int mode = 1)
|
||||||
|
{
|
||||||
|
for(int i=0;i<mode;i++)
|
||||||
|
{
|
||||||
|
tone(Buzzer_pin, 2730, 1000);
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void all_LEDs_turnoff()
|
||||||
|
{
|
||||||
|
setOutput(GAIN_1, 0, 1);
|
||||||
|
setOutput(GAIN_1, 0, 2);
|
||||||
|
setOutput(GAIN_1, 0, 3);
|
||||||
|
setOutput(GAIN_1, 0, 4);
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void secondary_gain_select(int gain_combo)
|
||||||
|
{
|
||||||
|
bool gain_choice1=bitRead(gain_combo, 0);
|
||||||
|
bool gain_choice2=bitRead(gain_combo, 1);
|
||||||
|
bool gain_choice3=bitRead(gain_combo, 2);
|
||||||
|
bool gain_choice4=bitRead(gain_combo, 3);
|
||||||
|
digitalWrite(PD1G1, gain_choice1);
|
||||||
|
digitalWrite(PD1G2, gain_choice2);
|
||||||
|
digitalWrite(PD2G1, gain_choice3);
|
||||||
|
digitalWrite(PD2G2, gain_choice4);
|
||||||
|
if(appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print("Gains Used\t G1:");Serial.print(gain_choice1);Serial.print("\t G2:");Serial.print(gain_choice2);Serial.print("\t G3:");Serial.print(gain_choice3);Serial.print("\t G4:");Serial.println(gain_choice4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void setOutput(byte gain, unsigned int val, int LEDselect)
|
||||||
|
{
|
||||||
|
byte channel = 0;
|
||||||
|
byte shutdown = 1;
|
||||||
|
if(LEDselect%2 == 0)
|
||||||
|
{
|
||||||
|
channel = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
channel = 0;
|
||||||
|
}
|
||||||
|
byte lowByte = val & 0xff;
|
||||||
|
byte highByte = ((val >> 8) & 0xff) | channel << 7 | gain << 5 | shutdown << 4;
|
||||||
|
if (LEDselect <= 2)
|
||||||
|
{
|
||||||
|
PORTB &= 0xfb;
|
||||||
|
SPI.transfer(highByte);
|
||||||
|
SPI.transfer(lowByte);
|
||||||
|
PORTB |= 0x4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PORTB &= 0xfd;
|
||||||
|
SPI.transfer(highByte);
|
||||||
|
SPI.transfer(lowByte);
|
||||||
|
PORTB |= 0x2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void indicator_LED(char statusLED)
|
||||||
|
{
|
||||||
|
digitalWrite(LEDR,LOW);
|
||||||
|
digitalWrite(LEDG,LOW);
|
||||||
|
digitalWrite(LEDB,LOW);
|
||||||
|
digitalWrite(LEDR,(statusLED=='R'));
|
||||||
|
digitalWrite(LEDG,(statusLED=='G'));
|
||||||
|
digitalWrite(LEDB,(statusLED=='B'));
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
int setDAC_LED()
|
||||||
|
{
|
||||||
|
int LED_flags[]={0,0,0,0};
|
||||||
|
int flags_combined=0;
|
||||||
|
if(appmode==0)
|
||||||
|
{
|
||||||
|
Serial.println("#DAC Sweep Start");
|
||||||
|
}
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
delay(1000);
|
||||||
|
////////////////////////////////////////FIRST FORLOOP TO DISCARD STARTS/////////////////////////////////////////////////
|
||||||
|
for (int i=3000;i<4096;i+=incr)
|
||||||
|
{
|
||||||
|
setOutput(GAIN_1, i, 1);
|
||||||
|
delay(del);
|
||||||
|
int16_t val_0 = ADS.readADC(0);
|
||||||
|
if((val_0>24000))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
delay(2000);
|
||||||
|
////////////////////////////////////////FIRST FORLOOP TO DISCARD ENDS/////////////////////////////////////////////////
|
||||||
|
for (int j=0;j<4;j++)
|
||||||
|
{
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
secondary_gain_select(DacSweep_gain_combinations[j]);
|
||||||
|
for (int i=DAC_Sweep_start_limit[j];i<DAC_Sweep_end_limit[j];i+=incr)
|
||||||
|
{
|
||||||
|
setOutput(GAIN_1, i, j+1);
|
||||||
|
delay(del);
|
||||||
|
int16_t val_0 = ADS.readADC(0);
|
||||||
|
if((val_0>LED_DS_threshold[j])&&(LED_flags[j]==0))
|
||||||
|
{
|
||||||
|
LED_dac[j]=i;
|
||||||
|
LED_flags[j]=1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
if (appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print("LED ");Serial.print(j+1);Serial.print(" DACsweep Completed");Serial.print("\t");Serial.print("NEW DAC VALUE : ");Serial.println(LED_dac[j]);
|
||||||
|
}
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
flags_combined=(LED_flags[3]*1)+(LED_flags[2]*10)+(LED_flags[1]*100)+(LED_flags[0]*1000);
|
||||||
|
if(appmode==0)
|
||||||
|
{
|
||||||
|
Serial.println("#DAC Sweep End");
|
||||||
|
}
|
||||||
|
return flags_combined;
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void eeprom_DAC_write()
|
||||||
|
{
|
||||||
|
int led_dac_split[]= {0,0,0,0,0,0,0,0};
|
||||||
|
Serial.println("#GS");
|
||||||
|
led_dac_split[0] = LED_dac[0]/100;
|
||||||
|
led_dac_split[1] = LED_dac[0]-(led_dac_split[0]*100);
|
||||||
|
led_dac_split[2] = LED_dac[1]/100;
|
||||||
|
led_dac_split[3] = LED_dac[1]-(led_dac_split[2]*100);
|
||||||
|
led_dac_split[4] = LED_dac[2]/100;
|
||||||
|
led_dac_split[5] = LED_dac[2]-(led_dac_split[4]*100);
|
||||||
|
led_dac_split[6] = LED_dac[3]/100;
|
||||||
|
led_dac_split[7] = LED_dac[3]-(led_dac_split[6]*100);
|
||||||
|
if(appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print(led_dac_split[0]);Serial.print("\t");Serial.println(led_dac_split[1]);
|
||||||
|
Serial.print(led_dac_split[2]);Serial.print("\t");Serial.println(led_dac_split[3]);
|
||||||
|
Serial.print(led_dac_split[4]);Serial.print("\t");Serial.println(led_dac_split[5]);
|
||||||
|
Serial.print(led_dac_split[6]);Serial.print("\t");Serial.println(led_dac_split[7]);
|
||||||
|
}
|
||||||
|
for (int i=0;i<8; i++)
|
||||||
|
{
|
||||||
|
EEPROM.write(i, led_dac_split[i]);
|
||||||
|
}
|
||||||
|
Serial.println("#GC");
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void eeprom_DAC_read()
|
||||||
|
{
|
||||||
|
int led_dac_split[]= {0,0,0,0,0,0,0,0};
|
||||||
|
Serial.println("#RS");
|
||||||
|
for(int i=0;i<8;i++)
|
||||||
|
{
|
||||||
|
led_dac_split[i] = EEPROM.read(i);
|
||||||
|
}
|
||||||
|
for(int i=0;i<4;i++)
|
||||||
|
{
|
||||||
|
LED_dac[i] = (led_dac_split[i*2]*100)+led_dac_split[(i*2)+1];
|
||||||
|
}
|
||||||
|
if(appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print(led_dac_split[0]);Serial.print("\t");Serial.println(led_dac_split[1]);
|
||||||
|
Serial.print(led_dac_split[2]);Serial.print("\t");Serial.println(led_dac_split[3]);
|
||||||
|
Serial.print(led_dac_split[4]);Serial.print("\t");Serial.println(led_dac_split[5]);
|
||||||
|
Serial.print(led_dac_split[6]);Serial.print("\t");Serial.println(led_dac_split[7]);
|
||||||
|
Serial.print(LED_dac[0]);Serial.print("\t");Serial.print(LED_dac[1]);Serial.print("\t");Serial.print(LED_dac[2]);Serial.print("\t");Serial.println(LED_dac[3]);
|
||||||
|
}
|
||||||
|
Serial.println("#R C");
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void eeprom_DeviceID_write()
|
||||||
|
{
|
||||||
|
int deviceID_split[]= {0,0,0,0};
|
||||||
|
int D_ID_temp[]={000,0000};
|
||||||
|
Serial.println("#GS");
|
||||||
|
deviceID_split[0] = D_ID_temp[0]/100;
|
||||||
|
deviceID_split[1] = D_ID_temp[0]-(deviceID_split[0]*100);
|
||||||
|
deviceID_split[2] = D_ID_temp[1]/100;
|
||||||
|
deviceID_split[3] = D_ID_temp[1]-(deviceID_split[2]*100);
|
||||||
|
if(appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print(deviceID_split[0]);Serial.print("\t");Serial.println(deviceID_split[1]);
|
||||||
|
Serial.print(deviceID_split[2]);Serial.print("\t");Serial.println(deviceID_split[3]);
|
||||||
|
}
|
||||||
|
for (int i=0;i<4;i++)
|
||||||
|
{
|
||||||
|
EEPROM.write(i+8, deviceID_split[i]);
|
||||||
|
}
|
||||||
|
Serial.println("#GC");
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void eeprom_DeviceID_read()
|
||||||
|
{
|
||||||
|
int led_DeviceID_split[]= {0,0,0,0};
|
||||||
|
int Device_version[]={0,0,0};
|
||||||
|
int Dev_ID[]={0,0,0,0};
|
||||||
|
String Dev_version="000";
|
||||||
|
String D_ID="0000";
|
||||||
|
//Serial.println("#NS");
|
||||||
|
for(int i=8;i<12;i++)
|
||||||
|
{
|
||||||
|
led_DeviceID_split[i-8] = EEPROM.read(i);
|
||||||
|
}
|
||||||
|
Device_version[0] = led_DeviceID_split[0]%10;
|
||||||
|
Device_version[1] = (led_DeviceID_split[1]-(led_DeviceID_split[1]%10))/10;
|
||||||
|
Device_version[2] = led_DeviceID_split[1]%10;
|
||||||
|
Dev_ID[0] = (led_DeviceID_split[2]-(led_DeviceID_split[2]%10))/10;
|
||||||
|
Dev_ID[1] = led_DeviceID_split[2]%10;
|
||||||
|
Dev_ID[2] = (led_DeviceID_split[3]-(led_DeviceID_split[3]%10))/10;
|
||||||
|
Dev_ID[3] = led_DeviceID_split[3]%10;
|
||||||
|
Dev_version = String(Device_version[0])+String(Device_version[1])+String(Device_version[2]);
|
||||||
|
D_ID = String(Dev_ID[0])+String(Dev_ID[1])+String(Dev_ID[2])+String(Dev_ID[3]);
|
||||||
|
if(appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print("Device Version: ");Serial.println(Dev_version);
|
||||||
|
Serial.print("Device ID: ");Serial.println(D_ID);
|
||||||
|
}
|
||||||
|
Device_ID = "SNS HCV-" + Dev_version + "-" + D_ID + " SNE";
|
||||||
|
//Serial.println("#NC");
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void LED_warmup(int warmup_time)
|
||||||
|
{
|
||||||
|
Serial.println("#YS");
|
||||||
|
int warmup_start=millis();
|
||||||
|
int current_time=millis();
|
||||||
|
while((current_time-warmup_start)<warmup_time)
|
||||||
|
{
|
||||||
|
setOutput(GAIN_1, led_warmup_DAC[0], 1);
|
||||||
|
setOutput(GAIN_1, led_warmup_DAC[1], 2);
|
||||||
|
setOutput(GAIN_1, led_warmup_DAC[2], 3);
|
||||||
|
setOutput(GAIN_1, led_warmup_DAC[3], 4);
|
||||||
|
current_time=millis();
|
||||||
|
}
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
Serial.println("#YC");
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
float ten_ADC_averager(int detector_select, int reading_delay)
|
||||||
|
{
|
||||||
|
long temp_storage=0;
|
||||||
|
float temp_average_storage=0;
|
||||||
|
for (int i=0;i<10;i++)
|
||||||
|
{
|
||||||
|
delay(reading_delay);
|
||||||
|
int16_t val_0 = ADS.readADC(detector_select-1);
|
||||||
|
if(raw_ADC_print==1)
|
||||||
|
{
|
||||||
|
Serial.println(val_0);
|
||||||
|
}
|
||||||
|
temp_storage+=val_0;
|
||||||
|
}
|
||||||
|
temp_average_storage=temp_storage/10;
|
||||||
|
return temp_average_storage;
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
float detector(int number_of_readings, int detector_select, int gain_combo, int settling_time, int reading_delay)
|
||||||
|
{
|
||||||
|
if((appmode==0)&&(timestamp_printer==1))
|
||||||
|
{
|
||||||
|
Serial.print("Process: Detector Start for any one LED; Timestamp: ");Serial.println(millis());
|
||||||
|
}
|
||||||
|
secondary_gain_select(gain_combo);
|
||||||
|
temp_counter = 0;
|
||||||
|
intensity_storage = 0;
|
||||||
|
int16_t detector_val = 0;
|
||||||
|
int start_time=millis();
|
||||||
|
int current_time=millis();
|
||||||
|
int lower_limit=0;
|
||||||
|
int upper_limit=0;
|
||||||
|
//limit_average_storage=0;
|
||||||
|
while((current_time-start_time)<settling_time)
|
||||||
|
{
|
||||||
|
int16_t val_0 = ADS.readADC(detector_select-1);
|
||||||
|
if(raw_ADC_print==1)
|
||||||
|
{
|
||||||
|
Serial.println(val_0);
|
||||||
|
}
|
||||||
|
current_time=millis();
|
||||||
|
}
|
||||||
|
for (int i=0;i<(number_of_readings/10);i++)
|
||||||
|
{
|
||||||
|
intensity_storage+=ten_ADC_averager(detector_select, reading_delay);
|
||||||
|
temp_counter++;
|
||||||
|
}
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
average_readings = intensity_storage/temp_counter;
|
||||||
|
if(appmode==0)
|
||||||
|
{
|
||||||
|
if (timestamp_printer==1)
|
||||||
|
{
|
||||||
|
Serial.print("Process: Detector Start for any one LED; Timestamp: ");Serial.println(millis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return average_readings;
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
int limit_switch_check()
|
||||||
|
{
|
||||||
|
int switch_state = digitalRead(Limit_Switch);
|
||||||
|
return switch_state;
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
ADS.setGain(0);
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
if(initialRGBstate==0)
|
||||||
|
{
|
||||||
|
indicator_LED('R');
|
||||||
|
initialRGBstate=1;
|
||||||
|
}
|
||||||
|
if (Serial.available())
|
||||||
|
{
|
||||||
|
char data = Serial.read();
|
||||||
|
if ((data == 'C')||(data == 'D'))
|
||||||
|
{
|
||||||
|
if(data == 'C')
|
||||||
|
{
|
||||||
|
Serial.println("#CS");
|
||||||
|
int flags=setDAC_LED();
|
||||||
|
if(flags!=1111)
|
||||||
|
{
|
||||||
|
if(appmode==0)
|
||||||
|
{
|
||||||
|
Serial.println("Unable to reach MAX ADC Limit for atleast one LED");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Serial.print("LED1_DAC: ");Serial.println(LED_dac[0]);
|
||||||
|
Serial.print("LED2_DAC: ");Serial.println(LED_dac[1]);
|
||||||
|
Serial.print("LED3_DAC: ");Serial.println(LED_dac[2]);
|
||||||
|
Serial.print("LED4_DAC: ");Serial.println(LED_dac[3]);
|
||||||
|
Serial.println("#CC");
|
||||||
|
buzzer(1);
|
||||||
|
}
|
||||||
|
else if(data == 'D')
|
||||||
|
{
|
||||||
|
startTask3();
|
||||||
|
buzzer(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (data == 'B')
|
||||||
|
{
|
||||||
|
indicator_LED('G');
|
||||||
|
startTask1();
|
||||||
|
indicator_LED('B');
|
||||||
|
buzzer(1);
|
||||||
|
}
|
||||||
|
else if ((data == 'S')||(data == 'T')||(data == 'U')||(data == 'V')||(data == 'W'))
|
||||||
|
{
|
||||||
|
indicator_LED('G');
|
||||||
|
if(data == 'S')
|
||||||
|
{
|
||||||
|
startTask2(1);
|
||||||
|
}
|
||||||
|
else if(data == 'T')
|
||||||
|
{
|
||||||
|
startTask2(2);
|
||||||
|
}
|
||||||
|
else if(data == 'U')
|
||||||
|
{
|
||||||
|
startTask2(3);
|
||||||
|
}
|
||||||
|
else if(data == 'V')
|
||||||
|
{
|
||||||
|
startTask2(4);
|
||||||
|
}
|
||||||
|
else if(data == 'W')
|
||||||
|
{
|
||||||
|
startTask2(5);
|
||||||
|
}
|
||||||
|
indicator_LED('R');
|
||||||
|
buzzer(1);
|
||||||
|
}
|
||||||
|
else if (data == 'O')
|
||||||
|
{
|
||||||
|
startTask4();
|
||||||
|
buzzer(1);
|
||||||
|
}
|
||||||
|
else if ((data == 'I')||(data == 'F')||(data == 'X'))
|
||||||
|
{
|
||||||
|
if(data == 'I')
|
||||||
|
{
|
||||||
|
//Serial.println(Device_ID); //Remove code or comment it out
|
||||||
|
//eeprom_DeviceID_write(); //Remove code or comment it out
|
||||||
|
eeprom_DeviceID_read();
|
||||||
|
Serial.println(Device_ID);
|
||||||
|
}
|
||||||
|
else if (data == 'F')
|
||||||
|
{
|
||||||
|
int location=0;
|
||||||
|
for (int i=0;i<Firmware_location_Auto.length();i++)
|
||||||
|
{
|
||||||
|
if(Firmware_location_Auto[i]=='\\')
|
||||||
|
{
|
||||||
|
location=i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Firmware_version_Auto.remove(0,location+1);
|
||||||
|
if (appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print("Firmware Location: ");Serial.println(Firmware_location_Auto);
|
||||||
|
}
|
||||||
|
Serial.print("Firmware Version: ");Serial.println(Firmware_version_Auto);
|
||||||
|
Serial.print("Date and Time of Firmware Upload : ");Serial.println(compile_date);
|
||||||
|
}
|
||||||
|
else if (data =='X')
|
||||||
|
{
|
||||||
|
Serial.println(F("Command 'C' will do : Slow DAC sweep, stop the sweep once required ADC is reached, update DAC value"));
|
||||||
|
Serial.println(F("Command 'D' will do : Fast DAC sweep, will sweep from 0 to 4096, prints ADC values"));
|
||||||
|
Serial.println(F("Command 'B' will do : Blank readings"));
|
||||||
|
Serial.println(F("Command 'S/T/U/V/W' will do : Sample readings with pre-defined secondary gain"));
|
||||||
|
Serial.println(F("Command 'O' will do : Blank readings without turning on the LED"));
|
||||||
|
Serial.println(F("Command 'I' will do : Print Device Serial No."));
|
||||||
|
Serial.println(F("Command 'F' will do : Print the firmware version and upload date and time"));
|
||||||
|
Serial.println(F("Command 'X' will do : Print all available commands"));
|
||||||
|
Serial.println(F("Command 'Y' will do : LED warmup for 5 seconds"));
|
||||||
|
Serial.println(F("Command 'G' will do : Write DAC values temporarily set into permanent EEPROM"));
|
||||||
|
Serial.println(F("Command 'E' will do : Read EEPROM stored DAC values and store in temporary memory"));
|
||||||
|
Serial.println(F("Command 'N' will do : Read EEPROM stored Device ID values and store in temporary memory"));
|
||||||
|
Serial.println(F("Command 'P' will do : Print all ADC values (blank and sample)"));
|
||||||
|
Serial.println(F("Command 'R' will do : Print DAC values stored in temporary memory"));
|
||||||
|
Serial.println(F("Command 'L' will do : Print the status of the limit switch"));
|
||||||
|
Serial.println(F("Command '1/2/3/4' will do : Turen ON LED 1/2/3/4 if pressed ODD number of times, Turen OFF LED 1/2/3/4 if pressed EVEN number of times"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (data == 'Y')
|
||||||
|
{
|
||||||
|
Serial.println("#YS");
|
||||||
|
LED_warmup(5000);
|
||||||
|
Serial.println("#YC");
|
||||||
|
}
|
||||||
|
else if ((data == 'G')||(data == 'E')||(data == 'N'))
|
||||||
|
{
|
||||||
|
if(data == 'G')
|
||||||
|
{
|
||||||
|
eeprom_DAC_write();
|
||||||
|
}
|
||||||
|
else if(data == 'E')
|
||||||
|
{
|
||||||
|
eeprom_DAC_read();
|
||||||
|
}
|
||||||
|
else if(data == 'N')
|
||||||
|
{
|
||||||
|
eeprom_DeviceID_read();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((data == 'P')||(data == 'R'))
|
||||||
|
{
|
||||||
|
if(data == 'P')
|
||||||
|
{
|
||||||
|
Serial.print("RESULT"); Serial.println(" ");
|
||||||
|
Serial.print("LB1 "); Serial.println(avg_blank[0]); Serial.print("LB2 "); Serial.println(avg_blank[1]);
|
||||||
|
Serial.print("LB3 "); Serial.println(avg_blank[2]); Serial.print("LB4 "); Serial.println(avg_blank[3]);
|
||||||
|
Serial.print("LS1 "); Serial.println(avg_sample[0]); Serial.print("LS2 "); Serial.println(avg_sample[1]);
|
||||||
|
Serial.print("LS3 "); Serial.println(avg_sample[2]); Serial.print("LS4 "); Serial.println(avg_sample[3]);
|
||||||
|
Serial.println("REND");
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
else if (data == 'R')
|
||||||
|
{
|
||||||
|
Serial.println("#RS");
|
||||||
|
int dac_print=0;
|
||||||
|
for (int i=0;i<4;i++)
|
||||||
|
{
|
||||||
|
Serial.print("LED");Serial.print(i+1);Serial.print(" DAC : ");Serial.println(LED_dac[i]);
|
||||||
|
}
|
||||||
|
Serial.println("#RC");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (data == 'L')
|
||||||
|
{
|
||||||
|
int switch_state = digitalRead(Limit_Switch);
|
||||||
|
if (switch_state == LOW)
|
||||||
|
{
|
||||||
|
Serial.println("#CIN");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.println("#AIN");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((data == '1')||(data == '2')||(data == '3')||(data == '4'))
|
||||||
|
{
|
||||||
|
int LED_ID = data-'0';
|
||||||
|
String ON_msg= "#L" + String(LED_ID) + "ON";
|
||||||
|
String OFF_msg= "#L" + String(LED_ID) + "OF";
|
||||||
|
if(LED_on_status[LED_ID-1]==0)
|
||||||
|
{
|
||||||
|
setOutput(GAIN_1, LED_dac[LED_ID-1], LED_ID);
|
||||||
|
LED_on_status[LED_ID-1]=1;
|
||||||
|
Serial.println(ON_msg);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
setOutput(GAIN_1, 0, LED_ID);
|
||||||
|
LED_on_status[LED_ID-1]=0;
|
||||||
|
Serial.println(OFF_msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void startTask1()
|
||||||
|
{
|
||||||
|
if((appmode==0)&&(timestamp_printer==1))
|
||||||
|
{
|
||||||
|
Serial.print("Process: Buffer start; Timestamp: ");Serial.println(millis());
|
||||||
|
}
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
delay(100);
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
Serial.println("#BS");
|
||||||
|
for (int i=0;i<4;i++)
|
||||||
|
{
|
||||||
|
setOutput(GAIN_1, LED_dac[i], LED_sequence[i]);
|
||||||
|
delay(100);
|
||||||
|
avg_blank[i] = detector(LED_sample_size[LED_sequence[i]-1], Detector_sequence[LED_sequence[i]-1], Blank_gain_combinations[LED_sequence[i]-1], LED_settling_time[LED_sequence[i]-1], ADC_reading_delay[LED_sequence[i]-1]);
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
if (appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print(millis());Serial.print("\t");Serial.print("LED1B : "); Serial.print(avg_blank[0]); Serial.print("\t"); Serial.print("LED2B : "); Serial.print(avg_blank[1]); Serial.print("\t"); Serial.print("LED3B : "); Serial.print(avg_blank[2]); Serial.print("\t"); Serial.print("LED4B : "); Serial.println(avg_blank[3]);
|
||||||
|
}
|
||||||
|
Serial.println("#BC");
|
||||||
|
if((appmode==0)&&(timestamp_printer==1))
|
||||||
|
{
|
||||||
|
Serial.print("Process: Buffer end; Timestamp: ");Serial.println(millis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void startTask2(int selector)
|
||||||
|
{
|
||||||
|
if((appmode==0)&&(timestamp_printer==1))
|
||||||
|
{
|
||||||
|
Serial.print("Process: Sample start; Timestamp: ");Serial.println(millis());
|
||||||
|
}
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
delay(100);
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
Serial.print("#SS");Serial.println(selector);
|
||||||
|
for (int i=0;i<4;i++)
|
||||||
|
{
|
||||||
|
setOutput(GAIN_1, LED_dac[i], LED_sequence[i]);
|
||||||
|
delay(100);
|
||||||
|
avg_sample[i] = detector(LED_sample_size[LED_sequence[i]-1], Detector_sequence[LED_sequence[i]-1], Sample_gain_combinations[selector-1][LED_sequence[i]-1],LED_settling_time[LED_sequence[i]-1], ADC_reading_delay[LED_sequence[i]-1]);
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
if (appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print(millis());Serial.print("\t");Serial.print("LED1S : "); Serial.print(avg_sample[0]); Serial.print("\t"); Serial.print("LED2S : "); Serial.print(avg_sample[1]); Serial.print("\t"); Serial.print("LED3S : "); Serial.print(avg_sample[2]); Serial.print("\t"); Serial.print("LED4S : "); Serial.println(avg_sample[3]);
|
||||||
|
}
|
||||||
|
Serial.print("#SC");Serial.println(selector);
|
||||||
|
if((appmode==0)&&(timestamp_printer==1))
|
||||||
|
{
|
||||||
|
Serial.print("Process: Sample end; Timestamp: ");Serial.println(millis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void startTask3()
|
||||||
|
{
|
||||||
|
if((appmode==0)&&(timestamp_printer==1))
|
||||||
|
{
|
||||||
|
Serial.print("Process: Diagnostics start; Timestamp: ");Serial.println(millis());
|
||||||
|
}
|
||||||
|
int16_t val_0 =0;
|
||||||
|
int reading_increment=128;
|
||||||
|
int lowerlimit_dac=0;
|
||||||
|
int upperlimit_dac=0;
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
Serial.println("#DS");
|
||||||
|
|
||||||
|
for (int j=0;j<4;j++)
|
||||||
|
{
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
int start_time=millis();
|
||||||
|
int current_time=millis();
|
||||||
|
while (((current_time-start_time)<(detector_settling_time*2)))
|
||||||
|
{
|
||||||
|
val_0 = ADS.readADC((Detector_sequence[LED_sequence[j]-1])-1);
|
||||||
|
current_time=millis();
|
||||||
|
}
|
||||||
|
if((val_0 > 10)&&(appmode==0))
|
||||||
|
{
|
||||||
|
Serial.println("Error: stray light interference or LED partly ON ");
|
||||||
|
}
|
||||||
|
int flag=0;
|
||||||
|
secondary_gain_select(DacSweep_gain_combinations[j]);
|
||||||
|
for (int i = 0; i <=4096; i+= reading_increment)
|
||||||
|
{
|
||||||
|
setOutput(GAIN_1, i, j+1);
|
||||||
|
int start_time=millis();
|
||||||
|
int current_time=millis();
|
||||||
|
while ((current_time-start_time)<detector_settling_time_DACsweep)
|
||||||
|
{
|
||||||
|
val_0 = ADS.readADC((Detector_sequence[LED_sequence[j]-1])-1);
|
||||||
|
current_time=millis();
|
||||||
|
}
|
||||||
|
if((val_0>LED_DS_threshold[LED_sequence[j]-1])&&(flag==0))
|
||||||
|
{
|
||||||
|
lowerlimit_dac= i-(reading_increment*2);
|
||||||
|
upperlimit_dac= i+(reading_increment*2);
|
||||||
|
if (lowerlimit_dac<0)
|
||||||
|
{
|
||||||
|
lowerlimit_dac=0;
|
||||||
|
}
|
||||||
|
if(upperlimit_dac>4095)
|
||||||
|
{
|
||||||
|
upperlimit_dac=4095;
|
||||||
|
}
|
||||||
|
DAC_lower_limit[j]=lowerlimit_dac;
|
||||||
|
DAC_upper_limit[j]=upperlimit_dac;
|
||||||
|
flag=1;
|
||||||
|
}
|
||||||
|
Serial.print("LED:");Serial.print(j+1);Serial.print("__DAC:");Serial.print(i);Serial.print("__ADC:");Serial.println(val_0);
|
||||||
|
}
|
||||||
|
if (appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print("LED:");Serial.print(j+1);Serial.print("__DAC LOWER LIMIT:");Serial.println(DAC_lower_limit[j]);
|
||||||
|
Serial.print("LED:");Serial.print(j+1);Serial.print("__DAC UPPER LIMIT:");Serial.println(DAC_upper_limit[j]);
|
||||||
|
}
|
||||||
|
secondary_gain_select(0);
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
}
|
||||||
|
Serial.println("#DC");
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
delay(1000);
|
||||||
|
if((appmode==0)&&(timestamp_printer==1))
|
||||||
|
{
|
||||||
|
Serial.print("Process: Diagnostics end; Timestamp: ");Serial.println(millis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void startTask4()
|
||||||
|
{
|
||||||
|
if((appmode==0)&&(timestamp_printer==1))
|
||||||
|
{
|
||||||
|
Serial.print("Process: Buffer start; Timestamp: ");Serial.println(millis());
|
||||||
|
}
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
delay(100);
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
Serial.println("#OS");
|
||||||
|
for (int i=0;i<4;i++)
|
||||||
|
{
|
||||||
|
setOutput(GAIN_1, 0, LED_sequence[i]);
|
||||||
|
delay(100);
|
||||||
|
avg_blank[i] = detector(LED_sample_size[LED_sequence[i]-1], Detector_sequence[LED_sequence[i]-1], Blank_gain_combinations[LED_sequence[i]-1], 2000, ADC_reading_delay[LED_sequence[i]-1]);
|
||||||
|
all_LEDs_turnoff();
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
if (appmode==0)
|
||||||
|
{
|
||||||
|
Serial.print(millis());Serial.print("\t");Serial.print("LED1B : "); Serial.print(avg_blank[0]); Serial.print("\t"); Serial.print("LED2B : "); Serial.print(avg_blank[1]); Serial.print("\t"); Serial.print("LED3B : "); Serial.print(avg_blank[2]); Serial.print("\t"); Serial.print("LED4B : "); Serial.println(avg_blank[3]);
|
||||||
|
}
|
||||||
|
Serial.println("#OC");
|
||||||
|
if((appmode==0)&&(timestamp_printer==1))
|
||||||
|
{
|
||||||
|
Serial.print("Process: Buffer end; Timestamp: ");Serial.println(millis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
Reference in New Issue
Block a user