122 lines
2.7 KiB
Arduino
122 lines
2.7 KiB
Arduino
|
#define Light_sensor A0
|
||
|
#define Lamp_mod 6
|
||
|
#define Move_sensor 2
|
||
|
|
||
|
// Gemessene Sensorwete des Helligkeitssensors
|
||
|
|
||
|
// max 945 in 02
|
||
|
// min 12 in 02
|
||
|
|
||
|
// max 922 in 04
|
||
|
// min 10 in 04
|
||
|
|
||
|
// 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), 10, 922, 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);
|
||
|
analogWrite(lpin, n_led_pwr);
|
||
|
}
|
||
|
|
||
|
uint8_t movement_threshold;
|
||
|
|
||
|
void movement(){
|
||
|
if ((movement_threshold>=80)&&(movement_threshold+5<100)){
|
||
|
movement_threshold += 5;
|
||
|
}
|
||
|
else{
|
||
|
movement_threshold = 80;
|
||
|
}
|
||
|
if (Serial.availableForWrite()) {
|
||
|
if (movement_threshold-25 > 0) Serial.write(movement_threshold-25);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void setup() {
|
||
|
delay(random(0, 20));
|
||
|
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 = 945;
|
||
|
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);
|
||
|
|
||
|
int8_t i;
|
||
|
while ((Serial.available())&&(i<10) ){
|
||
|
uint8_t msg = Serial.read();
|
||
|
if (msg < 101){
|
||
|
if ( msg >= movement_threshold ) movement_threshold= msg;
|
||
|
}
|
||
|
i++;
|
||
|
}
|
||
|
|
||
|
}
|