//Pushbutton switch Parameters
int pushbutton = 2; //pushbutton pin
int pushbutton_light = 7; //pushbutton light pin
//Potentiometer Parameters
int knobe = A2; //knobe pin
int dimmer_led = 10; //pin of light to be dimmmed
// Light sensor Parameters
int light_sensor = 3; // DO pin of light sensor
int light_sensor_light = 5; //light 'controlled' by light sensor
//Ultrasound Parameters
int trigPin = 9; // TRIG pin
int echoPin = 8; // ECHO pin
int ultrasound_light = 4; //Ultrasound light pin
float duration_us, distance_cm;
void setup() {
// begin serial port
Serial.begin (9600);
//pushbutton switch
pinMode(pushbutton,INPUT);
pinMode(pushbutton_light,OUTPUT);
//Potentiometer
pinMode(knobe, INPUT);
pinMode(dimmer_led, OUTPUT);
//LDR sensor
pinMode(light_sensor,INPUT); // LDR Sensor output pin connected
pinMode(light_sensor_light,OUTPUT); // light sensor LED PIN
//Ultrasound sensor
pinMode(trigPin, OUTPUT); // configure the trigger pin to output mode
pinMode(echoPin, INPUT); // configure the echo pin to input mode
pinMode(ultrasound_light,OUTPUT); // ultrasounbd LED pin
}
void loop() {
/* Code for digital */
if (digitalRead(pushbutton) == 0){ //if push-button is NOT pressed, LOW signal sent
digitalWrite(pushbutton_light,LOW); // turn LED off
}
else{ // if push button is pressed , HIGH signal sent
digitalWrite(pushbutton_light,HIGH);// turn LED on
}
/* Code for analog */
analogWrite(10,analogRead(A2)/4); // Read the analog input from potentiometer
// Then write that analog input to the light
// Why divide by 4?
// analogRead is 10-bit because Arduino ADC (Analog to Digital converter) is 10-bit value
// analogWrite is 8-bit. The PWM (Pulse-Width Modulation) timer is a 8-bit value
/* Code for LDR sensor*/
if(digitalRead(light_sensor) == 0 ) //if light_sensor_do gives a LOW signal, aka it is dark
{
digitalWrite(light_sensor_light,LOW); // turn LED ON
}
else // if light_sensor_do gives a HIGH signal, aka it is bright
{
digitalWrite(light_sensor_light,HIGH); // turn LED OFF
}
// generate 10-microsecond pulse to TRIG pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// measure duration of pulse from ECHO pin
duration_us = pulseIn(echoPin, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us; //unit conversion
if (distance_cm <= 10.0){
digitalWrite(ultrasound_light, HIGH);
}
if (distance_cm > 10.0){
digitalWrite(ultrasound_light, LOW);
}
// print the value to Serial Monitor
Serial.print("distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
delay(10);
}