[英]Need help creating a boundless array for Arduino (c/c++)
我正在嘗試制作兒童游戲西蒙·賽斯(Simon Says)的手工副本,在游戲中您可以按以前出現的相同順序按下顏色,並使用操縱桿在學校進行獨立學習。 我認為到目前為止,我已經或將要工作,但是我制作的數組給我“錯誤:'setOrder'的存儲大小未知”,這是有道理的,因為我已將其聲明為“ int setOrder” [];”,但這正是我想要的。 一個數組,它在創建時沒有變量,但是可以隨着游戲的進行而添加變量。這是到目前為止我在代碼中所擁有的,請告訴我它的外觀以及如何制作這樣的數組。 謝謝!!
int xAxis = A0;
int yAxis = A1;
int push = 6;
int blue = 9;
int yellow = 10;
int green = 11;
int red = 12;
int play = 0;
int setOrder[]; /* HERE IS THE PROBLEM */
void setup()
{
Serial.begin(9600);
pinMode(blue, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
pinMode(push, INPUT);
digitalWrite(push, HIGH);
}
void loop()
{
if(!digitalRead(push)){
play = 1;
Serial.println("start!");
}
if(play = 1){
for(int i = 0; i<=sizeof(setOrder); i++){
setOrder[i] = random(1, 4);
for(int j = 0; j<=sizeof(setOrder); j++){
int k = setOrder[j];
if(k=1){
digitalWrite(blue, HIGH); delay(750); digitalWrite(blue, LOW);
}
if(k=2){
digitalWrite(yellow, HIGH); delay(750); digitalWrite(yellow, LOW);
}
if(k=3){
digitalWrite(green, HIGH); delay(750); digitalWrite(green, LOW);
}
if(k=4){
digitalWrite(red, HIGH); delay(750); digitalWrite(red, LOW);
}
}
int playback[sizeof(setOrder)];
for(int l = 0; l<=sizeof(playback); l++){
//player presses RIGHT green led
if(analogRead(xAxis) > 600){
playback[l] = 4;
if(playback[l] == setOrder[l]){
digitalWrite(green, HIGH);
}
else{
digitalWrite(green, HIGH); digitalWrite(blue, HIGH); digitalWrite(yellow, HIGH); digitalWrite(red, HIGH);
play = 0;
break;
}
}
//player presses LEFT yellow led
if(analogRead(xAxis) < 400){
playback[l] = 2;
if(playback[l] == setOrder[l]){
digitalWrite(yellow, HIGH);
}
else{
digitalWrite(green, HIGH); digitalWrite(blue, HIGH); digitalWrite(yellow, HIGH); digitalWrite(red, HIGH);
play = 0;
break;
}
}
//player presses DOWN blue led
if(analogRead(yAxis) > 600){
playback[l] = 1;
if(playback[l] == setOrder[l]){
digitalWrite(blue, HIGH);
}
else{
digitalWrite(green, HIGH); digitalWrite(blue, HIGH); digitalWrite(yellow, HIGH); digitalWrite(red, HIGH);
play = 0;
break;
}
}
//player presses UP red led
if(analogRead(yAxis) < 400){
playback[l] = 3;
if(playback[l] == setOrder[l]){
digitalWrite(red, HIGH);
}
else{
digitalWrite(green, HIGH); digitalWrite(blue, HIGH); digitalWrite(yellow, HIGH); digitalWrite(red, HIGH);
play = 0;
break;
}
}
}
}
}
}
另外,我不確定是否正在使用“ break();”。 正確的方法,如果有人可以告訴我那太好了! 再次感謝。
我建議您研究一下std :: vector。
在檢查if (play = 1)
您的主要播放區域后的第一行,您使用sizeof(setOrder)
運行for循環,而無需setOrder
數據放入setOrder
。 即使可以編譯,從邏輯上講,您的代碼也始終會跳過循環。 一旦確定了向量(我很確定這是您要尋找的),您可能需要更改此方法,以將setOrder
向量的大小與預定義的最大值進行比較(這時玩家會贏嗎?)。
這是我從google找到的向量教程 。
另外,還有一些其他可能的錯誤/建議:
if (play = 1)
,實際上是在使用賦值=
運算符,而不是條件檢查==
。 您將分配值1
進行play
,而不是檢查play
是否等於1
。 sizeof(setOrder)
錯誤。 sizeof(X)
返回的字節數X
占用,不一定在元件的數量X
。 如果使用向量,則有一個vector.size()函數可返回向量中的元素數 analogRead(xAxis)
-每個方向一次。 玩家的第一個輸入將始終在第一個通話中被檢查。 如果為假,則播放器將需要再輸入一次以使其輸入第二個if語句。 對於方向檢測,您可能會看到以下內容:
enum Directions {
Center,
Left,
Right,
Up,
Down
}
const int threshold = 100;
int getJoyStick()
{
// get the X,Y coordinates with 0,0 the centre
int x = analogRead(xAxis) - 500;
int y = analogRead(yAxis) - 500;
/check if we are in the middle. Both X and Y need to be small for this to happen
if (abs(x) < threshold && abs(y) < threshold) return Center;
//to detect up, down, left, right draw diagonals at 45 degrees from the centre
//if we are more to the right than we are up or down, then it is Right. This
//is to cope if it isn't exactly horizonal or vertical
//so y big might mean up, but if x is bigger, than it is actually to the right
if (x>y) {
if (x>-y) return Right; else return Down;
}
else {
if (x>-y) return Up; else return Left;
}
}
枚舉只是顯示完成此操作的好方法。 您將在所有代碼中使用相同的代碼。 但是,如果對您來說更簡單,則可以為每個方向返回數字代碼。
如果您使用的是c ++語言,如您帖子中的c ++標記所示,請使用std :: vector而不是array。
向量是可以在程序執行期間增長的列表。 這是有關向量的文檔。
如果要使用普通的舊C數組,最簡單的路徑將是靜態長度的數組,該長度足以接受最大數量的條目。 然后,您將保留一個單獨的計數器,說明當前正在使用多少陣列。
const int MAX_ORDER_LENGTH = 1024;
int setOrder[MAX_ORDER_LENGTH];
int setOrderSize = 0; //the number of entries currently used
然后,當您添加條目時,請確保setOrderSize保持小於MAX_ORDER_LENGTH。
請注意,sizeof(setOrder)不會給出數組中的條目數。 實際上是結構占用的字節數。
休息 命令將使您退出最里面的for循環,但不會退出最外面的for循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.