阵列有上限吗?-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1946|回复: 4

[未解决] 阵列有上限吗?

[复制链接]
发表于 2021-3-29 13:01 | 显示全部楼层 |阅读模式
我有一些阵列, 以下红色字那些

如果阵列数mouth/eye 上到7以上
烧到UNO 中就会失效
删除了, 只有6个阵列才可正常运行
又不似是记忆体不足
求解, 谢谢



Sketch uses 11106 bytes (34%) of program storage space. Maximum is 32256 bytes.
Global variables use 1292 bytes (63%) of dynamic memory, leaving 756 bytes for local variables. Maximum is 2048 bytes.


#include <Adafruit_NeoPixel.h>
#include <IRremote.h>
int RECV_PIN = 11; //紅外線接收器角位
IRrecv irrecv(RECV_PIN);
decode_results results;//接收的資料結構

#define PIN 6

Adafruit_NeoPixel strip = Adafruit_NeoPixel(256, PIN, NEO_GRB + NEO_KHZ800);
//const byte ledPin[] = {8,9,10,11,12,13};


byte buttonStateE = 0;      // 記錄按鈕目前的狀態,一開始設為0,表示按鈕未按下
byte lastButtonStateE = 0;  // 按鈕前一次的狀態﹐,一開始設為0,表示前一次的按鈕未按下
byte buttonStateM = 0;      // 記錄按鈕目前的狀態,一開始設為0,表示按鈕未按下
byte lastButtonStateM = 0;  // 按鈕前一次的狀態﹐,一開始設為0,表示前一次的按鈕未按下
byte buttonStateB = 0;      // 記錄按鈕目前的狀態,一開始設為0,表示按鈕未按下
byte lastButtonStateB = 0;  // 按鈕前一次的狀態﹐,一開始設為0,表示前一次的按鈕未按下
byte buttonStateC = 0;      // 記錄按鈕目前的狀態,一開始設為0,表示按鈕未按下
byte lastButtonStateC = 0;  // 按鈕前一次的狀態﹐,一開始設為0,表示前一次的按鈕未按下

const byte buttonPinEye = 2;
const byte buttonPinMouth = 3;
const byte btnBrightness = 4;
const byte btnColor = 5;

int BringLv = 51;

int Brightness = 255;
int intMaxArray = 5;

int irPointer = 0 ;//0=eye, 1 = mouth\
int strStoreCode = 0;
int intOut = -1;

int intMouth = -1 ;
int intEye = -1;
int intCol = 0;

int col_red[] = {255,0,0};
int col_pink[] = {255,161,249};
int col_orange[] = {255,123,0};
int col_green[] = {0,255,0};
int col_pu[] = {123,0,255};
int col_white[] = {255,255,255};


int * col[6] = {col_red, col_pink,col_orange, col_green, col_pu, col_white};

//眼
int eye0[] = {9,10,19,20,29,35,36,41,42,57,59,114,113,108,107,101,92,91,82,81,67,65,-1}; //> <
int eye1[] = {18,19,26,27,34,35,42,43,50,51,58,59,107,106,99,98,90,91,82,83,75,74,66,67,-1}; // | |
int eye2[] = {1,10,11,18,19,26,27,34,35,42,43,57,59,124,115,114,107,106,99,98,91,90,83,82,67,65,-1}; // | | + 眼毛
int eye3[] = {9,10,19,20,29,35,36,41,42,57,59,106,105,98,97,90,89,82,81,67,65,-1}; // > |
int eye4[] = {18,19,26,27,34,35,42,43,57,59,114,113,108,107,101,92,91,82,81,67,65,-1}; // | <
int eye5[] = {18,19,26,27,28,29,35,36,49,51,107,106,101,100,99,98,92,91,75,73,-1}; //  強眼
int eye6[] = {25,26,27,28,29,30,102,101,100,99,98,97,90,88,81,-1}; //  眼訓
int eye7[] = {2,3,9,12,21,28,35,51,124,123,117,114,105,98,91,75,-1}; //  ??
int eye8[] = {18,19,25,26,28,33,34,35,36,42,43,44,51,53,60,108,107,101,100,98,93,92,91,90,84,83,82,75,73,66,-1}; //  淚眼


int * eye[10]= {eye0,eye1,eye2,eye3,eye4,eye5,eye6,eye7,eye8};

//口
int mouth0[] = {146,154,163,164,165,166,159,213,216,225,226,227,228,-1}; //貓咀
int mouth1[] = {147,156,165,166,167,212,219,224,225,226,-1}; // U
int mouth2[] = {143,200,150,209,158,217,167,224,-1}; // O
int mouth3[] = {148,157,166,175,232,225,218,211,-1}; // V
int mouth4[] = {131,132,133,134,135,192,193,194,195,196,139,204,148,211,157,218,166,167,224,225,-1}; // 開口笑
int mouth5[] = {140,141,142,143,200,201,202,203,148,211,157,218,166,225,175,232,-1}; // 開口笑 2
int mouth6[] = {142,143,200,201,149,210,156,219,163,228,171,236,179,180,181,182,183,240,241,242,243,244,-1}; // 反口
int mouth7[] = {147,151,156,158,165,208,212,217,219,226,-1}; // W
int mouth8[] = {156,157,158,159,216,217,218,219,163,228,-1}; //  反口2


int * mouth[10]= {mouth0,mouth1,mouth2,mouth3,mouth4,mouth5,mouth6,mouth7,mouth8};

void setup() {
  Serial.begin(115200);
  pinMode( buttonPinEye , INPUT);
  pinMode( buttonPinMouth , INPUT);
  pinMode( btnBrightness , INPUT);
  pinMode( btnColor , INPUT);
  irrecv.enableIRIn(); //啟用紅外線接收器
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {


//ir
//====================================================
  if (irrecv.decode()) {

   // Serial.println(irrecv.decodedIRData.command);

    switch (irrecv.decodedIRData.command) {


    case 12:
        intOut = 1;
    break;
    case 24:
        intOut = 2;
    break;
    case 94:
        intOut = 3;
    break;
    case 8:
        intOut = 4;
    break;
    case 28:
        intOut = 5;
    break;
    case 90:
        intOut = 6;
    break;
    case 66:
        intOut = 7;
    break;
    case 82:
        intOut = 8;
    break;
    case 74:
        intOut = 9;
    break;
    case 22:
        intOut = 0;
    break;
    case 21: //b_up;
      changeBrightness(0);
      colorWipe(getColor(),intEye, intMouth);
    break;
    case 7: //b_down;
      changeBrightness(1);
      colorWipe(getColor(),intEye, intMouth);
    break;

    case 68:
        changeColor(1);
        colorWipe(getColor(),intEye, intMouth);
    break;
    case 64:
      changeColor(0);
      colorWipe(getColor(),intEye, intMouth);
    break;
    default:
      intOut = -1;
      intMouth = 1;
    break;
  }
  //  Serial.println("=================================================");
//  Serial.println(StrOut);

    if(intOut >0){
      if(irPointer==1){//mouth
        intMouth = intOut = 1;;
        //Serial.println("Mouth");
        irPointer = 0;
      }else{
        intEye = intOut = 1;;
        //Serial.println("Eye");
        irPointer=1;
      }
      colorWipe(getColor(),intEye, intMouth);
    }else{ //function
/*       intMouth = atoi(1);
       intEye = atoi(1);*/
    }
    delay(750);
    irrecv.resume(); //準備接收下一個訊息
  }


//remote
//====================================================
buttonStateE = digitalRead(buttonPinEye);  //讀取按鈕目前的狀態
buttonStateM = digitalRead(buttonPinMouth);
buttonStateB = digitalRead(btnBrightness);
buttonStateC = digitalRead(btnColor);



  if (buttonStateB != lastButtonStateB){
    if(buttonStateB == HIGH) {
      changeBrightness(-1);
      colorWipe(getColor(),intEye, intMouth);
    }
  }

  if (buttonStateC != lastButtonStateC){
    if(buttonStateC == HIGH) {
      changeColor(-1);
      colorWipe(getColor(),intEye, intMouth);
    }
  }

  if (buttonStateE != lastButtonStateE){
    if (buttonStateE == HIGH) {
      intEye++;
      if(intEye>=5){
        intEye = -1;
      }
      colorWipe(getColor(),intEye, intMouth);
    }
  }

  if (buttonStateM != lastButtonStateM){
    if (buttonStateM == HIGH) {
      intMouth++;
      if(intMouth>=5){
        intMouth = -1;
      }
      colorWipe(getColor(),intEye, intMouth);
    }
  }

lastButtonStateC = buttonStateC;
lastButtonStateE = buttonStateE;
lastButtonStateM = buttonStateM;
lastButtonStateB = buttonStateB;
}

uint32_t getColor(){   
  if(intCol==-1){
    intCol = 0;
  }
  return strip.Color(col[intCol][0] , col[intCol][1], col[intCol][2]);
}


void changeColor(int z){//1= -,0=+,-1 = +

  if(z==-1){
    intCol++;
  }else{
    if(z==1){
      intCol--;
    }else{
      intCol++;
    }
  }
  if(intCol==6){
    intCol = 0;
  }
  if(intCol==-1){
    intCol = 5;
  }
//Serial.println(intCol);
}

void changeBrightness( int y){ //1= -,0=+,-1 = +
  if(y==-1){
    Brightness = Brightness + BringLv;
    if(Brightness>=255 ){
      Brightness = 255;
    }  

  }else{

    if(y==1){
      Brightness = Brightness - BringLv;
      if(Brightness<0){
        Brightness=0;
      }
    }else{
      Brightness = Brightness + BringLv;
      if(Brightness>255){
        Brightness=255;}
      }
  }
  //Serial.println(Brightness);  
}

void  colorWipe(uint32_t color, int e, int m) {


  strip.clear();

   int y = 0;

  if(m<0){
    m=0;
  }

  while(mouth[m][y] != -1){
     //   Serial.println(mouth[m][y]);
    strip.setPixelColor(mouth[m][y], color);   
            Serial.println(mouth[m][y]);   
              strip.setBrightness(Brightness); //  (max = 255)  
  strip.show();           
    y++;
  }


  int x = 0;
  if(e<0){
    e=0;
  }
      Serial.println("---------------------------------------------");
  while(eye[e][x] != -1){
    strip.setPixelColor(eye[e][x], color);
      strip.setBrightness(Brightness); //  (max = 255)  
  strip.show();  
        Serial.println(eye[e][x]);         
    x++;
  }

      Serial.println("End Color");
}

发表于 2021-3-29 14:48 | 显示全部楼层
感觉是 ram 不够,

如果你的数据运行过程中不需要修改,你可以试试 PROGMEM 这个定义
发表于 2021-3-30 00:17 | 显示全部楼层
本帖最后由 shouzama 于 2021-3-30 00:33 编辑
Sketch uses 11106 bytes (34%) of program storage space. Maximum is 32256 bytes.
Global variables use 1292 bytes (63%) of dynamic memory, leaving 756 bytes for local variables. Maximum is 2048 bytes.

這段回應並沒有顯示有任何問題

請將失敗時的問題訊息貼出來比較好判斷,
另外,你貼的程式碼是沒問題的?還是會出問題的?
因為你說>6陣列就出錯,但程式中都>6陣列了,
貼出的訊息卻沒有報錯? 還是文章認知上有差異?
=====
一個 INT 佔用 2 BYTES,極有可能是 RAM 不足(>2KB),
看是要改成用 PROGMEM ,還是你設法只使用
0~255 的正整數(不要有 -1),資料型態就可以改為
只佔用 1 BYTE 的 byte 或 char


 楼主| 发表于 2021-4-1 14:35 | 显示全部楼层
shouzama 发表于 2021-3-30 00:17
這段回應並沒有顯示有任何問題

請將失敗時的問題訊息貼出來比較好判斷,

UPLOAD 時都沒有甚麼錯誤信息
用的是UNO (淘宝)
UPLOAD 可以正確完成
按IR沒有任何反應
但一把ARRAY 減到6個就正常了
在想會不是不夠RAM? ARRAY 放不到10 個.
 楼主| 发表于 2021-4-1 14:36 | 显示全部楼层
Zoologist 发表于 2021-3-29 14:48
感觉是 ram 不够,

如果你的数据运行过程中不需要修改,你可以试试 PROGMEM 这个定义 ...

好, 我試一下, 謝謝
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-29 10:44 , Processed in 0.136749 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表