#include /* Blue Robotics Lumen LED Embedded Software ------------------------------------------------ Title: Lumen LED Light Embedded Microcontroller Software, modified by Seatools Pty Ltd Description: This file is used on the ATtiny45 microcontroller on the Lumen LED Light and controls the dimming of the light, using serial commands. Additionally, a thermistor is used to sense temperature and automatically dim the light if it is overheating. ------------------------------- The MIT License (MIT) Copyright (c) 2016 Blue Robotics Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------*/ // HARDWARE PIN DEFINITIONS #define LED_PIN 1 // pin 6 // #define TEMP_PIN A1 // pin 7 #define rxPin 0 // D0, Pin 5 #define txPin -1 // undefined pin, ignore // set up serial port SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin); // DIMMING CHARACTERISTICS // Temp to start dimming the lights at // #define DIM_ADC 265 // 265 for 85C board temperature // Dimming gains // #define DIM_KP 5.0 // #define DIM_KI 0.5 // float signal = 1100; // int16_t pwm; // float adc; // float error; // float control; // float dimI; // float maxPWM = 255; char c; int ilevel = 0; // const float smoothAlpha = 0.02; void setup() { pinMode(LED_PIN,OUTPUT); // define pin mode for rx: pinMode(rxPin, INPUT); // set the data rate for the SoftwareSerial port mySerial.begin(9600); // Setup up PWM on output pin // The default analogWrite frequency can mess with cameras // TCCR0B = _BV(CS01); // Set prescalar to 8 for 8M/8/256 = 3906 Hz } void loop() { // Read serial input if available, use serial code to set dimming level while (mySerial.available()){ c = mySerial.read(); // dimI = 0; // PID controller to control max temperature limit. Not a very linear // approach but it works well for this. This will basically control the // limit to maintain DIM_ADC temperature or better. // adc = adc*(1-smoothAlpha) + smoothAlpha*analogRead(TEMP_PIN); // error = DIM_ADC-adc; // dimI += error*0.005; // dimI = constrain(dimI,-255/abs(DIM_KI),255/abs(DIM_KI)); // Limit I-term spooling // Build control value // control = error*DIM_KP + dimI*DIM_KI; // maxPWM = 255-control; // Output PWM to LED driver based on character read from serial input if (c == '3') { if (ilevel == 0) { digitalWrite(LED_PIN, LOW); } else if (ilevel == 1) { analogWrite(LED_PIN, 21); } else if (ilevel == 2) { analogWrite(LED_PIN, 70); } else if (ilevel == 3) { analogWrite(LED_PIN, 120); } else if (ilevel == 4) { analogWrite(LED_PIN, 170); } else if (ilevel == 5) { analogWrite(LED_PIN, 230); } ilevel = ilevel+1; if (ilevel > 5) { ilevel = 0; } } delay(50); } }