EasyManua.ls Logo

SunFounder 3in1 Kit - Page 149

Default Icon
351 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder 3in1 Kit
Open the 5.4.interval.ino file under the path of 3in1-kit\learning_project\5.4.interval.
Or copy this code into Arduino IDE.
After the code is uploaded successfully, the buzzer will play music; whenever you press the button, the LED will light
up. The work of LED and buzzer does not interfere with each other.
How it works?
Initial a variable named previousMillis to store previous operating time of microcontroller.
unsigned long previousMillis = 0;
Mark which note is played.
int thisNote=0;
The interval time of each note.
long interval = 1000;
In loop(), declare currentMillis to store the current time.
unsigned long currentMillis = millis();
When the interval between the current operating time and last updating time is larger than 1000ms, certain functions are
triggered. Meanwhile, update the previousMillis to the current time for the next triggering that is to happen 1 second
latter.
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;// save the last time of the last tone
//...
}
Play the notes in the melody one by one.
tone(buzzerPin,melody[thisNote],100);
interval=1000/noteDurations[thisNote]; // interval at which to tone
thisNote=(thisNote+1)%(sizeof(melody)/2); //iterate over the notes of the melody
The button control the LED.
// play button & led
digitalWrite(ledPin,digitalRead(buttonPin));
4.5.5 5.5 Use Internal Library
In the Arduino IDE, you can use many built-in libraries by adding the corresponding .h file directly to your code.
This project uses the Servo library to drive the Servo, so that it can rotate between 0° and 180°.
Required Components
In this project, we need the following components.
Its definitely convenient to buy a whole kit, here’s the link:
4.5. 5. More Syntax 145

Related product manuals