W5-无代写
时间:2023-03-20
Lab W5: Further Arduino programming
using Wokwi
Update note 3/3/2023:
- Clarified the instruction in the first task.
- Numbered each task.
- Fixed typos.
- Clarified the explanation of the circuit behaviour in the second task.
In this lab session, you will continue working on Arduino programming using Wokwi. You should
learn about more kinds of sensors and actuators and more advanced Arduino programming. Have you
tried “Try it yourself” suggestions from last week? If not, we recommend you try it before
proceeding.
1. Revisiting last week’s exercises – button and LED
Try it yourself
We suggested you try putting a button to turn the LED on and off on the Arduino board. What was the
default state of the button (i.e., when it is not pressed)? Was it HIGH or LOW? Why was it? Can you
change the schematic diagram and change its default state to the opposite? E.g., if its default state was
HIGH, can you change it to LOW? Note that this is about the schematic and not related to your code.
Hint: The below configuration of the resistor and the button is called a ‘pull-up’ resistor. Search for
the ‘pull-down’ resistor to see how it looks and how it works differently from the ‘pull-up’ resistor.
2. Revisiting last week’s exercises – seven segments and potentiometer
Try it yourself
Another task was using a potentiometer to control the number displayed on seven segments. One of
the students in the module, Dilkash Sahota kindly shared their code with us. If you like, you can use
their code below and make wirings between an Arduino, potentiometer, and seven segments for that
code on Wokwi. You can also tweak the code as you like. Hint: When you click an electronic part on
Wokwi, e.g., seven segments, there will be a question mark appearing. When you click it, you will be
led to a documentation page for the part. You can use it to learn how to wire the part.
const int pot1 = A1;
const int A = 13;
const int B = 12;
const int C = 11;
const int D = 10;
const int E = 9;
const int F = 8;
const int G = 7;
int readPot(int pin) {
return map(analogRead(pin), 0, 1023, 0, 9);
}
void setup() {
pinMode(pot1, INPUT);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
}
void loop() {
if(readPot(pot1) == 0){
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
}
if(readPot(pot1) == 1){
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
}
if(readPot(pot1) == 2){
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
}
if(readPot(pot1) == 3){
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, LOW);
}
if(readPot(pot1) == 4){
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
}
if(readPot(pot1) == 5){
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, LOW);
}
if(readPot(pot1) == 6){
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
}
if(readPot(pot1) == 7){
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
}
if(readPot(pot1) == 8){
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
}
if(readPot(pot1) == 9){
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, LOW);
}
}
3. Photoresistor and LED
In this exercise, a photoresistor is used instead of a potentiometer in one of the previous tasks. A
photoresistor changes its resistance depending on the light hitting on it. Select the ‘Photoresistor
(LDR) Sensor’ [note: LDR = light dependent resistor)] from the list of options.
const int analogInPin = A0; // Analog input pin that the potentiometer is
attached to
const int analogOutPin = 5; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(analogOutPin, OUTPUT);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the Serial Monitor:
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(2);
}
When you hit the green play button, you can click on the photoresistor sensor (highlighted with a dotted
line below) and this will call up a slider to let you adjust its values. When you are using the slider, it
changes the amount of light the photoresistor gets. It is irrelevant to the brightness of the LED.
4. Photoresistor and servo motor
In this example, changing the values of the photoresistor is used to drive a servo motor. You could
imagine the servo-motor attached to an object. For example, this could be the electronics for a cat toy:
as the cat covers the photoresistor, the servo-motor moves a toy mouse.
Set up
Use the Plus button to open the list of attachments. Select the ‘Photoresistor (LDR) Sensor’ [note:
LDR = light dependent resistor)] and the ‘Servo’ from the list.
Connect these to the Uno as shown in this figure.
The Code
In this examples, we call a library which is used to manage the operations of the servo-
motor.
#include
Servo myservo; // create servo object to control a servo
const int analogInPin = A0; // Analog input pin that the potentiometer is
attached to
const int analogOutPin = 5; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
int pos = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(analogOutPin, OUTPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 255, 0);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the Serial Monitor:
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// move the servo motor. The speed is inversely proportional to the light
coming in
if (outputValue < 200){
myservo.write(0);
delay(random(outputValue*5,outputValue*20));
myservo.write(180);
delay(random(outputValue*5,outputValue*20));
}
// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(2);
}
When you hit the green ‘play’ button, click on the photoresitor (the round, grey part of the sensor) to
call up a slider which simulates the amount of light (measured in lux) received by the sensor. The
measured values appear at the bottom of the screen.
Try it yourself
You used Serial.print() to send text from the Arduino to your “PC.” Can you change the code to send
text from the “PC” to the Arduino? Hint: Serial.read(). Reference:
https://www.arduino.cc/reference/en/language/functions/communication/serial/readstring/
5. Ultrasonic Distance Sensor
The ultrasound distance sensor can recognize the distance between the sensor and an object in fron of
it. Try to make wire the sensor as below and read the distance value.
The Code
const int echoPin = 2; // attach pin D2 to pin Echo of HC-SR04
const int trigPin = 3; //attach pin D3 to pin Trig of HC-SR04
long duration; // duration of sound wave travel
int distance; // distance measurement
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound
wave travel time in microseconds
distance = duration * 0.034 / 2; // Calculating the distance. Speed of
sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
6. Ultrasound Distance Sensor and LCD
Now display the distance value on the LCD.
The Code
#include
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const int echoPin = 2; // attach pin D2 Arduino to pin Echo of HC-SR04
const int trigPin = 3; //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
lcd.begin(16, 2);
// you can now interact with the LCD, e.g.:
lcd.print("Hello world!");
lcd.setCursor(0,1);
lcd.print("Display demo.");
delay(1000);
lcd.begin(16, 2); // Reset the LCD
lcd.print("Distance:");
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go
and back)
// Displays the distance on the LCD
lcd.setCursor(0,1); // update only the second line
lcd.print(distance); // display the distance
lcd.print(" cm ");
}