From 7c70b75b78775fd40903b9f0620e55944877544e Mon Sep 17 00:00:00 2001 From: i3le Date: Thu, 11 Jul 2024 20:03:48 +0200 Subject: [PATCH] Add light_fast_comments.ino a pesentamle version --- light_fast_comments.ino | 133 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 light_fast_comments.ino diff --git a/light_fast_comments.ino b/light_fast_comments.ino new file mode 100644 index 0000000..ca89672 --- /dev/null +++ b/light_fast_comments.ino @@ -0,0 +1,133 @@ +#include +SoftwareSerial mySerial(3, 4); + +#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; + } + sendnetsteck(25); +} + +void sendnetsteck(int8_t livx){ + if (movement_threshold-livx > 0){ + mySerial.write(movement_threshold-livx); + Serial.print("Msg Send:"); + Serial.println(movement_threshold-livx); + } +} +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); + mySerial.begin(38400); +} + +// 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); + + while (mySerial.available()){ + uint8_t msg = mySerial.read(); + Serial.print("recived msg:"); + Serial.println(msg); + if (msg < 101){ + if ( msg >= movement_threshold ){ + movement_threshold= msg; + sendnetsteck(25); + } + } + } +} \ No newline at end of file