Arduino E1
Arduino E1
------------------------------------------------------------------
Designing and implementing prototypes using
software and microprocessor platforms.
Task_0:
Load example Blink.
LED
Code_1:
void setup() {
pinMode(9, OUTPUT); // Configure pin 9 as an output
}
void loop() {
digitalWrite(9, HIGH); // Turn on the LED
delay(1000); // Wait for 1 second
digitalWrite(9, LOW); // Turn off the LED
delay(1000); // Wait for 1 second
}
The code above uses the delay function to introduce a delay. This
function takes a single argument, which specifies the number of
milliseconds to wait before continuing.
It's worth noting that the use of the delay function can sometimes
cause problems in more complex programs. This is because delay
pauses the entire program, preventing it from doing anything else
during the pause.
Task_1:
You need to connect the LED and test different delays between
state changes.
https://wiki.dfrobot.com/Digital_Buzzer_Module__SKU__DFR0032_
Another option for using the output signal is for an audio signal.
The simplest solution is to use a buzzer from a ready-made
solution such as DFrobot (on the left) or by connecting a generic
buzzer in series with a resistor (on the right).
Task_2:
Test code_1 in the variant with a buzzer connected.
If a sound output signal is used, it is possible to play music
notes by modulating the signal accordingly. However, creating a
library containing all the notes would be very time-consuming.
Built-in examples come to the rescue in this case.
We can see that in this case, the code consists of two files: the
main one - toneMelody and a library created specifically for this
code - pitches.h.
The main code contains the
➔ implementation of two arrays:
◆ melody[] containing the corresponding notes (note
positions on a staff) and
◆ noteDurations[] containing the duration values of the
notes (note types).
➔ A for loop is executed only once (to hear the melody again,
the code must be uploaded again or the reset button on the
Arduino board must be pressed directly) by converting the
length of the note into values in seconds and retrieving the
notes from the library using the command melody[thisNote].
To play the melody in a loop, move the for loop from void setup()
to void loop().
Task_3:
Test the example and prepare a melody in the way that suits you
(also check the code with a melody from the internet, e.g.
https://github.com/robsoncouto/arduino-songs/blob/master/supermari
obros/supermariobros.ino). However, pay attention to the pin
number assigned to the buzzer.
void setup() {
pinMode(8, OUTPUT); //LED as output
pinMode(5, INPUT_PULLUP); //Button as input with internal pull-up resistor
digitalWrite(8, LOW); //Turn off the LED
}
void loop()
{
if (digitalRead(5) == LOW) { //If the button is pressed
digitalWrite(8, HIGH); //Turn on the LED
} else { //If the condition is not met (button is not pressed)
digitalWrite(8, LOW); //Turn off the LED
}
}
Task_4:
Testing the above connection along with the code.
2)UART
void setup(){
Serial.begin(9600); //Ustawienie prędkości transmisji
Serial.println("Start programu"); //Jednorazowe wysłanie tekstu
}
void loop() {
delay(1000);
Serial.println("minęła sekunda"); //Wysyłanie w pętli
}
Receiving data in the Serial Monitor
void setup() {
Serial.begin(9600); //Uruchomienie komunikacji
}
void loop() {
if(Serial.available() > 0) { //Czy Arduino odebrało dane
odebraneDane = Serial.readStringUntil('\n'); //Jeśli tak, to odczytaj je do znaku
końca linii i zapisz w zmiennej odebraneDane
Serial.println("Witaj " + odebraneDane + "!"); //Wyświetl komunikat
}
}
Task_5:
Write code that on "start" will start incrementing the variable,
while on "stop" it will display it, and then reset it to be ready
for the repeated operation.
Serial.print(Wartosc1);
Serial.print("\t"); // Znak tabulatora
Serial.print(Wartosc2);
Serial.print("\t"); // Znak tabulatora
Serial.print(Wartosc3);
Serial.print("\t"); // Znak tabulatora
Serial.print(Wartosc4);
Serial.println(""); // Przejście do nowej linii
#define
#define ledPin 8
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
3)Analog values
Arduino pins signals can be divided into two types - digital and
analog. This means that the data transmitted and received can be
in the form of low and high states (0 - 0V or 1 - 5V) or
intermediate values ranging from 0 - 5V.
Among the available sensors and elements that use analog pins, we
can distinguish two types - elements based on resistance change
that require the use of a voltage divider (such as a
photoresistor), and ready-made modules that properly process the
signal and provide an analog value (such as a microphone).
Reading analog values from a voltage divider can also serve as
user input in the form of changes in the position of a
potentiometer.
void setup() {
Serial.begin(9600); // initialize serial communication at 9600 bits per second
}
void loop() {
potVal = analogRead(potPin); // read the value from the potentiometer
Serial.println(potVal); // print the potentiometer value to the serial monitor
delay(100); // wait for 100ms before reading the potentiometer again
}
Task_6:
Knowing how the values are mapped, write a code that allows you to
display the value of the read voltage (a simple voltmeter). You
can do it by writing a linear function that converts the values,
or you can familiarize yourself with the operation of the map
function.
(https://reference.arduino.cc/reference/en/language/functions/math
/map/)
The voltage divider should be connected in such a way that one end
is connected to the power supply voltage, and the other end to
ground. Between them, two resistors should be connected, with the
fixed resistor connected to the power supply voltage, and the
variable resistor connected to ground. The voltage at the junction
of the resistors will be proportional to the amount of light
hitting the photoresistor.
Task_8:
Choose at least two of the sensors below and test their
functioning.
int ledPin = 13; // Connect LED on pin 13, or use the onboard one
int KEY = 2; // Connect Touch sensor on Digital Pin 2
void setup(){
pinMode(ledPin, OUTPUT); // Set ledPin to output mode
pinMode(KEY, INPUT); //Set touch sensor pin to input mode
}
void loop(){
if(digitalRead(KEY)==HIGH) { //Read Touch sensor signal
digitalWrite(ledPin, HIGH); // if Touch sensor is HIGH, then turn on LED
}
else{
digitalWrite(ledPin, LOW); // if Touch sensor is LOW, then turn off LED
}
}
Joystick
https://wiki.dfrobot.com/Joystick_Module_For_Arduino_SKU_DFR0061
void setup()
{
pinMode(JoyStick_Z, INPUT);
Serial.begin(9600); // 9600 bps
}
void loop()
{
int x,y,z;
x=analogRead(JoyStick_X);
y=analogRead(JoyStick_Y);
z=digitalRead(JoyStick_Z);
Serial.print(x ,DEC);
Serial.print(",");
Serial.print(y ,DEC);
Serial.print(",");
Serial.println(z ,DEC);
delay(100);
}
Vibration Sensor
https://wiki.dfrobot.com/DFRobot_Digital_Vibration_Sensor_V2_SKU_D
FR0027
#define SensorLED 13
//Connect the sensor to digital Pin 3 which is Interrupts 1.
#define SensorINPUT 3
unsigned char state = 0;
void setup() {
pinMode(SensorLED, OUTPUT);
pinMode(SensorINPUT, INPUT);
//Trigger the blink function when the falling edge is detected
attachInterrupt(1, blink, FALLING);
}
void loop() {
if (state != 0) {
state = 0;
digitalWrite(SensorLED, HIGH);
delay(500);
}
else
digitalWrite(SensorLED, LOW);
}
//Interrupts function
void blink() {
state++;
}
Tilt Sensor
void setup()
{
pinMode(ledPin, OUTPUT); // Set digital pin 13 to output mode
pinMode(switcher, INPUT); // Set digital pin 3 to input mode
}
void loop()
{
https://wiki.dfrobot.com/Digital_Infrared_motion_sensor__SKU_SEN00
18_
Steam Sensor
https://wiki.dfrobot.com/Steam_Sensor__SKU_SEN0121_
/*******************************
Connection:
VCC-5V
GND-GND
S-Analog pin 0
void setup()
{
Serial.begin(9600);// open serial port, set the baud rate to 9600 bps
}
void loop()
{
int sensorValue;
sensorValue = analogRead(0); //connect Steam sensors to Analog 0
Serial.println(sensorValue); //print the value to serial
delay(200);
}
Soil Moisture Sensor
https://wiki.dfrobot.com/Moisture_Sensor__SKU_SEN0114_
/*
# Example code for the moisture sensor
# Editor : Lauren
# Date : 13.01.2012
# Version : 1.0
# Connect the sensor to the A0(Analog 0) pin on the Arduino board
# the sensor value description
# 0 ~300 dry soil
# 300~700 humid soil
# 700~950 in water
*/
void setup(){
Serial.begin(57600);
}
void loop(){
https://wiki.dfrobot.com/Triple_Axis_Accelerometer_MMA7361_SKU_DFR
0143
// # Description:
// # This sample shows how to measure the angle value using two axis value
#include<math.h>
#include<stdio.h>
#define A_X 5
#define A_Y 4
#define A_Z 3
int val_x,val_y,val_z;
double b;
void setup()
{
pinMode(A_X,INPUT);
pinMode(A_Y,INPUT);
pinMode(A_Z,INPUT);
Serial.begin(9600);
}
void loop()
{
float a;
for (int i=0;i<10;i++)
{
val_x+=analogRead(A_X);delay(2);
val_y+=analogRead(A_Y);delay(2);
val_z+=analogRead(A_Z);delay(2);
}
val_x=val_x/10;
val_y=val_y/10;
val_z=val_z/10;
delay(300);
Serial.print(" X_Axis: ");
Serial.print(val_x);
Serial.print(" Z_Axis: ");
Serial.print(val_z);
Serial.print(" ");
b=(double) (abs(val_x-320))/(abs(val_z-320));
Serial.print(" B: ");
Serial.print(b);
Serial.print(" ");
a=atan(b);
Serial.print(" A: ");
Serial.println(a/3.14*180); //the value of Angle
val_x=0;
val_y=0;
val_z=0;
}
Flame Sensor
https://wiki.dfrobot.com/Flame_sensor_SKU__DFR0076
/*
# Product: Flame sensor
# SKU : DFR0076
# Description:
# When flame sensor detected flame, the data will be read by the serial.
*/
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
}
Sound Sensor
https://wiki.dfrobot.com/Analog_Sound_Sensor_SKU__DFR0034
void setup()
{
Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}
void loop()
{
int val;
val=analogRead(0); //connect mic sensor to Analog 0
Serial.println(val,DEC);//print the sound value to serial
delay(100);
}
To control an LED with PWM using Arduino, you will need an Arduino
board with at least one PWM-enabled pin. Here's a step-by-step
guide:
void setup() {
pinMode(ledPin, OUTPUT); // Set the pin as output
}
void loop() {
analogWrite(ledPin, 128); // Set brightness to half (value 0-255)
delay(1000); // Wait for 1 second
analogWrite(ledPin, 255); // Set full brightness
delay(1000); // Wait for 1 second
}
To control a servo using PWM, you will need an Arduino board with
at least one PWM-enabled pin. Here are the steps you need to
follow:
#include <Servo.h>
void setup() {
myservo.attach(9); // Assign PWM pin to the servo
}
void loop() {
myservo.write(90); // Set angle to 90 degrees
delay(1000); // Wait for 1 second
myservo.write(180); // Set angle to 180 degrees
delay(1000); // Wait for 1 second
}
Task_9:
Program the LED brightness control and servo position depending on
the selected analog sensor.
Exercise 1.1 (grade 3)
Remember to take into account that before turning on the red light, the
yellow light should stay on longer than before switching to green.
Program the control of 3 LEDs through text commands entered in the Serial
Port Monitor. The command has two values - led color and lighting duration.