Hackathon/light.ino
2024-07-11 17:15:06 +02:00

113 lines
2.5 KiB
C++

#define Light_sensor A0
#define Lamp_mod 6
#define Move_sensor 2
// Gemessene Sensorwete des Helligkeitssensors
// max 630 in 01
// min 58 in 01
// max 995 in 03
// max 19 in 03
uint8_t sensoflight(uint8_t lpin){
uint16_t light = map(analogRead(lpin), 58, 630, 0, 100);
return light;
}
uint8_t led_pwr;
// maximale dauer der funktion less than 860 * 10^-6 s
/*
tested with:
unsigned long time_stp;
unsigned long time_fin;
setlamp(Lamp_mod, 0);
time_stp = micros();
setlamp(Lamp_mod, 100);
time_fin = micros();
unsigned long need = time_fin - time_stp;
Serial.println (need);
delay(1000);
*/
void setlamp(uint8_t lpin, uint8_t lbrightness){
lbrightness = lbrightness%101;
uint8_t n_led_pwr = map(lbrightness, 0, 100, 0, 255);
int16_t led_lamp = led_pwr - n_led_pwr;
while (led_lamp != 0) {
if (led_lamp < 0){
led_pwr ++;
}
if (led_lamp > 0){
led_pwr --;
}
delayMicroseconds(10);
analogWrite(lpin, led_pwr);
led_lamp = led_pwr - n_led_pwr;
}
}
uint8_t movement_threshold;
void movement(){
if ((movement_threshold>=80)&&(movement_threshold+5<100)){
movement_threshold += 5;
}
else{
movement_threshold = 80;
}
}
void setup() {
pinMode(Move_sensor,INPUT);
attachInterrupt(digitalPinToInterrupt(Move_sensor), movement, RISING);
pinMode(Light_sensor,INPUT);
pinMode(Lamp_mod,OUTPUT);
Serial.begin(9600);
}
// functions werte definiert von 0 bis 100 darüber und unter chaos!
void setlight_with_wehtherinfluence(int8_t f_light){
// 0 - 100 limit.
f_light = (f_light % 101);
// aretmetics over 100 scans!
uint16_t env_light = 0;
for (int i=0; i <100; i++){
env_light += sensoflight(Light_sensor);
delayMicroseconds(10);
}
env_light = (env_light/100);
// aretmetic light of enviroment
int8_t setlight = f_light - env_light;
if (setlight > 100) setlamp(Lamp_mod,100); // would still never heppen but if lamps won't flicker.
if (setlight > 0) setlamp(Lamp_mod,setlight);
else setlamp(Lamp_mod,0);
}
int maxwert;
void loop() {
/*setlight_with_wehtherinfluence(30);
delay(1000);
setlight_with_wehtherinfluence(80);
delay(1000);
*/
/*
int test = analogRead(Light_sensor);
if (test > maxwert) maxwert = test;
Serial.println(maxwert);
*/
// testcode for precentage
//Serial.println(sensoflight(Light_sensor));
Serial.println(movement_threshold);
if (movement_threshold) movement_threshold--; // time based light reduction!
setlight_with_wehtherinfluence(movement_threshold);
delay(100);
}