User Manual

Software Code
Open Arduino IDE.
Copy the following code, click Verify to check for syntax errors. Verify that there are no
errors, and you can upload the code.
//light Induction Desk Lamp
int soundPin = A2; // Analog sound sensor is to be attached to analog
int lightPin = A6; //Analog light sensor is to be attached to analog
int ledPin = 4; // Digital LED is to be attached to digital
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop(){
int soundState = analogRead(soundPin); // Read sound sensor’s value
int lightState = analogRead(lightPin); // Read light sensor’s value
// if the sound sensor's value is greater than 50 or the sound sensor's
is less than 10, the light will be on.
//Otherwise, the light will be turned off
if (soundState > 50 || lightState < 10) {
digitalWrite(ledPin, HIGH);
//delay(5000); //You can delete the "//" to make the LED on for five se
conds
}else{
digitalWrite(ledPin, LOW);
}
}
Code Analysis
if (soundState > 50 || lightState < 10) {
...
}
In parentheses is a logical expression. Both && and || are commonly used in logical expressions.
The common usage is if (expression 1 || expression 2) and if (expression 1 && expression 2).
|| represents “or“, satisfies one of them, the whole expression is true, and satisfies the condition
of the if judgment.
&& means “and“, the statement in if{} is executed only if all expressions in parentheses are true.
Demo Effect and Serial Print Result:
If the surrounding sound is loud enough or light intensity is low, the LED module will light up more
intensity.
65
Grove Beginner Kit For Arduino®