簡體   English   中英

如何打印與單詞Arduino相關的變量?

[英]How to print a variable that is tied to a word Arduino?

我需要我的Arduino代碼以打印出替換為變量的隨機單詞。 因此,就像我將擁有一個隨機數生成器一樣,將隨機數吐出到一個單詞上,然后將其作為變量打印出來。 現在是我的代碼,對不起,我還是Arduino的初學者。

long randnumber = 0;

int aye = 1;
int sup = 2;
int boi = 3;
int bruv = 4;

void setup() {
  Serial.begin(9600); // Starts the serial communication

}

void loop() {
int randnumber = 0;
  randnumber = random(0,4);
  Serial.println(randnumber);

}

您需要將單詞放入數組中:

const char *words[] = {"aye", "sup", "boi", "bruv"};

然后選擇一個隨機索引並在該索引處發送單詞:

// Calculate the number of words. Better than hardcoding
// 4. If you add/remove words from array, this code
// won't have to change
int num_words = sizeof(words) / sizeof(words[0]);
randnumber = random(0, num_words);
Serial.println(words[randnumber]);

您還應該為RNG設置種子,否則每次將獲得相同的結果。 在PC上,人們通常使用當前時間來播種RNG,但是Arduino上沒有時鍾,因此更加困難。 這里有一個很好的討論: 在Arduino中獲得一個真正的隨機數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM