Uploaded by kmgvv23

arduino solution

advertisement
2022 09 22 08 06 15
CB16715 001
i
1
2
IP 61 42 109 60
Final 2021_A
3
4
5
6
7
8
9
10
11
2021 12 03 15 26
2021 12 03 16 05
12
13
14
15
16
17
18
19
20
21
22
23
38
42
125 00
28 00
22%
The total points of the final exam is 150
The following table shows points assigned to each question.
Midterm A Short Answer Questions 125 points
Q#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
pts
5
7
4
4
7
5
8
8
6
3
6
1
3
5
9
8
5
4
5
2
12
4
4
24
25
26
#
Midterm B Circuit Drawing Questions 25 points
#
#
1
The figure in the bottom shows an Arduino circuit we are going to use In the circuit we placed an LM35 analog sensor to find the temperature in Celsius
and Fahrenheit.
$
5 00
2 00
The output voltage of LM35 increases by 10mV for every 1
temperature increase 0mV in 0
Complete the following sketch
Write a proper function name for
Case Sensitive
!
Write a proper value for
!
1024
"
!
Write a proper variable name for 간 Case Sensitive
tempC
"
void setup() {
}
void loop() {
int reading =
( );
float voltage = reading * 5.0 /
float tempC = voltage *
;
;
float tempF = (간 * 9.0 / 5.0) + 32;
delay(500);
}
2
7 00
2 00
Complete the following sketch playing the sequence of notes in the arrary melody For your information we include the references of
noTone functions in the bottom
Select a proper variable name for
tone
and
간
melody
speakerPin!
noteDuration
melody
speakerPin
noteDuration!
melody
speakerPin
noteDuration
melody
speakerPin
noteDuration!
melody
speakerPin
noteDuration
melody
speakerPin
noteDuration!
melody
speakerPin"
thisNote
noteLength
thisNote
noteLength
thisNote!
noteLength
thisNote
noteLength
간
#include <pitches.h>
int melody[] = {
NOTE_G4, NOTE_G4,
NOTE_G4, NOTE_G4,
NOTE_G4, NOTE_G4,
NOTE_G4, NOTE_E4,
};
int speakerPin = 57;
int noteDuration = 4;
thisNote
noteDuration
noteLength"
thisNote
noteLength
thisNote
noteLength
NOTE_A4, NOTE_A4, NOTE_G4, NOTE_G4, NOTE_E4, 0,
NOTE_E4, NOTE_E4, NOTE_D4, 0,
NOTE_A4, NOTE_A4, NOTE_G4, NOTE_G4, NOTE_E4, 0,
NOTE_D4, NOTE_E4, NOTE_C4, 0
void setup() {
}
void loop() {
for (int thisNote = 0; thisNote < sizeof( ) / sizeof(int); thisNote++) {
int noteLength = 1000 / noteDuration;
tone( ,
[ ],간);
delay( );
noTone( );
}
}
tone()
Description
Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be
specified, otherwise the wave continues until a call to noTone . The pin can be connected to a piezo
buzzer or other speaker to play tones.
Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to
tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.
Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the
Mega).
It is not possible to generate tones lower than 31Hz. For technical details, see Brett Hagman s notes.
Syntax
tone(pin, frequency)
tone(pin, frequency, duration)
Parameters
pin : the pin on which to generate the tone
frequency : the frequency of the tone in hertz - unsigned int
duration : the duration of the tone in milliseconds (optional) - unsigned long
Returns
Nothing
noTone()
Description
Stops the generation of a square wave triggered by tone() . Has no effect if no tone is being generated.
Syntax
noTone(pin)
Parameters
pin : the pin on which to stop generating the tone
Returns
Nothing
3
4 00
2 00
4
4 00
1 00
5
Determine if the given statements for the Arduino circuit below are true or false
1
X
! The circuit diagram is a speaker connection circuit that reproduces only one note at a time
2
O
" The pitches h library is required to operate the speaker of the circuit
3
O
" The playing time can be specified with the Arduino code
4
O
! The circuit converts analog signals to digital through ADC
The following is Sketch code to turn on 4 LEDs using the value of Illuminance sensor
Assuming the input value of the illuminance sensor is 170 which LED is turned on
5 00
1 00
On
!
LED of pin 3
On
!
LED of pin 4
On
"
LED of pin 5
On
!
The following shows the connection of 7 segments of common anode type At this time if 0 0 1 0 0 1 0 1 is given to segmentLEDs as LED status
what number is displayed in 7 Segment
7 00
0 00
6
LED of pin 2
7
!
Fill in the blanks with proper words
The number of control pins to control 7 segments per digit is 8
So basically
" control pins are needed to control 7 segments of 4 digits However if you use the afterimage effect
32
control 7 segments of 4 digits with only
7
8 00
0 00
8
! control pins and 12
12
you can
! digit selection pins
The following shows several patterns common cathode type for in 7 segment
Write down the number corresponding to each pattern
1
2
!
3
!
4
!
!
Fill in the blank with the corresponding output to represent the customized character below.
8 00
#include <LiquidCrystal.h>
LiquidCrystal lcd(44, 45, 46, 47, 48, 49);
byte user1[8] = {
! ,
! ,
! ,
! ,
! ,
! ,
! ,
!
};
void setup(){
lcd.createChar(0, user1);
lcd.begin(16,2);
lcd.clear();
lcd.write(byte(0));
}
void loop(){
analogWrite(A0, 75);
}
9
6 00
4 00
Determine whether the each of following staements is true O or not X
1 The standard servo motor has a 20ms period
X
!
2 Standard servo motor rotates 180 degrees at 50% duty cycle
3 Standard servo motor use 2 lines VCC and GND
X
X
"
"
4 The L293D motor driver requires two separate power supplies
O
"
5 A motor driver is required to select the direction in the standard servo motor
6 With the L293D motor driver you can control two motors separately
10
O
O
!
"
The following figure shows the angle of a servo motor according to PWM signal
3 00
2 00
Fill in the blanks with proper values
What is the frequency of the PWM signals which the servo motor use
50
" Hz
What is the duty cycle in percentage when the angle of a servo motor is 0 degree
What is the duty cycle in percentage when the angle of a servo motor is 45 degree
11
6 00
0 00
5
" %
5
! %
Complete the following sketch that control the speed of a DC motor using a potentiometer The circuit for the sketch is included in the bottom and as
shown in the figure the potentiometer is connected to the pin A0
We want the motor to rotate forward at the highest speed when the input voltage on the pin A0 is 5V but to rotate reverse at the highest speed when
the input is 0V
Enter or choose a proper function name for
digitalWrite
!
Also enter or choose a proper value for
!
!
LOW
!
간
간
!
HIGH
!
int Enable1 = 38; int Speed_PWM = 9; int DIR1 = 39;
void setup() {
pinMode(Enable1, OUTPUT);
pinMode(Speed_PWM, OUTPUT);
pinMode(DIR1, OUTPUT);
digitalWrite(Enable1, HIGH);
}
void loop() {
int reading = analogRead(A0);
int speed =
(reading, 0, 1023,
int speed_pwm = 0;
);
if (speed > 0) {
digitalWrite(DIR1, );
speed_pwm = 255 - speed;
}
else {
digitalWrite(DIR1, 간);
speed_pwm = abs(speed);
}
(Speed_PWM, speed_pwm);
delay(1000);
}
12
The following is a Sketh code for the servo motor control After pressing the keyboard in the order of q > q > w what is the angle value of the servo
motor
1 00
0 00
13
1
15
105
180!
The following is a DC motor connection circuit diagram and Arduino sketch code We are trying to rotate the motor 1 in the forward direction with
maximum speed Choose the appropriate code for the blank
3 00
2 00
HIGH"
14
LOW
HIGH
LOW!
HIGH
LOW"
For each description of UART serial communication determine whether it is true O or not X
UART is supported in hardware level
5 00
3 00
True"
False
Both Arduino Uno and Mega provide 4 UART channel
True!
False
There is a class that emulates UART in software
True"
False
SoftwareSerial class supports 1 N communication
True
False"
To use Serial_ class users should define an object at first
True!
15
False
The following is the code for I2C communication by connecting Arduino Mega 2560 as master and Arduino Uno as slave Fill in the following code
!
9 00
!
!
16
8 00
1 00
The following code is for sending messages to SPI slave from SPI master. Fill in the blanks in the following code.
#include <SPI.h>
int SS_arduino = 53;
int SS_EEPROM = 42;
void setup(void){
pinMode(SS_arduino, OUTPUT);
pinMode(SS_EEPROM,OUTPUT);
digitalWrite(SS_arduino,
HIGH
digitalWrite(SS_EEPROM,
SPI. 111
" );
LOW
! );
// slave is not selected
// slave is not selected
! ();
SPI.setClockDivider(SPI_CLOCK_DIV16);
}
void loop(void){
const char *p = "Hello, World\n";
digitalWrite(SS_arduino, HIGH
! );
// Choose SPI slave
for(int i = 0 ; i < strlen(p); i++){ // Transfer characters
SPI. 111
! (p[i]);
}
digitalWrite(SS_arduino, LOW
! );
// Release SPI slave
delay(1000);
}
s
17
Suppose that one master and three slaves are trying to communicate with SPI Answer the following questions
The number of wires required to be connected to the master
5 00
!
For each slave the number of connections for data transmission
!
For each slave the number of connectinos for clock transmission
For each slave the number of connectinos for control signal
18
5 00
2 00
!
Determine whether the following sentences are true or false
4 00
3 00
19
!
O
! Arduino Uno has a total of 2 pairs of serial communication pins
X
" In Mega2560 UART Serial1 communication uses 9 RX and 18 TX pins
O
" UART is 1 1 communication
O
" UART is asynchronous communication while SPI is synchronous communication
Complete the following sketch that use an HC SR04 ultrasonic sensor to find the distance in mm using an Arduino circuit included in the bottom
The speed of the wave used by the ultrasonic sensor is 340m s
Choose or write a proper value for
INPUT
2
간
"
3
!
1024 0
!
!
void setup() {
}
OUTPUT
pinMode(2,
);
pinMode(3,
);
간
"
void loop() {
digitalWrite( , HIGH);
delay(10);
digitalWrite( , LOW);
float duration = pulseIn( , HIGH);
float distance = duration * 간);
delay(500);
}
20
2 00
! sensors allow you to sense motion almost always used to detect whether a human has moved in or out of the
sensors range They are small inexpensive low power easy to use and don t wear out
! Sensor used in many applications where measuring distance or sensing objects are required
21
12 00
1 00
The following sketch code the master reads the distance from the ultrasonic sensor Afterwards the distance value is transmitted to the slave through
I2C communication and the slave printout the received distance value to the serial monitor The received distance value is printed out as an integer
value
Fill in the blanks with the appropriate code
!
trigPin
!
! 간 echoPin
"
!
!
trigPin
!
!
Master code
Slave code
//Master
#include <Wire.h>
#define SLAVE 4
int trigPin = 3;
int echoPin = 2;
//Slave
#include <Wire.h>
void setup() {
#define SLAVE 4
Wire.begin(SLAVE);
void setup() {
Serial.begin(9600);
Wire.begin(SLAVE);
pinMode(trigPin, OUTPUT);
Wire.onReceive(receiveFromMaster);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
}
void loop(){
void i2c_communication(int reading){
}
Wire.beginTransmission(SLAVE);
Wire.write( 1 );
int bytes2int(byte a, byte b){
Wire.write( 2 );
return ( 7 ) + 8 ;
Wire.endTransmission(SLAVE);
}
}
void receiveFromMaster(int bytes){
byte r1 = Wire.read();
void loop() {
byte r2 = Wire.read();
digitalWrite( 3 , HIGH );
int value = bytes2int(r1, r2);
delay( 4 );
Serial.println(value);
digitalWrite( 5 , LOW );
}
float duration = pulseIn( 6 , HIGH );
float distance = duration * 340 / 10000 / 2;
i2c_communication(int(distance));
delay(500);
}
22
4 00
The following is a description for the ultrasonic sensor Fill in the Pin name field
Pin Name
Description
Powers the sensor
!
!
Input pin
!
!
23
Output pin
This pin is connected to the ground of the system
Determine if the given statements for the sensor below are true or false
4 00
2 00
O
! It is a HIR sensor
O
" The detection distance range can be adjusted
X
" It can select the object to be detected
O
! The next detection time can be set up to value between 1 second and 5 minutes
Download