#include <Adafruit_SoftServo.h>
Adafruit_SoftServo myservo;
int fotoweerstandPin = A0; // Analoge pin waar de fotoweerstand is aangesloten
int ledPin1 = 9; // Digitale pin voor LED 1 (bijvoorbeeld)
int ledPin2 = 10; // Digitale pin voor LED 2 (bijvoorbeeld)
int ledPin3 = 11; // Digitale pin voor LED 3 (bijvoorbeeld)
int knop1 = 2;
int currentButtonState;
int lastButtonState = LOW; // Initialize last button state
int ledState1 = LOW; // Initialize LED state
int helderheidLicht;
int blauwPin = 6; // Digitale pin voor rood van de RGB LED
int groenPin = 7; // Digitale pin voor groen van de RGB LED
int roodPin = 8; // Digitale pin voor blauw van de RGB LED
int currentbuttonstate2;
int lastbuttonstate2 = LOW; // Initialize last button state
int knop2 = 3;
int temperatuurPin = A1; // Analoge pin waar de temperatuursensor is aangesloten
int count = 1;
int knop3 = 4;
int currentbuttonstate3;
int lastbuttonstate3;
int potPin = A0; // Analog pin for the potentiometer
int potValue; // Variable to store potentiometer value
int servoAngle; // Variable to store servo angle
int servoPosition = 0;
void setup() {
myservo.attach(5);
Serial.begin(9600);
pinMode(ledPin1, OUTPUT); // Stel de LED pinnen in als uitvoer
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(knop1, INPUT); // Set button pin as input
pinMode(roodPin, OUTPUT); // Stel de RGB LED pinnen in als uitvoer
pinMode(groenPin, OUTPUT);
pinMode(blauwPin, OUTPUT);
pinMode(knop2, INPUT);
analogWrite(blauwPin, 0);
analogWrite(groenPin, 0);
analogWrite(roodPin, 0);
pinMode(temperatuurPin, INPUT); // Stel de pin van de temperatuursensor in als invoer
pinMode(knop3, INPUT);
currentbuttonstate3 = digitalRead(knop3);
}
void loop() {
// Button 1: Control LEDs with brightness based on light sensor
lastButtonState = currentButtonState;
currentButtonState = digitalRead(knop1);
// Toggle LED state when button is pressed
if (lastButtonState == LOW && currentButtonState == HIGH) {
ledState1 = !ledState1; // Toggle LED state
Serial.println(ledState1 ? "Turning LEDs on" : "Turning LEDs off");
}
// If LEDs are on, adjust brightness
if (ledState1) {
// Lichtsensorregeling // Lees het omgevingslichtniveau van de fotoweerstand
int lichtNiveau = analogRead(fotoweerstandPin);
helderheidLicht = map(lichtNiveau, 8, 1016, 0, 255);
// Update LED brightness
analogWrite(ledPin1, helderheidLicht);
analogWrite(ledPin2, helderheidLicht);
analogWrite(ledPin3, helderheidLicht);
} else {
// If LEDs are off, turn them off
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
//button 2: temp
int temperatuur = analogRead(temperatuurPin); // Lees de temperatuurwaarde van de sensor
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
int analogValue = temperatuur;
float celsius = (1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15);
// Check for button press
currentbuttonstate2 = digitalRead(knop2);
if (lastbuttonstate2 == LOW && currentbuttonstate2 == HIGH) {
count++; // Increment count
if (count > 4) {
count = 1; // Reset count if it exceeds 4
}
}
lastbuttonstate2 = currentbuttonstate2; // Update last button state
// Handle LED color based on count
if (count == 1) {
Serial.println("Blue RGB");
analogWrite(blauwPin, 255);
analogWrite(groenPin, 0);
analogWrite(roodPin, 0);
} else if (count == 2) {
Serial.println("Green RGB");
analogWrite(blauwPin, 0);
analogWrite(groenPin, 255);
analogWrite(roodPin, 0);
} else if (count == 3) {
Serial.println("Red RGB");
analogWrite(blauwPin, 0);
analogWrite(groenPin, 0);
analogWrite(roodPin, 255);
} else if (count == 4) {
Serial.println("temp_sensitive");
// Handle LED color based on temperature
if (celsius >= -20 && celsius <= 0) {
// Onder 20 graden Celsius, LED blauw
analogWrite(blauwPin, 255);
analogWrite(groenPin, 0);
analogWrite(roodPin, 0);
} else if (celsius > 0 && celsius <= 40) {
// Tussen 20 en 40 graden Celsius, LED groen
analogWrite(blauwPin, 0);
analogWrite(groenPin, 255);
analogWrite(roodPin, 0);
} else if (celsius > 40) {
// Boven 40 graden Celsius, LED rood
analogWrite(blauwPin, 0);
analogWrite(groenPin, 0);
analogWrite(roodPin, 255);
}
}
potValue = analogRead(potPin); // Read potentiometer value (0-1023)
servoAngle = map(potValue, 0, 1023, 0, 180); // Map potentiometer value to servo angle (0-180 degrees)
lastbuttonstate3 = currentbuttonstate3;
currentbuttonstate3 = digitalRead(knop3);
if (lastbuttonstate3 == LOW && currentbuttonstate3 == HIGH) {
if (servoPosition == 0) {
myservo.write(180);
servoPosition = 180;
Serial.println("180°");
} else {
myservo.write(0);
servoPosition = 0;
Serial.println("0°");
}
}
myservo.refresh();
}