Untitled

 avatar
unknown
c_cpp
2 years ago
4.7 kB
6
Indexable
#define speakerPin 3
char readchars[255];
unsigned long duration = 100;
int speakerFre = 500;
String comdata = "";
void setup()
{
	pinMode(speakerPin, OUTPUT);
	pinMode(9, OUTPUT);
	Serial.begin(9600);
	uint32_t fTransmit = 1000;
	// TCCR1A = 0b01000000;
	// TCCR1B = 0b00001001;
	// OCR1A = F_CPU / (2000 * fTransmit) - 1;
	char strbuf[255];
	// sprintf(strbuf, " 设置载波频率:%d KHz", (F_CPU / (2 * (1 + OCR1A)) / 1000));
	Serial.println(strbuf);
}
void loop()
{
	comdata = "";
	if (Serial.available() > 0)
	{
		comdata = Serial.readString();
		Serial.println(comdata);
	}
	if (comdata.length() > 0)
	{
		int str_length = comdata.length();
		comdata.toUpperCase();
		for (int read_pos = 0; str_length >= read_pos + 1; read_pos++)
		{
			char read_now = comdata[read_pos];
			switch (read_now)
			{
			case 'A':
				dot();
				dash();
				delay(2 * duration);
				break;
			case 'B':
				dash();
				dot();
				dot();
				dot();
				delay(2 * duration);
				break;
			case 'C':
				dash();
				dot();
				dash();
				dot();
				delay(2 * duration);
				break;
			case 'D':
				dash();
				dot();
				dot();
				delay(2 * duration);
				break;
			case 'E':
				dot();
				delay(2 * duration);
				break;
			case 'F':
				dot();
				dot();
				dash();
				dot();
				delay(2 * duration);
				break;
			case 'G':
				dash();
				dash();
				dot();
				delay(2 * duration);
				break;
			case 'H':
				dot();
				dot();
				dot();
				dot();
				delay(2 * duration);
				break;
			case 'I':
				dot();
				dot();
				delay(2 * duration);
				break;
			case 'J':
				dot();
				dash();
				dash();
				dash();
				delay(2 * duration);
				break;
			case 'K':
				dash();
				dot();
				dash();
				delay(2 * duration);
				break;
			case 'L':
				dot();
				dash();
				dot();
				dot();
				delay(2 * duration);
				break;
			case 'M':
				dash();
				dash();
				delay(2 * duration);
				break;
			case 'N':
				dash();
				dot();
				delay(2 * duration);
				break;
			case 'O':
				dash();
				dash();
				dash();
				delay(2 * duration);
				break;
			case 'P':
				dot();
				dash();
				dash();
				dot();
				delay(2 * duration);
				break;
			case 'Q':
				dash();
				dash();
				dot();
				dash();
				delay(2 * duration);
				break;
			case 'R':
				dot();
				dash();
				dot();
				delay(2 * duration);
				break;
			case 'S':
				dot();
				dot();
				dot();
				delay(2 * duration);
				break;
			case 'T':
				dash();
				delay(2 * duration);
				break;
			case 'U':
				dot();
				dot();
				dash();
				delay(2 * duration);
				break;
			case 'V':
				dot();
				dot();
				dot();
				dash();
				delay(2 * duration);
				break;
			case 'W':
				dot();
				dash();
				dash();
				delay(2 * duration);
				break;
			case 'X':
				dash();
				dot();
				dot();
				dash();
				delay(2 * duration);
				break;
			case 'Y':
				dash();
				dot();
				dash();
				dash();
				delay(2 * duration);
				break;
			case 'Z':
				dash();
				dash();
				dot();
				dot();
				delay(2 * duration);
				break;
			case '1':
				dot();
				dash();
				dash();
				dash();
				dash();
				delay(2 * duration);
				break;
			case '2':
				dot();
				dot();
				dash();
				dash();
				dash();
				delay(2 * duration);
				break;
			case '3':
				dot();
				dot();
				dot();
				dash();
				dash();
				delay(2 * duration);
				break;
			case '4':
				dot();
				dot();
				dot();
				dot();
				dash();
				delay(2 * duration);
				break;
			case '5':
				dot();
				dot();
				dot();
				dot();
				dot();
				delay(2 * duration);
				break;
			case '6':
				dash();
				dot();
				dot();
				dot();
				dot();
				delay(2 * duration);
				break;
			case '7':
				dash();
				dash();
				dot();
				dot();
				dot();
				delay(2 * duration);
				break;
			case '8':
				dash();
				dash();
				dash();
				dot();
				dot();
				delay(2 * duration);
				break;
			case '9':
				dash();
				dash();
				dash();
				dash();
				dot();
				delay(2 * duration);
				break;
			case '0':
				dash();
				dash();
				dash();
				dash();
				dash();
				delay(2 * duration);
				break;
			case ' ':
				delay(4 * duration);
				break;
			default:
				break;
			}
		}
	}
} // di:T,da:3T,di-da:T,char-char:3T,word-word:7T
void dot()
{
	tone(speakerPin, speakerFre, duration);
	delay(2 * duration);
}
void dash()
{
	tone(speakerPin, speakerFre, 3 * duration);
	delay(4 * duration);
}
Editor is loading...