[英]Arduino Uno does not want to write variable
我正在寫一個小草圖以讀取傳感器值並將其打印在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上進行了測試,同樣的事情發生了。
請更新問題,以直接包含所有相關代碼,而不是在貼上鏈接到該代碼。 錯誤不在您在此處顯示的代碼中,是您的text
變量
char text[10];
太短了
sprintf(text, "s: %i, t: %i Wh ", (int) value, (int) (((double) turns) * wattPerTurn));
您稍后再做,因此這將超出text
覆蓋了RAM中恰好位於text
后面的變量。 據推測, ringpos
恰好是在一種情況下被覆蓋的,而在另一種情況下卻未被覆蓋,但這當然會導致UB,並且無論哪種方式都不可靠。
要解決此問題,請使text
足夠大。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.