LQFP32 - programs
1. TRY THIS: Test serial port communication. Copy and run the following program. Observe serial monitor. Make sure you select 9600 baud in serial monitor window.
void setup() {
Serial.begin(9600);
}
int i = 0;
void loop() {
Serial.print("Hello ");
Serial.println(i++);
delay(1000);
}
YOUR TASK: change the program so that after reaching 100, it will start counting in decreasing order, until 0 and then again to 100, etc. Use the operator for decrementing (i--) and if condition:
if (condition)
{
commands to be executed when condition is satisfied
}
2. TRY THIS: Test internal LED flashing:
#define LED 13
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(100);
}
YOUR TASK: change the program so that it will be showing SOS in Morse code.
3. TRY THIS: Change the defined constant LED in the previous program from 13 to 4 and connect an external LED to pin 4, make sure your external LED is flashing the correct message. Important: External LED must be connected in series with resistor! Use breadboard. The following picture explains how breadboard pins are connected inside:
(source https://learn.pimoroni.com/article/anatomy-of-a-mini-breadboard)
How to connect: Notice that LED has two legs - the longer one (anode) connects to + (i.e. to the pin D4 of), while the shorter one (cathode) connects to one leg of resistor, and the other leg of resistor connects to ground (GND pin on the microcontroller).
(source https://www.switchelectronics.co.uk/blog/post/ledpolarity.html)
YOUR TASK: connect 3 LEDs (red, yello, green) independently (each one needs its own resistor in series) to three different pins, and create a program that works like traffic lights: first shows red, then red and yellow together, then only green, then only yellow, and finally red again to start over from the beginning.
4. TRY THIS: Test read from touch sensor (the red part that has a little pad with a label TOUCH) and IR sensor (the blue narrow part with two LEDs - black one and transparent one). Both sensors work the same and have the same connections:
- GND - connects to GND on microcontroller
- VCC - connects to 5V on microcontroller
- OUT - digital output - connects to a digital pin of your preference (such as D10) - use that number in the code below
#define IRSENSOR 10
void setup() {
Serial.begin(9600);
pinMode(IRSENSOR, INPUT);
}
void loop() {
int sensor = digitalRead(IRSENSOR);
Serial.println(sensor);
delay(300);
}
YOUR TASK: Modify the program in such a way that each time the touch sensor is pressed, the message "click" will be sent to serial console and the internal LED will show light for half a second. If you are fast, try to improve the program to distinguish between a "click" and a "double-click". When double-click is pressed, no click event should be reported.
5. TRY THIS: Insert the black buzzer to breadboard. Connect the pin marked + with some digital pin of microcontroller, and the other one with GND pin. Test the following program. What will happen? Why? How does it work? Note: you will need to send "something" to microcontroller to hear anything.
#define BUZZER 7
#define LED 13
void setup() {
pinMode(BUZZER, OUTPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void pipip()
{
for (int i = 0; i < 400; i++)
{
digitalWrite(BUZZER, HIGH);
delayMicroseconds(800);
digitalWrite(BUZZER, LOW);
delayMicroseconds(800);
}
}
void loop()
{
if (Serial.available())
{
char c = Serial.read();
if (c == 'p') pipip();
}
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(300);
}
YOUR TASK: Find the musical tones frequencies table on the internet. Create a program that can play some simple melody consisting of at least 5 tones.