//------------------------------------------------------------------------
#include <Arduino.h>
#include <math.h>
/* Logistic regression weights
const float W_LDR = -0.9543717503547668f;
const float W_POT = -1.900092601776123f;
const float BIAS = 1.0f; // adjusted for demo
#define ADC_MAX 4095.0f
/* Pin definitions
const int ldrPin = A1;
const int potPin = A2;
const int ledGreen = D5;
const int ledRed = D6;
/* Sigmoid function
float sigmoidf(float x) { return 1.0f / (1.0f + exp(-x)); }
/* Logistic regression prediction
float logistic_predict(float x_ldr, float x_pot) {
float z = W_LDR*x_ldr + W_POT*x_pot + BIAS;
return sigmoidf(z);
}
void setup() {
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
analogReadResolution(12);
Serial.begin(9600);
}
void loop() {
// Read sensors
float x_ldr = analogRead(ldrPin) / ADC_MAX; // normalize 0..1
float x_pot = analogRead(potPin) / ADC_MAX;
// Compute probability
float p = logistic_predict(x_ldr, x_pot);
bool class1 = (p >= 0.5f);
// Drive LEDs
digitalWrite(ledGreen, class1 ? HIGH : LOW);
digitalWrite(ledRed, class1 ? LOW : HIGH);
// Debug output
Serial.print("LDR="); Serial.print(x_ldr, 3);
Serial.print(" POT="); Serial.print(x_pot, 3);
Serial.print(" P="); Serial.println(p, 3);
delay(200);
}
//------------------------------------------------------------------------
#include <Arduino.h>
#include <math.h>
/*
/* =========================
Pins
=========================
const int ldrPin = A1; // LDR analog
const int potPin = A2; // Potentiometer analog
const int dhtPin = PA3; // DHT22 data pin
const int ledGreen = D5; // Comfort LED
const int ledRed = D6; // Not comfortable LED
/* =========================
Logistic regression weights
=========================
const float W_LDR = -0.9543717503547668f;
const float W_POT = -1.900092601776123f;
const float W_TEMP = 2.0f; // example weight for temperature
const float W_HUM = -1.0f; // example weight for humidity
const float BIAS = 1.0f; // adjusted for demo
#define ADC_MAX 4095.0f
/* =========================
Sigmoid function
=========================
float sigmoidf(float x) { return 1.0f / (1.0f + exp(-x)); }
/* =========================
Logistic prediction
=========================
float logistic_predict(float x_ldr, float x_pot, float temp, float hum) {
float z = W_LDR*x_ldr + W_POT*x_pot + W_TEMP*temp + W_HUM*hum + BIAS;
return sigmoidf(z);
}
/* =========================
DHT22 single-wire reading (blocking)
Simple implementation for Wokwi demonstration
=========================
uint8_t readDHT22(float &temperature, float &humidity) {
// Wokwi simulation placeholder values
// Normally you implement full DHT22 protocol here
// For demo, we just simulate some values
temperature = 25.0f; // simulate 25°C
humidity = 50.0f; // simulate 50%
return 1; // success
}
/* =========================
Setup
=========================
void setup() {
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
analogReadResolution(12); // 12-bit ADC
Serial.begin(9600);
}
/* =========================
Main loop
=========================
void loop() {
// Read analog sensors
float x_ldr = (float)analogRead(ldrPin) / ADC_MAX;
float x_pot = (float)analogRead(potPin) / ADC_MAX;
// Read DHT22
float temp = 0, hum = 0;
readDHT22(temp, hum);
// Normalize temp/humidity to 0..1 for demo
float norm_temp = temp / 50.0f; // assuming 0-50°C
float norm_hum = hum / 100.0f; // 0-100%
// Compute probability
float p = logistic_predict(x_ldr, x_pot, norm_temp, norm_hum);
bool class1 = (p >= 0.5f);
// Drive LEDs
digitalWrite(ledGreen, class1 ? HIGH : LOW);
digitalWrite(ledRed, class1 ? LOW : HIGH);
// Debug output
Serial.print("LDR="); Serial.print(x_ldr,3);
Serial.print(" POT="); Serial.print(x_pot,3);
Serial.print(" TEMP="); Serial.print(norm_temp,3);
Serial.print(" HUM="); Serial.print(norm_hum,3);
Serial.print(" P="); Serial.println(p,3);
delay(500);
}
//--------------------------------------------------
//---------------------------------------------------
//-----------------------------------------------------
//working
#include <Arduino.h>
#include <math.h>
/* Pins
const int ldrPin = A1;
const int potPin = A2;
const int dhtPin = PA3; // data pin (simulated)
const int ledGreen = D5;
const int ledRed = D6;
/* Logistic regression weights
const float W_LDR = -0.954f;
const float W_POT = -1.900f;
const float W_TEMP = 2.0f; // example weight
const float W_HUM = -1.0f; // example weight
const float BIAS = 1.0f;
#define ADC_MAX 4095.0f
/* Sigmoid
float sigmoidf(float x) { return 1.0f / (1.0f + exp(-x)); }
/* Logistic prediction
float logistic_predict(float x_ldr, float x_pot, float temp, float hum) {
float z = W_LDR*x_ldr + W_POT*x_pot + W_TEMP*temp + W_HUM*hum + BIAS;
return sigmoidf(z);
}
/* Simulate DHT22 reading
float readTemp() { return 25.0f; } // 25°C fixed for demo
float readHum() { return 50.0f; } // 50% fixed for demo
void setup() {
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
analogReadResolution(12);
Serial.begin(9600);
}
void loop() {
float x_ldr = analogRead(ldrPin) / ADC_MAX;
float x_pot = analogRead(potPin) / ADC_MAX;
float norm_temp = readTemp() / 50.0f; // normalize
float norm_hum = readHum() / 100.0f;
float p = logistic_predict(x_ldr, x_pot, norm_temp, norm_hum);
bool class1 = (p >= 0.5f);
digitalWrite(ledGreen, class1 ? HIGH : LOW);
digitalWrite(ledRed, class1 ? LOW : HIGH);
Serial.print("LDR="); Serial.print(x_ldr,3);
Serial.print(" POT="); Serial.print(x_pot,3);
Serial.print(" TEMP="); Serial.print(norm_temp,3);
Serial.print(" HUM="); Serial.print(norm_hum,3);
Serial.print(" P="); Serial.println(p,3);
delay(1000);
}*/
/////////---------------
/////////////-
/*
#include <Arduino.h>
#include <math.h>
// Pins
const int ldrPin = A1;
const int potPin = A2;
const int ledGreen = D5;
const int ledRed = D6;
//Logistic regression weights
const float W_LDR = -0.954f;
const float W_POT = -1.900f;
const float W_TEMP = 2.0f;
const float W_HUM = -1.0f;
const float BIAS = 1.0f;
#define ADC_MAX 4095.0f
// Sigmoid function
float sigmoidf(float x) { return 1.0f / (1.0f + exp(-x)); }
// Logistic regression prediction
float logistic_predict(float x_ldr, float x_pot, float temp, float hum) {
float z = W_LDR*x_ldr + W_POT*x_pot + W_TEMP*temp + W_HUM*hum + BIAS;
return sigmoidf(z);
}
// Simulated environment
float simTemp = 25.0f;
float simHum = 50.0f;
float simLDR = 0.0f; // start with real sensor value later
//Auto-adjust: move slowly toward comfort
void adjust_environment(float x_ldr, float x_pot, float &simLDR, float &simTemp, float &simHum) {
// Gradually adjust simulated values toward real ones
simLDR += (x_ldr - simLDR) * 0.05f; // 5% step per loop
simTemp += ((x_pot*50.0f) - simTemp) * 0.02f; // scale pot → temp 0..50
simHum += ((x_pot*100.0f) - simHum) * 0.01f; // scale pot → hum 0..100
// Clamp
if(simLDR > 1.0f) simLDR = 1.0f;
if(simTemp > 50.0f) simTemp = 50.0f;
if(simHum > 100.0f) simHum = 100.0f;
if(simLDR < 0.0f) simLDR = 0.0f;
if(simTemp < 0.0f) simTemp = 0.0f;
if(simHum < 0.0f) simHum = 0.0f;
}
void setup() {
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
analogReadResolution(12);
Serial.begin(9600);
}
void loop() {
// Read actual sensors
float x_ldr = analogRead(ldrPin) / ADC_MAX;
float x_pot = analogRead(potPin) / ADC_MAX;
// Compute comfort probability using simulated environment
float normTemp = simTemp / 50.0f;
float normHum = simHum / 100.0f;
float p = logistic_predict(x_ldr, x_pot, normTemp, normHum);
bool class1 = (p >= 0.5f);
// LEDs: red shows until comfort reached
digitalWrite(ledGreen, class1 ? HIGH : LOW);
digitalWrite(ledRed, class1 ? LOW : HIGH);
// Debug
Serial.print("LDR="); Serial.print(x_ldr,3);
Serial.print(" POT="); Serial.print(x_pot,3);
Serial.print(" TEMP="); Serial.print(normTemp,3);
Serial.print(" HUM="); Serial.print(normHum,3);
Serial.print(" P="); Serial.println(p,3);
// Slowly adjust simulated environment toward comfort
adjust_environment(x_ldr, x_pot, simLDR, simTemp, simHum);
delay(500); // slower loop so red LED is visible
}
//-----------------------------------------------*/
/*
//woooorking
#include <Arduino.h>
#include <math.h>
// Pins
const int ldrPin = A1; // LDR
const int potPin = A2; // Potentiometer
const int ledGreen = D5; // Green LED
const int ledRed = D6; // Red LED
// Logistic regression weights
const float W_LDR = -0.954f;
const float W_POT = -1.900f;
const float W_TEMP = 2.0f;
const float W_HUM = -1.0f;
const float BIAS = 1.0f;
#define ADC_MAX 4095.0f
// Sigmoid function
float sigmoidf(float x) { return 1.0f / (1.0f + exp(-x)); }
// Logistic regression prediction
float logistic_predict(float x_ldr, float x_pot, float temp, float hum) {
float z = W_LDR * x_ldr + W_POT * x_pot + W_TEMP * temp + W_HUM * hum + BIAS;
return sigmoidf(z);
}
// Simulated environment
float simTemp = 25.0f;
float simHum = 50.0f;
float simLDR = 0.0f;
// Flag to print comfort message only once
bool comfortPrinted = false;
// Auto-adjust: move reliably toward comfort
void adjust_environment(float x_ldr, float x_pot, float &simLDR, float &simTemp, float &simHum) {
float targetTemp = x_pot * 50.0f; // desired temperature
float targetHum = x_pot * 100.0f; // desired humidity
// Minimum step per loop
//float stepTemp = 1.0f; // °C per loop
// float stepHum = 2.0f; // % per loop
float stepTemp = 5.0f; // 5°C per loop
float stepHum = 8.0f; // 8% humidity per loop
simLDR += (x_ldr - simLDR) * 0.6f;
// Adjust Temp
if(abs(targetTemp - simTemp) < stepTemp)
simTemp = targetTemp;
else
simTemp += (targetTemp > simTemp) ? stepTemp : -stepTemp;
// Adjust Hum
if(abs(targetHum - simHum) < stepHum)
simHum = targetHum;
else
simHum += (targetHum > simHum) ? stepHum : -stepHum;
// Adjust LDR smoothly
//simLDR += (x_ldr - simLDR) * 0.2f;
// Clamp values
simTemp = constrain(simTemp, 0.0f, 50.0f);
simHum = constrain(simHum, 0.0f, 100.0f);
simLDR = constrain(simLDR, 0.0f, 1.0f);
}
void setup() {
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
analogReadResolution(12);
Serial.begin(9600);
}
void loop() {
// Read actual sensors
float x_ldr = analogRead(ldrPin) / ADC_MAX;
float x_pot = analogRead(potPin) / ADC_MAX;
// Normalize simulated environment for logistic regression
float normTemp = simTemp / 50.0f;
float normHum = simHum / 100.0f;
// Compute comfort probability
float p = logistic_predict(x_ldr, x_pot, normTemp, normHum);
bool class1 = (p >= 0.5f); // true → comfort reached
// Control LEDs
digitalWrite(ledGreen, class1 ? HIGH : LOW);
digitalWrite(ledRed, class1 ? LOW : HIGH);
// Auto-adjust environment if comfort not reached
if(!class1) {
adjust_environment(x_ldr, x_pot, simLDR, simTemp, simHum);
comfortPrinted = false; // reset message if comfort lost
}
else if(!comfortPrinted) {
Serial.println(">>> Comfort reached! <<<");
comfortPrinted = true; // print only once
}
// Debug print
Serial.print("LDR="); Serial.print(x_ldr, 3);
Serial.print(" POT="); Serial.print(x_pot, 3);
Serial.print(" TEMP="); Serial.print(normTemp, 3);
Serial.print(" HUM="); Serial.print(normHum, 3);
Serial.print(" P="); Serial.println(p, 3);
delay(500); // slow loop for visibility
}
*/
/////////////////////////////////////////////
/*
#include <Arduino.h>
#include <math.h>
#include <Servo.h>
// Pins
const int ldrPin = A1; // LDR (PA1)
const int potPin = A2; // Potentiometer (PA2)
const int ledGreen = D5; // Green LED
const int ledRed = D6; // Red LED
const int servoPin = D9; // SERVO PWM PIN
// Logistic regression weights
const float W_LDR = -0.954f;
const float W_POT = -1.900f;
const float W_TEMP = 2.0f;
const float W_HUM = -1.0f;
const float BIAS = 1.0f;
#define ADC_MAX 4095.0f
Servo fan;
// Sigmoid
float sigmoidf(float x) { return 1.0f / (1.0f + exp(-x)); }
// Logistic regression
float logistic_predict(float x_ldr, float x_pot, float temp, float hum) {
float z = W_LDR * x_ldr + W_POT * x_pot + W_TEMP * temp + W_HUM * hum + BIAS;
return sigmoidf(z);
}
// Simulated environment
float simTemp = 25.0f;
float simHum = 50.0f;
float simLDR = 0.0f;
bool comfortPrinted = false;
// Faster environment adjustment
void adjust_environment(float x_ldr, float x_pot, float &simLDR, float &simTemp, float &simHum) {
float targetTemp = x_pot * 50.0f;
float targetHum = x_pot * 100.0f;
float stepTemp = 5.0f;
float stepHum = 8.0f;
// LDR smooth approach
simLDR += (x_ldr - simLDR) * 0.6f;
if(abs(targetTemp - simTemp) < stepTemp)
simTemp = targetTemp;
else
simTemp += (targetTemp > simTemp) ? stepTemp : -stepTemp;
if(abs(targetHum - simHum) < stepHum)
simHum = targetHum;
else
simHum += (targetHum > simHum) ? stepHum : -stepHum;
simTemp = constrain(simTemp, 0.0f, 50.0f);
simHum = constrain(simHum, 0.0f, 100.0f);
simLDR = constrain(simLDR, 0.0f, 1.0f);
}
void setup() {
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
analogReadResolution(12);
Serial.begin(9600);
fan.attach(servoPin); // MUST BE D9
}
void loop() {
float x_ldr = analogRead(ldrPin) / ADC_MAX;
float x_pot = analogRead(potPin) / ADC_MAX;
float normTemp = simTemp / 50.0f;
float normHum = simHum / 100.0f;
float p = logistic_predict(x_ldr, x_pot, normTemp, normHum);
bool class1 = (p >= 0.5f);
digitalWrite(ledGreen, class1 ? HIGH : LOW);
digitalWrite(ledRed, class1 ? LOW : HIGH);
// --- SERVO CONTROL ---
bool comfort = (p >= 0.5f);
if (!comfort) {
// NON COMFORT → FAN ON
fan.write(90); // move to 90° (visible movement)
} else {
// COMFORT → FAN OFF
fan.write(0); // return to off
}
Serial.print("LDR="); Serial.print(x_ldr, 3);
Serial.print(" POT="); Serial.print(x_pot, 3);
Serial.print(" TEMP="); Serial.print(normTemp, 3);
Serial.print(" HUM="); Serial.print(normHum, 3);
Serial.print(" P="); Serial.println(p, 3);
delay(500);
}
*/
#include <Arduino.h>
#include <math.h>
// Pins
const int ldrPin = A1; // LDR (PA1)
const int potPin = A2; // Potentiometer (PA2)
const int ledGreen = D5; // Green LED
const int ledRed = D6; // Red LED
const int stepPin = D7; // STEP
const int dirPin = D8; // DIR
// Logistic regression weights
const float W_LDR = -0.954f;
const float W_POT = -1.900f;
const float W_TEMP = 2.0f;
const float W_HUM = -1.0f;
const float BIAS = 1.0f;
#define ADC_MAX 4095.0f
// Sigmoid function
float sigmoidf(float x) { return 1.0f / (1.0f + exp(-x)); }
// Logistic regression prediction
float logistic_predict(float x_ldr, float x_pot, float temp, float hum) {
float z = W_LDR * x_ldr + W_POT * x_pot + W_TEMP * temp + W_HUM * hum + BIAS;
return sigmoidf(z);
}
// Simulated environment variables
float simTemp = 25.0f;
float simHum = 50.0f;
float simLDR = 0.0f;
// Stepper control function
void stepMotor(bool dir) {
digitalWrite(dirPin, dir ? HIGH : LOW);
digitalWrite(stepPin, HIGH);
delay(2);
digitalWrite(stepPin, LOW);
delay(2);
}
// Auto-regulation: adjusts environment gradually (proportional control)
void auto_regulate(float x_ldr, float x_pot, float &simLDR, float &simTemp, float &simHum) {
// Define target values from inputs
float targetTemp = x_pot * 50.0f; // Pot controls target temperature
float targetHum = x_pot * 100.0f; // Pot controls target humidity
float targetLDR = x_ldr; // LDR input is target light
// Proportional step sizes
float stepTemp = 0.1f * (targetTemp - simTemp);
float stepHum = 0.1f * (targetHum - simHum);
float stepLDR = 0.05f * (targetLDR - simLDR);
// Apply gradual adjustments
simTemp += stepTemp;
simHum += stepHum;
simLDR += stepLDR;
// Constrain values within physical limits
simTemp = constrain(simTemp, 0.0f, 50.0f);
simHum = constrain(simHum, 0.0f, 100.0f);
simLDR = constrain(simLDR, 0.0f, 1.0f);
}
void setup() {
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
analogReadResolution(12); // 12-bit ADC
Serial.begin(9600);
}
void loop() {
// Read sensors
float x_ldr = analogRead(ldrPin) / ADC_MAX;
float x_pot = analogRead(potPin) / ADC_MAX;
// Normalize simulated environment
float normTemp = simTemp / 50.0f;
float normHum = simHum / 100.0f;
// Compute comfort probability
float p = logistic_predict(x_ldr, x_pot, normTemp, normHum);
bool comfort = (p >= 0.5f);
// LED indicators
digitalWrite(ledGreen, comfort ? HIGH : LOW);
digitalWrite(ledRed, comfort ? LOW : HIGH);
// Auto-regulation: always try to reach comfort zone
auto_regulate(x_ldr, x_pot, simLDR, simTemp, simHum);
// Stepper control if not comfortable
if (!comfort) stepMotor(true);
// Serial output for debugging
Serial.print("LDR="); Serial.print(simLDR, 3);
Serial.print(" POT="); Serial.print(x_pot, 3);
Serial.print(" TEMP="); Serial.print(simTemp, 3);
Serial.print(" HUM="); Serial.print(simHum, 3);
Serial.print(" P="); Serial.println(p, 3);
delay(500);
}