In: Computer Science
THIS IS FOR ARDUINO PROGRAMMING
NEED THE BELOW CODE TO FIT THE ABOVE REQUIREMENTS.
const int lightSensor = 5; //light sensor variable
float sensValue = 0; //variable to hold sensor readings
const int button = 3; //pin for button reads
const int LED1 = 5;
const int LED2 = 8; //
void SerialOutput() {
Serial.print(“Light Sensor Value=”); //send to screen
Serial.print(sensValue, 2); //output variable to screen
Serial.print(“\n”); //drop a line
}
void setup() {
// put your setup code here, to turn once:
pinMode(button, INPUT); //setup button pin for input reads
Serial.begin(9600); //start serial coms
}
void loop() {
// put your main code here, to run repeatedly:
sensValue = analogRead(lightSensor); //read from sensor
SerialOutput ();
delay(1000); //wait for 1 second
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
const int lightSensor = 5; //light sensor variable
float sensValue = 0; //variable to hold sensor readings
const int button = 3; //pin for button reads
const int LED1 = 5;
const int LED2 = 8;
//light sensor value upto which is considered dark, or night
//change this as needed.
const int threshold = 200;
void SerialOutput()
{
Serial.print("Light Sensor Value ="); //send to screen
Serial.print(sensValue, 2); //output variable to screen
Serial.print("\n"); //drop a line
}
void setup()
{
// put your setup code here, to turn once:
pinMode(button, INPUT); //setup button pin for input reads
pinMode(LED1, OUTPUT); //setting LED1 for output
Serial.begin(9600); //start serial coms
}
void loop()
{
sensValue = analogRead(lightSensor); //read from sensor
//if sensorValue is below threshold, it means its dark, or low light
//in which case, we switch ON LED1
if (sensValue <= threshold) {
digitalWrite(LED1, HIGH);
}
//otherwise (value above threshold) we switch OFF LED1
else {
digitalWrite(LED1, LOW);
}
SerialOutput();
delay(1000); //wait for 1 second
}