作业3。

This commit is contained in:
unlockable
2023-07-01 00:23:33 +08:00
parent c43bbc83b3
commit 906336eafd
3 changed files with 152 additions and 1 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
/.DS_Store .DS_Store
.vscode

View 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");
}