Arduino草图中未访问第一个数组值

问题描述

我正在创建一个无需使用RTC模块即可重新创建时钟的代码。我忙于添加这些日子。遍历数组时出现问题。当index==0时,它给出随机形式的char。 这是无效的代码

int theDayindex=0;
String weekDay[]={"maandag","dinsdag","woensdag","donderdag","vrijdag","zaterdag","zondag"};
String theDaydisplay=weekDay[theDayindex];
    theDayindex++;
    theDaydisplay=weekDay[theDayindex];
    if(theDayindex>=6){
      theDayindex=0;
    }

我认为问题在于,当您循环遍历代码时,它不会检测到0作为索引。我对C语言不是很熟悉,因此可能会有所不同。

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <dht.h>
#include <NewPing.h>

int page = 1;
int secs = 0;
int mins = 52;
int hors = 20;
int T;
int H;
int IRpin = 13;
int Timer = 0;
int theDayindex = 0;
String weekDay[] = {
  "maandag","zondag"
};
String theDaydisplay = weekDay[theDayindex];

hd44780_I2Cexp lcd;
dht DHT;
NewPing sonar(11,12,400);

void setup() {
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.init();
  lcd.backlight();
}

void loop() {
  secs++;
  Timer++;
  theDayindex++;
  theDaydisplay = weekDay[theDayindex];
  if (theDayindex >= 6) {
    theDayindex = 0;
  }
  Serial.println(theDayindex);
  Serial.println(theDaydisplay);
  unsigned int dstnc = sonar.ping_cm();
  if (dstnc <= 30) {
    lcd.backlight();
    lcd.display();
    Timer = 0;
    page++;
  }
  if (secs >= 60) {
    secs = 0;
    mins++;
    lcd.clear();
  }
  if (mins >= 60) {
    hors++;
    mins = 0;
    secs = 0;
    lcd.clear();
  }
  if (hors >= 24) {
    hors = 0;
    mins = 0;
    secs = 0;
    lcd.clear();
    theDayindex++;
    theDaydisplay = weekDay[theDayindex];
    if (theDayindex >= 6) {
      theDayindex = 0;
    }
  }
  if (Timer >= 60) {
    page = 1;
    lcd.noBacklight();
    lcd.nodisplay();
  }
  if (page == 1) {
    lcd.clear();
    lcd.setCursor(0,2);
    lcd.print(theDaydisplay);
    lcd.setCursor(0,0);
    lcd.print(hors);
    lcd.setCursor(2,0);
    lcd.print(":");
    lcd.setCursor(3,0);
    lcd.print(mins);
    lcd.setCursor(5,0);
    lcd.print(":");
    lcd.setCursor(6,0);
    lcd.print(secs);
  }
  if (page == 2) {
    int chk = DHT.read11(7);
    H = DHT.humidity;
    T = DHT.temperature;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Hum:");
    lcd.setCursor(4,0);
    lcd.print(H);
    lcd.setCursor(9,0);
    lcd.print("temp: ");
    lcd.setCursor(14,0);
    lcd.print(T);
  }
  if (page >= 3) {
    page = 1;
  }
  delay(955);
}

解决方法

如果问题是循环无法访问第一个索引,则罪魁祸首是:

theDayindex++;

theDayindex首先设置为0,但在循环开始时立即增加。因此,只需将索引增量移动到循环的结尾即可。

#include <Wire.h>

#include <hd44780.h>

#include <hd44780ioClass/hd44780_I2Cexp.h>

#include <dht.h>

#include <NewPing.h>

int page = 1;
int secs = 0;
int mins = 52;
int hors = 20;
int T;
int H;
int IRpin = 13;
int Timer = 0;
int theDayindex = 0;
String weekDay[] = {
  "maandag","dinsdag","woensdag","donderdag","vrijdag","zaterdag","zondag"
};
String theDaydisplay = weekDay[theDayindex];

hd44780_I2Cexp lcd;
dht DHT;
NewPing sonar(11,12,400);

void setup() {
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.init();
  lcd.backlight();
}

void loop() {
  theDaydisplay = weekDay[theDayindex];
  if (theDayindex >= 6) {
    theDayindex = 0;
  }
  Serial.println(theDayindex);
  Serial.println(theDaydisplay);
  unsigned int dstnc = sonar.ping_cm();
  if (dstnc <= 30) {
    lcd.backlight();
    lcd.display();
    Timer = 0;
    page++;
  }
  if (secs >= 60) {
    secs = 0;
    mins++;
    lcd.clear();
  }
  if (mins >= 60) {
    hors++;
    mins = 0;
    secs = 0;
    lcd.clear();
  }
  if (hors >= 24) {
    hors = 0;
    mins = 0;
    secs = 0;
    lcd.clear();
    theDayindex++;
    theDaydisplay = weekDay[theDayindex];
    if (theDayindex >= 6) {
      theDayindex = 0;
    }
  }
  if (Timer >= 60) {
    page = 1;
    lcd.noBacklight();
    lcd.noDisplay();
  }
  if (page == 1) {
    lcd.clear();
    lcd.setCursor(0,2);
    lcd.print(theDaydisplay);
    lcd.setCursor(0,0);
    lcd.print(hors);
    lcd.setCursor(2,0);
    lcd.print(":");
    lcd.setCursor(3,0);
    lcd.print(mins);
    lcd.setCursor(5,0);
    lcd.print(":");
    lcd.setCursor(6,0);
    lcd.print(secs);
  }
  if (page == 2) {
    int chk = DHT.read11(7);
    H = DHT.humidity;
    T = DHT.temperature;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Hum:");
    lcd.setCursor(4,0);
    lcd.print(H);
    lcd.setCursor(9,0);
    lcd.print("temp: ");
    lcd.setCursor(14,0);
    lcd.print(T);
  }
  if (page >= 3) {
    page = 1;
  }
  delay(955);

  secs++;
  Timer++;
  theDayindex++;
}
,

在Stormix的提示下,您永远不会使用My dictionary: variable 'a0' is '/tmp/PhotoFile/photo09' My dictionary: variable 'a1' is '/tmp/PhotoFile/photo08' My dictionary: variable 'a2' is '/tmp/PhotoFile/photo07' My dictionary: variable 'a3' is '/tmp/PhotoFile/photo06' My dictionary: variable 'a4' is '/tmp/PhotoFile/photo05' My dictionary: variable 'a5' is '/tmp/PhotoFile/photo04' My dictionary: variable 'a6' is '/tmp/PhotoFile/photo03' My dictionary: variable 'a7' is '/tmp/PhotoFile/photo02' My dictionary: variable 'a8' is '/tmp/PhotoFile/photo01' My dictionary: variable 'a9' is '/tmp/PhotoFile/photo00' 。首次执行weekDay[0]时,theDaydisplay=weekDay[theDayindex]已递增,因此您正在访问theDayIndex。只需更改行顺序即可解决您的问题。

weekDay[1]