Paso 28: El código
Este código puede ser portado a Arduino IDE de Energia y funciona perfectamente en ambas plataformas.
Usted puede descargar energia aquí: http://energia.nu/download/
CAMBIAR la x y la D y la nm VARIABLES a partido su resultados de la último paso! De lo contrario su calibración será mi calibración de mi equipo, no tuyo. Entonces dejará de proporcionar los datos exactos.
El código de abajo es para el uso con una tarjeta SD y transmisión en vivo.
#include "SPI.h" #include "pfatfs.h" #define cs_pin 10 // chip select pin #define read_buffer 128 // size (in bytes) of read buffer unsignedshortint bw, br;//, i; char buffer[read_buffer]; int rc; DIR dir; /* Directory object */ FILINFO fno; /* File information object *///I/O Belowconstint StepperMotor = P2_4; constint StepperDirection = P2_5; constint Sensors[] = {P1_3}; // Add more sensors here!\//Constants for calibration below!constdouble nm = 405; // Known Calibration source Frequencyconstdouble x = 33.28; // Distance of point above the "Screen"constdouble D = 48.78; // distance to the "Screen"constdouble degreesperstep = 18; // Degrees per step for the stepper motorconstdouble microstepping = 8; // This allows for you to determine how often you want the Sample to be taken (inverse microstep)constdouble mmPerRotation = 2.76; // This is the distance between the two teeth of the spindle which moves the sensor up and downconstdouble heightofTray = 38.00; // Under estimate this a bitconstdouble starty = 32.35; constboolean SDCARD = false; /////////////////////////Fiddle with the below at your own risk!///////////////////////////////////////////constdouble stepsperrotation = 360/degreesperstep; constdouble microstepsuntilend = heightofTray/mmPerRotation * stepsperrotation * (microstepping); constdouble d = (nm*1e-9)/sin(atan(x/D)); //const double d = 720e-9;//const double x = (nm*1e-9*d);int wavelengthvIntensity[sizeof(Sensors)-1]; int datafile = 0; double cnt = 0; char buf[30]; double tempnm = 0; double currenty = 0; constdouble zero = starty-x; char ksk = '0'; voidsetup() { pinMode(PUSH2, INPUT_PULLUP); pinMode(StepperMotor, OUTPUT); pinMode(StepperDirection, OUTPUT); digitalWrite(StepperMotor, LOW); digitalWrite(StepperDirection, LOW); Serial.begin(9600); // initialize the serial terminal if (SDCARD == true){ FatFs.begin(cs_pin); // initialize FatFS library calls } for (int i=0;i<sizeof(wavelengthvIntensity);i++) { pinMode(Sensors[i], INPUT); } for (int i = 0; i < microstepsuntilend; i++) { digitalWrite(StepperMotor, HIGH); delayMicroseconds(250); digitalWrite(StepperMotor, LOW); delayMicroseconds(250); } digitalWrite(StepperDirection, HIGH); } void die (int pff_err){ Serial.println();Serial.print("Failed with rc=");Serial.print(pff_err,DEC); for (;;) ; } void writedata() { rc = FatFs.open("Data.csv"); if (rc) die(rc); Serial.println(); Serial.println("Writing scan Data"); delay(100); bw=0; for (int i=0;i<sizeof(wavelengthvIntensity);i++) { sprintf( buf, "%lu, ",wavelengthvIntensity[i]); int StringLength = strlen(buf); rc = FatFs.write(buf, StringLength, &bw); if (rc || !bw) break; } sprintf( buf, "%lu\n\r",tempnm); int StringLength = strlen(buf); rc = FatFs.write(buf, StringLength, &bw); if (rc) die(rc); rc = FatFs.write(0, 0, &bw); //Finalize write if (rc) die(rc); rc = FatFs.close(); //Close fileif (rc) die(rc); } voidloop() { if (ksk == '1') { ksk = 0; cnt = 0; while (cnt < microstepsuntilend) { for (int i=0;i<sizeof(wavelengthvIntensity)-1;i++) { wavelengthvIntensity[i] = analogRead(Sensors[i]); Serial.print(wavelengthvIntensity[i]); Serial.print(", "); } Serial.println(tempnm); if (SDCARD == true) { writedata(); } digitalWrite(StepperMotor, HIGH); delayMicroseconds(1); digitalWrite(StepperMotor, LOW); delayMicroseconds(1); cnt++; //Serial.println(currenty); currenty = zero+((cnt/microstepsuntilend) * heightofTray); tempnm = d*sin(atan((x+currenty)/D))*(1e9); } digitalWrite(StepperDirection, LOW); for (int i = 0; i < microstepsuntilend; i++) { digitalWrite(StepperMotor, HIGH); delayMicroseconds(250); digitalWrite(StepperMotor, LOW); delayMicroseconds(250); } digitalWrite(StepperDirection, HIGH); Serial.println("$"); } ksk = Serial.read(); }
El código a continuación es el código para el espectrómetro: modo de vivir sólo.
//I/O Belowconstint StepperMotor = P2_4; constint StepperDirection = P2_5; constint Sensors[] = {P1_3}; // Add more sensors here!\//Constants for calibration below!constdouble nm = 405; // Known Calibration source Frequencyconstdouble x = 37.65; // Distance of point above the "Screen"constdouble D = 50.00; // distance to the "Screen"constdouble degreesperstep = 18; // Degrees per step for the stepper motorconstdouble microstepping = 8; // This allows for you to determine how often you want the Sample to be taken (inverse microstep)constdouble mmPerRotation = 2.76; // This is the distance between the two teeth of the spindle which moves the sensor up and downconstdouble heightofTray = 38.00; // Under estimate this a bitconstdouble starty = 32.35; /////////////////////////Fiddle with the below at your own risk!///////////////////////////////////////////constdouble stepsperrotation = 360/degreesperstep; constdouble microstepsuntilend = heightofTray/mmPerRotation * stepsperrotation * (microstepping); constdouble d = (nm*1e-9)/sin(atan(x/D)); //const double d = 720e-9;//const double x = (nm*1e-9*d);int wavelengthvIntensity[sizeof(Sensors)-1]; int datafile = 0; double cnt = 0; char buf[30]; double tempnm = 0; double currenty = 0; constdouble zero = starty-x; char ksk = '0'; voidsetup() { pinMode(PUSH2, INPUT_PULLUP); Serial.begin(9600); // Hah, I forgot to add this in Special thanks to tommy_goh1997 for spotting the bug! pinMode(StepperMotor, OUTPUT); pinMode(StepperDirection, OUTPUT); digitalWrite(StepperMotor, LOW); digitalWrite(StepperDirection, LOW); for (int i=0;i<sizeof(wavelengthvIntensity);i++) { pinMode(Sensors[i], INPUT); } for (int i = 0; i < microstepsuntilend; i++) { digitalWrite(StepperMotor, HIGH); delayMicroseconds(250); digitalWrite(StepperMotor, LOW); delayMicroseconds(250); } digitalWrite(StepperDirection, HIGH); } voidloop() { if (ksk == '1') { ksk = 0; cnt = 0; while (cnt < microstepsuntilend) { for (int i=0;i<sizeof(wavelengthvIntensity)-1;i++) { wavelengthvIntensity[i] = analogRead(Sensors[i]); Serial.print(wavelengthvIntensity[i]); Serial.print(", "); } Serial.println(tempnm); digitalWrite(StepperMotor, HIGH); delayMicroseconds(1); digitalWrite(StepperMotor, LOW); delayMicroseconds(1); cnt++; //Serial.println(currenty); currenty = zero+((cnt/microstepsuntilend) * heightofTray); tempnm = d*sin(atan((x+currenty)/D))*(1e9); } digitalWrite(StepperDirection, LOW); for (int i = 0; i < microstepsuntilend; i++) { digitalWrite(StepperMotor, HIGH); delayMicroseconds(250); digitalWrite(StepperMotor, LOW); delayMicroseconds(250); } digitalWrite(StepperDirection, HIGH); Serial.println("$"); } ksk = Serial.read(); }