//This is the third attempt of the ultimate code. //In this sketch, I will utlilize the flashlight //This is still missing the clock/time, and //GPS tracking functions, those will be in V4 // *sigh* #include #include #include #include #include #include #include #include // If using software SPI (the default case): #define OLED_MOSI 1 #define OLED_CLK 2 #define OLED_DC 3 #define OLED_CS 5 #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS); #define NUMFLAKES 10 #define XPOS 0 #define YPOS 1 #define DELTAY 2 #define LOGO16_GLCD_HEIGHT 16 #define LOGO16_GLCD_WIDTH 16 // end of setup for OLED //defining pins so far, (also utilized for reference) const int rightWhiteButtonPin = 23; const int rightThreeButtonLedsPin = 12; const int blueButtonPin = 21; const int leftWhiteButtonPin = 20; const int redButtonPin = 6; const int pinkLedsPin = 7; const int flashlightLedsPin = 8; const int redButtonLedPin = 11; const int HIH4030_Pin = A0; //const int TSL2561_SCL_Pin = SCL0 //(D19) //const int TSL2561_SDA_Pin = SDA0 //(D18) //const int HMC6352_SCL_Pin = SCL1 //(D29) //const int HMC6352_SDA_Pin = SDA1 //(D30) //const int GPS_RX_Pin = RX2 //(D9) //const int GPS_TX_Pin = TX2 //(D10) const int temperaturePin = A8; //telling Teensy that buttons start off not being pressed int rightWhiteButtonState = 0; int blueButtonState = 0; int leftWhiteButtonState = 0; int redButtonState = 0; //Ints for Compass int HMC6352SlaveAddress = 0x42; int HMC6352ReadAddress = 0x41; //"A" in hex, A command is: int headingValue; //For Luminance Sensor Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345); //For GPS TinyGPS gps; SoftwareSerial ss(9, 10); #define ss Serial2 static void smartdelay(unsigned long ms); static void print_float(float val, float invalid, int len, int prec); static void print_int(unsigned long val, unsigned long invalid, int len); static void print_date(TinyGPS &gps); static void print_str(const char *str, int len); void setup() { //For Compass: // "The Wire library uses 7 bit addresses throughout. //If you have a datasheet or sample code that uses 8 bit address, //you'll want to drop the low bit (i.e. shift the value one bit to the right), //yielding an address between 0 and 127." HMC6352SlaveAddress = HMC6352SlaveAddress >> 1; // I know 0x42 is less than 127, but this is still required //end of Compass setup //always have to do this Serial.begin(115200); Wire.begin(); //using 3.3v line internally for OLED display.begin(SSD1306_SWITCHCAPVCC); //show Adafruit splashscreen, wait 2 second before moving on to actual program display.display(); delay(3000); ss.begin(9600); //Start Lux setup screens display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0,0); display.println("Lux SensorInfo:"); display.display(); delay(1500); /* Initialise the Lux sensor */ display.setTextSize(1); if(!tsl.begin()) { /* There was a problem detecting the ADXL345 ... check your connections */ display.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!"); display.display(); while(1); } /* Display some basic information on this sensor */ displaySensorDetails(); /* Setup the sensor gain and integration time */ configureSensor(); //end of Lux sensor setup //gotta determine our input pins, which are the 4 buttons pinMode(rightWhiteButtonPin, INPUT); pinMode(blueButtonPin, INPUT); pinMode(leftWhiteButtonPin, INPUT); pinMode(redButtonPin, INPUT); pinMode(HIH4030_Pin, INPUT); //gotta determine our outpins pins, which are the LEDs of the buttons, //the pink LEDs, and the flashlight LEDs pinMode(rightThreeButtonLedsPin, OUTPUT); pinMode(redButtonLedPin, OUTPUT); pinMode(pinkLedsPin, OUTPUT); pinMode(flashlightLedsPin, OUTPUT); pinMode(OLED_MOSI, OUTPUT); } //end of setup //Middle stuff (what goes inbetween Setup and Loop) //For Humidity Sensor float getHumidity(float degreesCelsius){ //caculate relative humidity float supplyVolt = 3.3; const int HIH4030_Pin = A0; // read the value from the sensor: int HIH4030_Value = analogRead(HIH4030_Pin); float voltage = HIH4030_Value/1023. * supplyVolt; // convert to voltage value // convert the voltage to a relative humidity // - the equation is derived from the HIH-4030/31 datasheet // - it is not calibrated to your individual sensor // Table 2 of the sheet shows the may deviate from this line float sensorRH = 161.0 * voltage / supplyVolt - 25.8; float trueRH = sensorRH / (1.0546 - 0.0026 * degreesCelsius); //temperature adjustment return trueRH; } //End of Humidity sensor middle stuff //For Lux Sensor void displaySensorDetails(void) { sensor_t sensor; tsl.getSensor(&sensor); display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); //display.println("------------------------------------"); //display.println("Light Sensor Test"); display.println(""); display.print ("Sensor:"); display.println(sensor.name); display.print ("Driver Ver:"); display.println(sensor.version); //display.print ("Unique ID:"); display.println(sensor.sensor_id); display.print ("Max Value:"); display.print(sensor.max_value); display.print("lux"); display.print ("Min Value:"); display.print(sensor.min_value); display.println("lux"); display.print ("Resolution:"); display.print(sensor.resolution); display.println("lux"); // display.println("------------------------------------"); //display.println(""); display.display(); delay(3000); display.clearDisplay(); } void configureSensor(void) { /* You can also manually set the gain or enable auto-gain support */ // tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */ // tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */ tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */ /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */ //tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */ // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */ tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */ /* Update these values depending on what you've set above! */ //display.println("------------------------------------"); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.print ("Gain: "); display.println("Auto"); display.print ("Timing: "); display.println("402 ms"); //display.println("------------------------------------"); display.display(); delay(2000); display.clearDisplay(); } //end of Lux Sensor Middle Stuff //For GPS static void smartdelay(unsigned long ms) { unsigned long start = millis(); do { while (ss.available()) gps.encode(ss.read()); } while (millis() - start < ms); } static void print_float(float val, float invalid, int len, int prec) { if (val == invalid) { while (len-- > 1) Serial.print('*'); Serial.print(' '); } else { Serial.print(val, prec); int vi = abs((int)val); int flen = prec + (val < 0.0 ? 2 : 1); // . and - flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1; for (int i=flen; i 0) sz[len-1] = ' '; Serial.print(sz); smartdelay(0); } static void print_date(TinyGPS &gps) { int year; byte month, day, hour, minute, second, hundredths; unsigned long age; gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age); if (age == TinyGPS::GPS_INVALID_AGE) Serial.print("********** ******** "); else { char sz[32]; sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ", month, day, year, hour, minute, second); Serial.print(sz); } print_int(age, TinyGPS::GPS_INVALID_AGE, 5); smartdelay(0); } static void print_str(const char *str, int len) { int slen = strlen(str); for (int i=0; i