


Preview text:
#include #include #include #include
const int motor = 6; // khai báo chân PWM điều khiển motor
const int button = 8; // khai báo chân đọc nút bấm
boolean oldState = 0; // trạng thái của nút bấm
int time; // biến lưu thời gian
int resetPin = 4; // The pin number of the reset pin.
int clockPin = 5; // The pin number of the clock pin.
int dataPin = 6; // The pin number of the data pin.
int busyPin = 7; // The pin number of the busy pin.
WTV020SD16P wtv020sd16p(resetPin,clockPin,dataPin,busyPin); boolean state = 0;
int speedMotor = 0; // biến lưu giữ tốc độ của motor #define sensor 2 void setup() {
Serial.begin(9600); // giao tiếp Serial với 9600 baudrate. pinMode(motor, OUTPUT); pinMode(button, INPUT); pinMode(sensor, INPUT); Serial.begin(9600); //Initializes the module. wtv020sd16p.reset(); } void loop() { #include
if (digitalRead(button) != oldState) { // kiểm tra thay đổi trạng thái nút bấm
time = millis(); // reset lại bộ timer khi nút bấm thay đổi trạng thái }
state = digitalRead(button); // đọc giá trị nút bấm
int duration = millis() - time; // đo thời gian duy trì trạng thái hiện tại
duration = constrain(duration, 0, 5000); // 0 <= duration <= 5000 ms.
if (state == 1) { //nếu đang nhấn nút
speedMotor = map(duration, 0, 5000, 0, 255);
analogWrite(motor, speedMotor); // xuất xung PWM để điều khiển tốc độ motor
// duty cycle càng lớn thì motor quay càng nhanh delay(500); }
Serial.println(speedMotor); // in ra Serial monitor giá trị tốc độ
oldState = state; // cập nhật trạng thái nút bấm.
Serial.print("Gia tri cam bien:");
Serial.println(digitalRead(sensor)); //Nếu sensor = 1 thì không phát hiện vật cản, nếu sensor = 0 thì phát hiện vật cản. delay(1000);
//Plays synchronously an audio file. Busy pin is used for this method. wtv020sd16p.playVoice(0);
//Plays asynchronously an audio file.
wtv020sd16p.asyncPlayVoice(1);
//Plays audio file number 1 during 5 seconds. delay(5000);
//Pauses audio file number 1 during 5 seconds. // wtv020sd16p.pauseVoice(); delay(5000);
//Resumes audio file number 1 during 5 seconds. wtv020sd16p.pauseVoice(); delay(5000);
//Stops current audio file playing. wtv020sd16p.stopVoice();
//Plays synchronously an audio file. Busy pin is used for this method.
wtv020sd16p.asyncPlayVoice(2); delay(2000);
//Mutes audio file number 2 during 2 seconds. wtv020sd16p.mute(); delay(2000);
//Unmutes audio file number 2 during 2 seconds. wtv020sd16p.unmute(); delay(2000);
//Stops current audio file playing. wtv020sd16p.stopVoice(); }