Arduino Uno不想写变量

时间:2015-02-26 11:51:30

标签: c++ arduino

我正在写一个小草图来读取传感器值并将其打印在LCD屏幕上。 到目前为止,这种方法很有效,我记不起改变了什么,但突然间我的Arduino不再想要更改ringbuf变量的值,现在它总是在8224。

这些是代码中最相关的部分:

const int RING_SIZE = 5;
double ringbuf[RING_SIZE]; // array for the ring buffer
unsigned int ringpos = 0; // position in the ring buffer - this will always be 8224 for some reason

// in loop()

ringbuf[ringpos] = readSensor();
ringpos++;
if (ringpos >= RING_SIZE) {
    ringpos = 0;
}

以下是整个代码:

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

#include <Keypad.h>

#include <stdio.h>

int readpin = A2;
int irpin = 9;
const int BUFFER_SIZE = 20;
int buf[BUFFER_SIZE];
const int RING_SIZE = 5;
unsigned int ringpos = 0;
double ringbuf[RING_SIZE];
unsigned long lastRedTime = millis();
char text[20];

const byte ROWS = 4;
const byte COLS = 3;
const unsigned long BACKLIGHT_DURATION = 5000;
const double THRESHOLD = 245;

const double wattPerTurn = 1.666666667;
double lastUsage = 0;
bool currentState = LOW;

char keys[ROWS][COLS] = {{'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[] = {5, 4, 3, 2};
byte colPins[] = {8, 7, 6};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x20, 16, 2);

unsigned long lastKey = millis();
int turns = 0;


void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.home();

  pinMode(irpin, OUTPUT);
  analogReference(DEFAULT);
  digitalWrite(irpin, HIGH);
}

void loop() {
  Serial.print(ringpos);
  Serial.println();
  ringbuf[ringpos] = readSensor();
  ringpos++;
  if (ringpos >= RING_SIZE) {
    ringpos = 0;
  }

  double value = getAverage();
  if (value < THRESHOLD && currentState == HIGH) {
    currentState = LOW;

    lastUsage = wattPerTurn / (double) ((millis() - lastRedTime) / 1000) * (double) 3600;

    turns ++;
    lastRedTime = millis();
    lcd.clear();
    lcd.setCursor(10, 0);
    lcd.print("rot");
  }
  if (value > THRESHOLD && currentState == LOW) {
    currentState = HIGH;
    lcd.clear();
    lcd.setCursor(10, 0);
    lcd.print("silber");
  }
  lcd.setCursor(0, 0);

  sprintf(text, "%i W", lastUsage);
  lcd.print(text);

  lcd.setCursor(0, 1);
  sprintf(text, "s: %i, t: %i Wh   ", (int) value, (int) (((double) turns) * wattPerTurn));
  lcd.print(text);

  delay(20);
}

double getRelative(int r) {
  int v = ringpos + r;
  while (v < 0) {
    v += RING_SIZE;
  }
  while (v >= RING_SIZE) {
    v -= RING_SIZE;
  }
  return ringbuf[v];
}

double getAverage() {
  double sum = 0;
  for (int i = 0; i < RING_SIZE; i++) {
    sum += ringbuf[i];
  }
  return sum / (double) RING_SIZE;
}

double readSensor() {
  for (int i = 0; i < BUFFER_SIZE; i++) {
    buf[i] = analogRead(readpin);
  }

  int sum = 0;
  for (int i = 0; i < BUFFER_SIZE; i++) {
    sum += buf[i];
  }

  return (double) sum / (double) BUFFER_SIZE;
}

我可能错过了一些非常愚蠢的东西,但我不知道为什么这会停止工作。我也在另一个Arduino上测试了这个,同样的事情发生了。

1 个答案:

答案 0 :(得分:2)

请更新问题以直接包含所有相关代码,而不是在pastie上链接到它。错误不在您此处显示的代码中,而是您的text变量

char text[10];

对于

来说太短了
sprintf(text, "s: %i, t: %i Wh   ", (int) value, (int) (((double) turns) * wattPerTurn));

你稍后会这样做,所以这会超出text超过RAM中text后面的变量。据推测,ringpos碰巧是在一个案例中被覆盖但在另一个案例中被覆盖的,但当然这会导致UB,无论其行为方式如何都不可靠。

要解决此问题,请将text设置得足够大。