作业3。
This commit is contained in:
51
Homework/Homework2/Homework2.ino
Normal file
51
Homework/Homework2/Homework2.ino
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <Servo.h>
|
||||
|
||||
Servo servo;
|
||||
int trigger_pin = 11;
|
||||
int echo_pin = 12;
|
||||
int servo_pin = 7;
|
||||
int r_led_pin = 4;
|
||||
int g_led_pin = 5;
|
||||
double distance = 0.0;
|
||||
int current_led_status = 0;
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(9600);
|
||||
pinMode(trigger_pin, OUTPUT);
|
||||
pinMode(echo_pin, INPUT);
|
||||
pinMode(r_led_pin, OUTPUT);
|
||||
pinMode(g_led_pin, OUTPUT);
|
||||
servo.attach(7);
|
||||
digitalWrite(r_led_pin, HIGH);
|
||||
digitalWrite(g_led_pin, HIGH);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
digitalWrite(trigger_pin, HIGH);
|
||||
delayMicroseconds(10);
|
||||
digitalWrite(trigger_pin, LOW);
|
||||
|
||||
distance = pulseIn(echo_pin, HIGH) / 58.00;
|
||||
|
||||
Serial.print("Distance: ");
|
||||
Serial.print(distance);
|
||||
Serial.print("\n");
|
||||
|
||||
if (distance < 10) {
|
||||
servo.write(90);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
digitalWrite(r_led_pin, LOW);
|
||||
delay(50);
|
||||
digitalWrite(r_led_pin, HIGH);
|
||||
delay(50);
|
||||
}
|
||||
}
|
||||
else {
|
||||
servo.write(0);
|
||||
digitalWrite(g_led_pin, LOW);
|
||||
delay(500);
|
||||
digitalWrite(g_led_pin, HIGH);
|
||||
}
|
||||
}
|
||||
150
Homework/Homework3/Homework3.ino
Normal file
150
Homework/Homework3/Homework3.ino
Normal file
@@ -0,0 +1,150 @@
|
||||
#include <LiquidCrystal.h>
|
||||
#define RESET 0x100
|
||||
#define READY 0x1
|
||||
#define BEGIN 0x2
|
||||
#define PAUSE 0x3
|
||||
#define STOP 0x4
|
||||
#define BEEPING 0x5
|
||||
#define STATUS_FIELD 0xf
|
||||
#define STATUS_CHANGED 0x10
|
||||
|
||||
const int rs_pin = 13, en_pin = 12, d4_pin = 7, d5_pin = 6, d6_pin = 5, d7_pin = 4;
|
||||
const int switch_pin = 2;
|
||||
const int x_pin = A0, y_pin = A1;
|
||||
|
||||
int status = 0;
|
||||
int last_tick;
|
||||
int last_start_tick = 0;
|
||||
int timed_length = 0;
|
||||
LiquidCrystal LCD(rs_pin, en_pin, d4_pin, d5_pin, d6_pin, d7_pin);
|
||||
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(9600);
|
||||
pinMode(switch_pin, INPUT_PULLUP);
|
||||
attachInterrupt(0, switch_pressed, FALLING);
|
||||
LCD.begin(16, 2);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
if (status & RESET) {
|
||||
// Serial.println("Reset");
|
||||
timed_length = 0;
|
||||
LCD.clear();
|
||||
LCD.setCursor(0, 0);
|
||||
LCD.print("WAIT...");
|
||||
status = READY | STATUS_CHANGED;
|
||||
delay(1000);
|
||||
} else {
|
||||
switch (status & STATUS_FIELD) {
|
||||
case READY:
|
||||
{
|
||||
// Serial.println("Ready");
|
||||
if (status & STATUS_CHANGED) {
|
||||
LCD.clear();
|
||||
LCD.print("READY");
|
||||
status = READY;
|
||||
}
|
||||
if (analogRead(y_pin) > 800) {
|
||||
last_tick = millis();
|
||||
status = BEGIN | STATUS_CHANGED;
|
||||
last_start_tick = last_tick;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BEGIN:
|
||||
{
|
||||
// Serial.println("Begin");
|
||||
if (status & STATUS_CHANGED) {
|
||||
LCD.clear();
|
||||
LCD.print("BEGIN");
|
||||
// delay(500);
|
||||
status = BEGIN;
|
||||
}
|
||||
|
||||
delay(10);
|
||||
int cur_tick = millis();
|
||||
timed_length += cur_tick - last_tick;
|
||||
last_tick = cur_tick;
|
||||
|
||||
if (timed_length > 10000) {
|
||||
status = BEEPING | STATUS_CHANGED;
|
||||
break;
|
||||
}
|
||||
|
||||
Serial.println(timed_length / 10 * 10);
|
||||
LCD.setCursor(0, 1);
|
||||
LCD.print(timed_length);
|
||||
|
||||
if (analogRead(y_pin) > 800 && cur_tick - last_start_tick > 200) {
|
||||
status = STOP | STATUS_CHANGED;
|
||||
break;
|
||||
}
|
||||
|
||||
if (analogRead(x_pin) > 800) {
|
||||
status = PAUSE | STATUS_CHANGED;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PAUSE:
|
||||
{
|
||||
// Serial.println("Pause");
|
||||
if (status & STATUS_CHANGED) {
|
||||
LCD.clear();
|
||||
LCD.print("PAUSED");
|
||||
LCD.setCursor(0, 1);
|
||||
LCD.print(timed_length);
|
||||
delay(500);
|
||||
status = PAUSE;
|
||||
}
|
||||
|
||||
if (analogRead(x_pin) < 200) {
|
||||
status = BEGIN | STATUS_CHANGED;
|
||||
last_tick = millis();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STOP:
|
||||
{
|
||||
// Serial.println("Stop");
|
||||
if (status & STATUS_CHANGED) {
|
||||
LCD.clear();
|
||||
LCD.print("STOPPED");
|
||||
status = STOP;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BEEPING:
|
||||
{
|
||||
// Serial.println("Beep");
|
||||
if (status & STATUS_CHANGED) {
|
||||
LCD.clear();
|
||||
LCD.print("BEEP");
|
||||
status = BEEPING;
|
||||
}
|
||||
|
||||
int y_cmd = analogRead(x_pin);
|
||||
if (y_cmd > 800) {
|
||||
timed_length = 0;
|
||||
status = READY | STATUS_CHANGED;
|
||||
} else if (y_cmd < 200) {
|
||||
timed_length = 0;
|
||||
last_tick = millis();
|
||||
status = BEGIN | STATUS_CHANGED;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void switch_pressed() {
|
||||
status = RESET;
|
||||
Serial.println("Pressed");
|
||||
}
|
||||
Reference in New Issue
Block a user