Arrays 如何使此代码更高效?

Arrays 如何使此代码更高效?,arrays,arduino,coding-efficiency,Arrays,Arduino,Coding Efficiency,我有两个问题与下面的代码有关。首先,我知道这段代码很可怕,可以减少和改进。我想把整个字母表和数字都加起来,但把每一个都打出来是愚蠢的 第一个问题:如何缩短此代码?我对数组不是很熟悉,但我猜这就是我要使用的 第二个问题:如何使用数组来表示每个字母/数字 谢谢大家 /* Blinks "BRENTON" in Morse Code, in pin # 12. */ void setup() { // initialize the digital pin as an output. //

我有两个问题与下面的代码有关。首先,我知道这段代码很可怕,可以减少和改进。我想把整个字母表和数字都加起来,但把每一个都打出来是愚蠢的

第一个问题:如何缩短此代码?我对数组不是很熟悉,但我猜这就是我要使用的

第二个问题:如何使用数组来表示每个字母/数字

谢谢大家

/*
Blinks "BRENTON" in Morse Code, in pin # 12. 
 */

void setup() {
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

int dot = 250;
int dash = dot * 3;
int space = dot * 7;
int rest = 250;

void A() {
  digitalWrite(12, HIGH);
  delay(dot);
  digitalWrite(12, LOW);
  delay(rest);
  digitalWrite(12, HIGH);
  delay(dash);
  digitalWrite(12, LOW);
  delay(space);
}
void B() {
  digitalWrite(12, HIGH);
  delay(dash);
  digitalWrite(12, LOW);
  delay(rest);
  digitalWrite(12, HIGH);
  delay(dot);
  digitalWrite(12, LOW);
  delay(rest);
  digitalWrite(12, HIGH);
  delay(dot);
  digitalWrite(12, LOW);
  delay(rest);
  digitalWrite(12, HIGH);
  delay(dot);
  digitalWrite(12, LOW);
  delay(space);
}
void R() {
  digitalWrite(12, HIGH);
  delay(dot);
  digitalWrite(12, LOW);
  delay(rest);
  digitalWrite(12, HIGH);
  delay(dash);
  digitalWrite(12, LOW);
  delay(rest);
  digitalWrite(12, HIGH);
  delay(dot);
  digitalWrite(12, LOW);
  delay(space);
}
void E() {
  digitalWrite(12, HIGH);
  delay(dot);
  digitalWrite(12, LOW);
  delay(space);
}
void N() {
  digitalWrite(12, HIGH);
  delay(dash);
  digitalWrite(12, LOW);
  delay(rest);
  digitalWrite(12, HIGH);
  delay(dot);
  digitalWrite(12, LOW);
  delay(space);
} 
void T() {
  digitalWrite(12, HIGH);
  delay(dash);
  digitalWrite(12, LOW);
  delay(space);
}
void O() {
  digitalWrite(12, HIGH);
  delay(dash);
  digitalWrite(12, LOW);
  delay(rest);
  digitalWrite(12, HIGH);
  delay(dash);
  digitalWrite(12, LOW);
  delay(rest);
  digitalWrite(12, HIGH);
  delay(dash);
  digitalWrite(12, LOW);
  delay(space);
}
void pin13(){
  digitalWrite(13, HIGH);
  delay(10000);
  digitalWrite(13, LOW);
  delay(1000);
}
void loopOne() {
  int i = 0;
  while(i < 100) {
    pin13();
    i++;
  }
}

void loop() {
  B();
  R();
  E();
  N();
  T();
  O();
  N();
}
/*
在莫尔斯电码中闪烁“布伦顿”,第12针。
*/
无效设置(){
//将数字管脚初始化为输出。
//引脚13在大多数Arduino板上都有一个LED连接:
pinMode(12,输出);
pinMode(13,输出);
}
int点=250;
int dash=点*3;
整数空间=点*7;
int rest=250;
作废{
数字写入(12,高);
延迟(dot);
数字写入(12,低);
延迟(休息);
数字写入(12,高);
延迟(破折号);
数字写入(12,低);
延迟(空间);
}
无效B(){
数字写入(12,高);
延迟(破折号);
数字写入(12,低);
延迟(休息);
数字写入(12,高);
延迟(dot);
数字写入(12,低);
延迟(休息);
数字写入(12,高);
延迟(dot);
数字写入(12,低);
延迟(休息);
数字写入(12,高);
延迟(dot);
数字写入(12,低);
延迟(空间);
}
void R(){
数字写入(12,高);
延迟(dot);
数字写入(12,低);
延迟(休息);
数字写入(12,高);
延迟(破折号);
数字写入(12,低);
延迟(休息);
数字写入(12,高);
延迟(dot);
数字写入(12,低);
延迟(空间);
}
void E(){
数字写入(12,高);
延迟(dot);
数字写入(12,低);
延迟(空间);
}
void N(){
数字写入(12,高);
延迟(破折号);
数字写入(12,低);
延迟(休息);
数字写入(12,高);
延迟(dot);
数字写入(12,低);
延迟(空间);
} 
void T(){
数字写入(12,高);
延迟(破折号);
数字写入(12,低);
延迟(空间);
}
void O(){
数字写入(12,高);
延迟(破折号);
数字写入(12,低);
延迟(休息);
数字写入(12,高);
延迟(破折号);
数字写入(12,低);
延迟(休息);
数字写入(12,高);
延迟(破折号);
数字写入(12,低);
延迟(空间);
}
void pin13(){
数字写入(13,高);
延迟(10 000);
数字写入(13,低);
延迟(1000);
}
void loopOne(){
int i=0;
而(i<100){
pin13();
i++;
}
}
void循环(){
B();
R();
E();
N();
T();
O();
N();
}

几年前,我为一位对地理缓存感兴趣的朋友写了一个莫尔斯电码发生器。这个代码控制着一个发射机。虽然它是用C编写的,但它可能会让您了解如何做到这一点。您实际上只需要三个例程,一个用于“dit”、“dah”和“space”

play_space
实际上只是关闭声音和短暂延迟

play\u-mark
实际上只不过是打开持续时间的声音,可以在Arduino中编码为
音调(pin、频率、持续时间\u毫秒)

init
中的所有内容都只是将控制器设置为以设定的频率播放音调。同样,使用
tone()
在Arduino上更轻松。主要是在
MCode
结构和数组中表示每个字符和数字。然后,对于要发送的字符串中的每个字符,只需向下搜索数组,直到找到匹配的字符,然后使用
xmit\u symbol()
从结构中播放
code

#定义F#u CPU 1000000
#包括
#包括
//传输长度,以毫秒为单位
#定义DIT_MS 60//DIT长度为120时每分钟应产生约10个字
#定义DAH_MS(DIT_MS*3)//DAH长度
#在字母空格后定义AL_空格(DIT_MS*3)//值
#定义AW_空格(DIT_MS*6)//在单词空格=6+字母空格=7之后
//简单诊断LED
#定义LED_1 PA0
//硬编码呼号
const char*CALLSIGN=“HELLO WORLD”;
静态常量结构
{
常量字符符号;
常数8_t长度;
const uint8_t代码;
}MCode[]=
{
{'A',20B01000000},
{'B',420B10010000},
{'C',4100000},
{'D',30b10000000},
{'E',100B00000000},
{'F',420B00100000},
{'G',30B11000000},
{'H',40b0000000},
{'I',20b0000000},
{'J',4,0b01110000},
{'K',30B10100000},
{'L',40B01000000},
{M',20B11000000},
{'N',20b10000000},
{'O',30b11100000},
{'P',420B01100000},
{'Q',420B11010000},
{'R',30B01000000},
{'S',30b0000000},
{T',100B10000000},
{'U',30B00100000},
{'V',420B00010000},
{'W',30B01100000},
{'X',420B10010000},
{'Y',401000},
{'Z',410000},
{'1',5,0b01111000},
{'2',5,0b00111000},
{'3',5,0b00011000},
{'4',50B00001000},
{'5',50B00000000},
{'6',50B10000000},
{'7',50B11000000},
{'8',510B11100000},
{'9',5,0b11110000},
{'0',5,0b11111000},
{'-',6,0B100000100}
};
void init(void)
{
//设置诊断指示灯

DDRA |=(1你应该问一下,你能用谷歌搜索一下“arduino morse”吗?…它没有那么难,其他的点击会给你足够的灵感。我投票结束这个问题,因为要求改进工作代码的问题属于代码审查,而不是堆栈溢出。
#define F_CPU 1000000
#include <avr/io.h>
#include <util/delay.h>

// transmission lengths, in milliseconds
#define DIT_MS 60                       // dit length of 120 should yield ~10 words per minute
#define DAH_MS (DIT_MS * 3)     // dah length
#define AL_SPACE (DIT_MS * 3)   // after-letter space
#define AW_SPACE (DIT_MS * 6)   // after-word space = 6 + after-letter space = 7

// simple diagnostic LED 
#define LED_1   PA0

// hardcoded callsign
const char* CALLSIGN = "HELLO WORLD";

static const struct
{
    const char symbol;
    const uint8_t length;
    const uint8_t code;

} MCode[] = 
{
    {'A', 2, 0b01000000},
    {'B', 4, 0b10010000},
    {'C', 4, 0b10100000},
    {'D', 3, 0b10000000},
    {'E', 1, 0b00000000},
    {'F', 4, 0b00100000},
    {'G', 3, 0b11000000},
    {'H', 4, 0b00000000},
    {'I', 2, 0b00000000},
    {'J', 4, 0b01110000},
    {'K', 3, 0b10100000},
    {'L', 4, 0b01000000},
    {'M', 2, 0b11000000},
    {'N', 2, 0b10000000},
    {'O', 3, 0b11100000},
    {'P', 4, 0b01100000},
    {'Q', 4, 0b11010000},
    {'R', 3, 0b01000000},
    {'S', 3, 0b00000000},
    {'T', 1, 0b10000000},
    {'U', 3, 0b00100000},
    {'V', 4, 0b00010000},
    {'W', 3, 0b01100000},
    {'X', 4, 0b10010000},
    {'Y', 4, 0b10110000},
    {'Z', 4, 0b11000000},
    {'1', 5, 0b01111000},
    {'2', 5, 0b00111000},
    {'3', 5, 0b00011000},
    {'4', 5, 0b00001000},
    {'5', 5, 0b00000000},
    {'6', 5, 0b10000000},
    {'7', 5, 0b11000000},
    {'8', 5, 0b11100000},
    {'9', 5, 0b11110000},
    {'0', 5, 0b11111000},
    {'-', 6, 0b10000100}
};


void init(void)
{
    // set up diagnostic led
    DDRA |= (1<<LED_1);
    // set up OC1A pin as output for PWM signal
    DDRB |= (1<<PB1);
    // set up fast PWM mode
    TCCR1A |= (1<<PWM1A);
    // timer1 prescaler
    TCCR1B |= (1<<CS12);    //  1/8 (yields ~490Hz at 1MHz FCPU) (p.117)
    // set OCR value to achieve close enough to square wave
    OCR1A = 128;
}

/////////////////////////////////////////////////////////////////////////////////
////  ROUTINES TO CREATE MARKS AND SPACES
/////////////////////////////////////////////////////////////////////////////////
void play_space(uint16_t length)
{
    // play a space of the specified length
    PORTA &= ~(1<<LED_1);
    TCCR1A &= ~(1<<COM1A1);
    _delay_ms(length);
}

void play_mark(uint16_t length)
{
    // play a mark for specified length
    PORTA |= (1<<LED_1);
    TCCR1A |= (1<<COM1A1);
    _delay_ms(length);
    // always play a dit-length space afterwards
    PORTA &= ~(1<<LED_1);
    TCCR1A &= ~(1<<COM1A1);
    _delay_ms(DIT_MS);
}
/////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////
////  TRANSMIT A CHARACTER AND INTER-CHARACTER SPACE
/////////////////////////////////////////////////////////////////////////////////
void xmit_symbol(uint8_t length, uint8_t code)
{
    for (uint8_t p = 0; p < length; p++)
    {
        uint8_t shift = 7 - p;
        uint8_t mask = 1<<shift;
        uint8_t result = code & mask;
        if (result == mask)
            play_mark(DAH_MS);
        else
            play_mark(DIT_MS);
    }
    play_space(AL_SPACE);
}
/////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////
////  TRANSMIT A NULL-TERMINATED STRING
/////////////////////////////////////////////////////////////////////////////////
void xmit_callsign()
{
    uint8_t iStringPos;
    uint8_t iSearchPos;

    // Process each character of the callsign string.
    // NOTE: Trick here uses null terminator on string to make comparison false.
    for (iStringPos = 0; CALLSIGN[iStringPos]; iStringPos++)
    {
        // Linear search through array of structs seeking matching symbol.
        for (iSearchPos = 0; iSearchPos < (sizeof MCode / sizeof *MCode); iSearchPos++ )
        {
            if (CALLSIGN[iStringPos] == MCode[iSearchPos].symbol)
            {
                // We found a match, so transmit this character/symbol.
                xmit_symbol(MCode[iSearchPos].length, MCode[iSearchPos].code);

                // Bail out and move on down the string until done...
                break;
            }
        }
    }
}
/////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////
////  MAIN LOOP
/////////////////////////////////////////////////////////////////////////////////
int main(void)
{
    // Initialize the controller.
    init();

    // Endless main loop.
    while(1)
    {
        // 3-second delay in before starting, for ease in copying...
        for (int n = 0; n < 3; n++) _delay_ms(1000);

        // call central transmit routine
        xmit_callsign();

        // insert a hard delay in between transmissions, for now
        for (int n = 0; n < 3; n++) _delay_ms(1000);
    }
    // required but unreachable
    return 0;
}