如何使用arduino上的AnalogRead值填充数组。每一秒arduino都从Analog0读取值,我想将这些读数放入数组。
假设您要读取最多100个值,请执行以下操作:
1.技术不佳(与一起使用阻止代码delay()):
//let's say you want to read up to 100 values
const unsigned int numReadings = 100;
unsigned int analogVals[numReadings];
unsigned int i = 0;
void setup()
{
}
void loop()
{
analogVals[i] = analogRead(A0);
i++;
if (i>=numReadings)
{
i=0; //reset to beginning of array, so you don't try to save readings outside of the bounds of the array
}
delay(1000); //wait 1 sec
}
Run Code Online (Sandbox Code Playgroud)
如果这对您有用,请用绿色对勾标记我的答案,以表示这是一个很好的答案。
注意:您不能使括号中的数字太大。例如:analogVals [2000]无法使用,因为它占用了过多的RAM。
PS。这是Arduino在网站帮助中涵盖的非常基本的内容。请从现在开始参考有关此类问题的信息,并先自己尝试一下:http : //arduino.cc/en/Reference/HomePage- >单击“数据类型”下的“数组”。
2.备用方法(由于使用阻塞代码和,因此也是一种较差的方法delay()):
//let's say you want to read up to 100 values
const unsigned int numReadings = 100;
unsigned int analogVals[numReadings];
void setup()
{
}
void loop()
{
//take numReadings # of readings and store into array
for (unsigned int i=0; i<numReadings; i++)
{
analogVals[i] = analogRead(A0);
delay(1000); //wait 1 sec
}
}
Run Code Online (Sandbox Code Playgroud)
更新:2018年10月6日
3.最佳技术(非阻塞方法-不delay!):
//let's say you want to read up to 100 values
const unsigned int numReadings = 100;
unsigned int analogVals[numReadings];
unsigned int i = 0;
void setup()
{
Serial.begin(115200);
}
void loop()
{
static uint32_t tStart = millis(); // ms; start time
const uint32_t DESIRED_PERIOD = 1000; // ms
uint32_t tNow = millis(); // ms; time now
if (tNow - tStart >= DESIRED_PERIOD)
{
tStart += DESIRED_PERIOD; // update start time to ensure consistent and near-exact period
Serial.println("taking sample");
analogVals[i] = analogRead(A0);
i++;
if (i>=numReadings)
{
i = 0; //reset to beginning of array, so you don't try to save readings outside of the bounds of the array
}
}
}
Run Code Online (Sandbox Code Playgroud)
4.专业类型的方法(非阻塞方法,改为通过传递指针来避免全局变量,使用C stdint类型,并使用静态变量来存储本地持久数据):
// Function prototypes
// - specify default values here
bool takeAnalogReadings(uint16_t* p_numReadings = nullptr, uint16_t** p_analogVals = nullptr);
void setup()
{
Serial.begin(115200);
Serial.println("\nBegin\n");
}
void loop()
{
// This is one way to just take readings
// takeAnalogReadings();
// This is a way to both take readings *and* read out the values when the buffer is full
uint16_t numReadings;
uint16_t* analogVals;
bool readingsDone = takeAnalogReadings(&numReadings, &analogVals);
if (readingsDone)
{
// Let's print them all out!
Serial.print("numReadings = "); Serial.println(numReadings);
Serial.print("analogVals = [");
for (uint16_t i=0; i<numReadings; i++)
{
if (i!=0)
{
Serial.print(", ");
}
Serial.print(analogVals[i]);
}
Serial.println("]");
}
}
// Function definitions:
//---------------------------------------------------------------------------------------------------------------------
// Take analog readings to fill up a buffer.
// Once the buffer is full, return true so that the caller can read out the data.
// Optionally pass in a pointer to get access to the internal buffer in order to read out the data from outside
// this function.
//---------------------------------------------------------------------------------------------------------------------
bool takeAnalogReadings(uint16_t* p_numReadings, uint16_t** p_analogVals)
{
static const uint16_t NUM_READINGS = 10;
static uint16_t i = 0; // index
static uint16_t analogVals[NUM_READINGS];
const uint32_t SAMPLE_PD = 1000; // ms; sample period (how often to take a new sample)
static uint32_t tStart = millis(); // ms; start time
bool bufferIsFull = false; // set to true each time NUM_READINGS have been taken
// Only take a reading once per SAMPLE_PD
uint32_t tNow = millis(); // ms; time now
if (tNow - tStart >= SAMPLE_PD)
{
Serial.print("taking sample num "); Serial.println(i + 1);
tStart += SAMPLE_PD; // reset start time to take next sample at exactly the correct pd
analogVals[i] = analogRead(A0);
i++;
if (i >= NUM_READINGS)
{
bufferIsFull = true;
i = 0; // reset to beginning of array, so you don't try to save readings outside of the bounds of the array
}
}
// Assign the user-passed-in pointers so that the user can retrieve the data if they so desire to do it this way
if (p_numReadings != nullptr)
{
*p_numReadings = NUM_READINGS;
}
if (p_analogVals != nullptr)
{
*p_analogVals = analogVals;
}
return bufferIsFull;
}
Run Code Online (Sandbox Code Playgroud)
上面最后一个代码的示例输出:
开始
取样品编号1
取样品编号2
取样品编号3
取样品编号4
取样品编号5
取样品编号6
取样品编号7
取样品编号8
取样品编号9
取样品编号10
numReadings = 10
AnalogVals = [1023,1023, 1023,1023,1023,687,0,0,0,0]
取样品数1
取样品数2
取样品数3
取样品数4
取样品数5
取样品数6
取样品数7
取样品数8
取样品num 9
抽取样本num 10
numReadings = 10
AnalogVals = [0,0,0,0,0,0,0,0,0,0]
| 归档时间: |
|
| 查看次数: |
9726 次 |
| 最近记录: |