- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经在串行监视器上显示输出,但我想要将输出保存到 EEPROM。我如何循环10次并收集10个数据并将10个数据存储到EEPROM?据我了解,我需要创建一个计数器来计数到 10。我需要为我的变量使用数组吗?还有一个 while 循环,我对吗?
编辑:我指的是ARDUINO
这是代码。我正在使用旧的加速度计代码。正如你所看到的,代码存储在之前的 X、Y、Z 轴和 g 级别中。但现在我想存储速度和距离的值。使用while循环让输出运行10次然后将其存储到EEPROM。
#include <math.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#define O0 11
#define O1 10
#define O2 9
#define O3 6
#define O4 5
#define O5 3
#define I0 A0
#define I1 A1
#define I2 A2
#define I3 A3
#define I4 A4
#define I5 A5
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin1 = I0; // Analog input pin that the Accelerometer's first pin is attached to
const int analogInPin2 = I1; // Analog input pin that the Accelerometer's second pin is attached to
const int analogInPin3 = I2; // z-axis
const float LPF_alpha = 0.16; //LPF_Alpha=dT/(RC+dT)
const float HPF_alpha = 0.83; //HPF_Alpha=RC/(RC+dT)
const float gNlowB=0.038,GNlowB=0.008;
const float gNhighB=1,GNhighB=1;
float ValueX,ValueY,ValueZ; //raw data from ADC
//acc date wrt to g value of earth, gN is the norm
//gNavg is after LPF (the constant due to earth). gNins should be 0 if not moving...
//these data do averaging on individual axis
float gX,gY,gZ,gXA,gYA,gZA,gXP,gYP,gZP;
float gXins,gYins,gZins,gNins,gXinsP,gYinsP,gZinsP,gNinsP;
float gXinsA=0,gYinsA=0,gZinsA=0,gNinsA=0;
float GN,GNP,GNA,GNins,GNinsA=0;
float gNinsMax,GNinsMax;
float actgN_acc=0,actGN_acc=0;
float actgN,actGN;
float act_dist=0,act_DIST=0;
float gX_hpf=0,gY_hpf=0,gZ_hpf=0,GN_hpf=0;
//these data convert to norm early on, and do avg/ins
float gN_hpf=0;
float vel=0, dist=0, VEL=0,DIST=0;
float aX, aY, aZ; //tilt angle
float vdd=4.91; //the measured 5V supply. Meter reading is more accurate
//sensitivity from datasheet is 0.5V/g. unit of sens is g/value. 1024=vdd
//this group of data can be calibrated and stored in EEPROM
float sens=(vdd/1024)/0.5000;
float sensX=sens; //without calibration, default value is from the datasheet
float sensY=sens;
float sensZ=sens;
float mid=1023/2;
float midX=mid;
float midY=mid;
float midZ=mid;
unsigned long time,preTime=0,curTime; //dT need for integration
float dT=0,aT=0;
int noAvg=10; //averaging of 10 reading
int dispCnt=1,actCnt=0; //display 1/10 the data or else human can't read. actCnt must be offset from dispCnt
int keyResponse;
struct calData_t
{ int written; //99 if written
float mx,my,mz,sx,sy,sz;
} ;
calData_t calData;
struct gVector_t{
float gx,gy,gz;
};
gVector_t gV,gVNorm;
float readAcc(float &ValueX,float &ValueY, float &ValueZ, char One, int noAvg);
void calOne(char One, float &mid, float &sens);
void LPF(float &d, float &dAvg, float &dIns); //LPF 1 data
void Max(float &Max, float &d);
void reset_vel(float &a, float lowB, float &v);
int readkey() {
// read 1 char from serial monitor
int c;
while(!Serial.available());
c= Serial.read(); //read 1 byte only read 1 byte
delay(100);
while(Serial.available()>0){
Serial.read(); //keeping reading. Clear the buffer until Serial.available()==0
}
return c;
}
void calibration(){
calOne('z',midZ,sensZ);
calOne('x',midX,sensX);
calOne('y',midY,sensY);
dispCalData();
storeCalData();
}
void storeCalData(){
calData=(calData_t){99 , midX,midY,midZ,sensX,sensY,sensZ};
while (!eeprom_is_ready()); // Wait for EEPROM to be ready
cli();
eeprom_write_block((const void*)&calData, (void*)0, sizeof(calData));
Serial.println("Cal data written to EEPROM...");
sei();
}
void dispCalData(){
Serial.println("The current middle value and sensitivity are: ");
Serial.print("sensX:");
Serial.print(sensX,5);
Serial.print("\tsensY:");
Serial.print(sensY,5);
Serial.print("\tsensZ");
Serial.print(sensZ,5);
Serial.print("\tsens without cal:");
Serial.println(sens,5);
Serial.print("midX:");
Serial.print(midX,5);
Serial.print("\tmidY:");
Serial.print(midY,5);
Serial.print("\tmidZ");
Serial.print(midZ,5);
Serial.print("\tmid without cal:");
Serial.println(mid,5);
delay(5000);
}
void calOne(char One, float &mid, float &sens){
//when you're cal z=+1g, you could cal for x=y=0g
//this only need to perfm 6 cal instead of 9 cal
//however, it is more messy in the code and hard to trace if there is cal error.
float Pos1g=0, Neg1g=0;
String String1=String(One)+"=0g cal: put "+String(One)+ "=0g and hit 'y' & RETURN";
String String2=String(One)+"=+1g cal: put +"+String(One)+ " facing upwards and hit 'y' & RETURN";
String String3=String(One)+"=-1g cal: put +"+String(One)+ " facing downwards and hit 'y' & RETURN";
Serial.println(String1);
keyResponse=readkey();
if (keyResponse=='y'){
delay(1000);
mid=readAcc(ValueX,ValueY,ValueZ,One,100);
}
Serial.println(String2);
keyResponse=readkey();
if (keyResponse=='y'){
delay(1000);
Neg1g=readAcc(ValueX,ValueY,ValueZ,One, 100);
}
Serial.println(String3);
keyResponse=readkey();
if (keyResponse=='y'){
delay(1000);
Pos1g=readAcc(ValueX,ValueY,ValueZ,One,100);
}
if (Pos1g==0 || Neg1g==0){}
else {sens=2/(Pos1g-Neg1g);}
}
float readAcc(float &ValueX,float &ValueY, float &ValueZ, char One, int noAvg){
//read acc data (from ADC) with averaging. Return values via pointers.
//parameter One allow you to return 1 axis acc reading.
float X=0; float Y=0; float Z=0;
for (int i=0; i < noAvg; i++){ //int is 16bit
X = X+analogRead(analogInPin1);
Y = Y+analogRead(analogInPin2);
Z = Z+analogRead(analogInPin3);
delay(10); //datasheet suggest 10ms delay for ADC to settle
}
ValueX=X/noAvg;
ValueY=Y/noAvg;
ValueZ=Z/noAvg;
if (One == 'x') { return ValueX;}
else if (One == 'y'){ return ValueY;}
else { return ValueZ;}
}
void Max(float &Max, float &d){ //max is keyword
if (d>Max){Max=d;}
}
void cap(float &d,float lowB, float highB){
if (d<lowB){d=0;}
else if (d>highB){d=highB;}
else {;}
}
void reset_vel(float &a, float lowB, float &v){
if (a<lowB){v=0;}
}
float mapAcc(float x, float mid, float sens){
//what return is the actual acc data in g
return sens*(x-mid);
}
float norm(float x, float y, float z){
return sqrt(x*x+y*y+z*z);
}
void LPF(float &gN, float &gNP, float &gNA, float &gNins){
gNA=LPF_alpha*gN + (1-LPF_alpha)*gNA;
gNins=gN-gNA;
}
void HPF(float &gin, float &ginP, float &gout){
gout=HPF_alpha*(gout+gin-ginP);
}
void display_result(int delayLoop){
delay(delayLoop);
Serial.print(" gXins=" );
Serial.print(gXins,5);
Serial.print(" gYins=" );
Serial.print(gYins,5);
Serial.print(" gZins=" );
Serial.print(gZins,5);
Serial.print(" gNins=" );
Serial.print(gNins,5);
Serial.print(" gXinsA=" );
Serial.print(gXinsA*1e3,5);
Serial.print(" gYinsA=" );
Serial.print(gYinsA*1e3,5);
Serial.print(" gZinsA=" );
Serial.print(gZinsA*1e3,5);
Serial.print(" gNinsA=" );
Serial.print(gNinsA*1e3,5);
Serial.print('\n');
Serial.print(" gNins=" );
Serial.print(gNins,5);
Serial.print(" GNins=" );
Serial.print(GNins,5);
Serial.print(" gNinsA=" );
Serial.print(gNinsA,5);
Serial.print(" GNinsA=" );
Serial.print(GNinsA,5);
Serial.print(" gNinsMax=" );
Serial.print(gNinsMax,5);
Serial.print(" GNinsMax=" );
Serial.print(GNinsMax,5);
Serial.print("\n");
Serial.print(" aT=" );
Serial.print(aT/1e6);
Serial.print(" dT=" );
Serial.print(dT/1000,3);
Serial.print(" dispCnt=" );
Serial.print(dispCnt);
Serial.print(" actCnt=" );
Serial.print(actCnt);
Serial.print("\n");
Serial.print(" vel=" );
Serial.print(vel,3);
Serial.print(" VEL=" );
Serial.print(VEL,3);
Serial.print(" dist=" );
Serial.print(dist,3);
Serial.print(" DIST=" );
Serial.print(DIST,3);
Serial.print(" actgN=" );
Serial.print(actgN,3);
Serial.print(" actGN=" );
Serial.print(actGN,3);
Serial.print(" act_dist=" );
Serial.print(act_dist,3);
Serial.print(" act_DIST=" );
Serial.print(act_DIST,3);
Serial.println(' ');
Serial.print("\n");
}
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
Serial.println("Hit 'y' and RETURN, if you want to calibrate...");
Serial.println("Hit 'x' and RETURN, if you want to clear the cal data in EEPROM...");
Serial.println("If not, 'n' and RETURN");
//keyResponse=readkey();
keyResponse='n';
if (keyResponse=='y'){
calibration();
}
else if (keyResponse=='x'){
while (!eeprom_is_ready()); // Wait for EEPROM to be ready
cli();
calData.written=0;
eeprom_write_block((const void*)&calData, (void*)0, sizeof(calData));
Serial.println("Cal data is cleared...");
sei();
}
else {
while (!eeprom_is_ready()); // Wait for EEPROM to be ready
cli();
eeprom_read_block((void*)&calData, (void*)0, sizeof(calData));
sei();
if (calData.written==99){
Serial.println("Using previously stored cal data from EEPROM");
midX=calData.mx; midY=calData.my; midZ=calData.mz;
sensX=calData.sx;sensY=calData.sy;sensZ=calData.sz;
}
else {
Serial.println("no previous cal data, using default");
}
dispCalData();
}
readAcc(ValueX,ValueY,ValueZ,'x',10);
gXA = mapAcc(ValueX, midX, sensX); //cannot let gXA at 0, will create err.
gYA = mapAcc(ValueY, midY, sensY);
gZA = mapAcc(ValueZ, midZ, sensZ);
GNA = norm(gXA,gYA,gZA);
preTime=micros();
}
void loop() {
// read the both analog in values:
readAcc(ValueX,ValueY,ValueZ,'x',10);
curTime=micros();
dT=curTime-preTime;
preTime=curTime;
aT=aT+dT;
gX = mapAcc(ValueX, midX, sensX);
gY = mapAcc(ValueY, midY, sensY);
gZ = mapAcc(ValueZ, midZ, sensZ);
LPF(gX,gXP,gXA,gXins);
LPF(gY,gYP,gYA,gYins);
LPF(gZ,gZP,gZA,gZins);
gNins=norm(gXins,gYins,gZins);
cap(gNins,0.01,1);
gXinsA=(gXinsA+gXins)/2;
gYinsA=(gYinsA+gYins)/2;
gZinsA=(gZinsA+gZins)/2;
gNinsA=(gNinsA+gNins)/2;
GN = norm(gX,gY,gZ);
LPF(GN,GNP,GNA,GNins);
cap(GNins,GNlowB,GNhighB);
GNinsA=(GNinsA+GNins)/2;
Max(gNinsMax,gNins);
Max(GNinsMax,GNins);
reset_vel(gNins, gNlowB, vel);
reset_vel(GNins, GNlowB, VEL);
vel=vel+9.81e-6*gNins*dT;
VEL=VEL+9.81e-6*GNins*dT;
act_dist=act_dist+9.81e-6*vel*dT;
act_DIST=act_DIST+9.81e-6*VEL*dT;
actCnt=actCnt+1;
if (actCnt>=10){
actGN=sqrt(actGN_acc/10);
actGN_acc=0;
actgN=sqrt(actgN_acc/10);
actgN_acc=0;
dist=dist+act_dist; act_dist=0;
DIST=DIST+act_DIST; act_DIST=0;
actCnt=0;
}
actgN_acc=actgN_acc+(gNins*gNins);
actGN_acc=actGN_acc+(GNins*GNins);
// print the results to the serial monitor:
dispCnt=dispCnt+1;
if (dispCnt>=40){
display_result(0);
dispCnt=0;
}
gXP=gX; gYP=gY; gZP=gZ; GNP=GN;
}
最佳答案
我假设你想要这样的东西
#define MAX_VAL 10
int arrvalues[MAX_VAL] = {0,10,20,30,40,50,60,70,80,90};
现在你可以像使用for..loop
一样编写
int i;
for ( i = 0; i < MAX_VAL ; ++i )
EEPROM.write ( i, arrvalues[ i ] );
使用while...循环
int i=0;
while(i<MAX_VAL){
EEPROM.write ( i, arrvalues[ i ] );
i++;
}
关于收集数据并将其存储到 EEPROM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25033308/
我有一个带有定制 I2C Master 硬件的 X86 CPU。我的 Linux 是 Ubuntu 14.04,内核 3.13。 我为我的自定义 I2C 主硬件编写了一个 I2c 驱动程序。 当我加载
我已经在串行监视器上显示输出,但我想要将输出保存到 EEPROM。我如何循环10次并收集10个数据并将10个数据存储到EEPROM?据我了解,我需要创建一个计数器来计数到 10。我需要为我的变量使用数
我正在尝试将 M95M02-DR 256KB EEPROM 存储芯片与 MSP430 微 Controller 连接起来。作为示例测试,我尝试向其中写入以下字符串: CHAPTER I. Down t
我有一些代码可以将配置数据存储到 EEPROM。变量配置中的数据可以打印到串行控制台,但不能保存到 EEPROM。我正在使用 ESP32(Arduino 框架)。以下代码是此 link 中代码的简化版
我正在编写一个类来将数据保存到 Arduino 的 EEPROM。该类称为内存。 该类包含不同的函数和变量。 char serverdefault[15] = "0032484716340"; int
我正在使用 Arduinos(Nano 板)进行 DIY 项目。我在第一个和第二个 Arduinos 之间的串行通信有问题。第一个 Arduino 向第二个发送一个字符(字母 r),这就是第二个 Ar
我正在团队中使用 stm32 f100 系列开发板进行一个项目。我应该使用外部 I2C eeprom 来存储一些数据,eeprom 如下:CAT24C512WI-GT3 我也有这个 I2C eepro
我为自己的 PC 桥使用 Teensy++ 2.0 为自己构建了一个基本的 EEPROM 刻录机,它运行良好,但当我希望扩展其兼容性时,我的代码变得相当笨拙。我正在寻找一些建议,以进行适当的设计以使此
我最近发现需要将不经常更新的配置变量存储在微 Controller 的 EEPROM 中。将状态添加到程序中会立即迫使人们担心 检测 EEPROM 中未初始化的数据(即首次启动), 转换旧固件版本的数
我正在开发一个项目,其中我需要能够将值存储到 PIC 的 EEPROM 中,我已设法使读取工作正常,但到目前为止我无法让 PIC 写入内存使用PIC汇编。这是我一直在使用的代码,它源自数据手册中的示例
我正在做我的大学项目,需要将数据存储在AtMega32的EEPROM中。我能够在内存的任何特定位置写入和读取数据。但是当我尝试从地址 0 到 1023 顺序写入数据时,我得到了错误的值。 这是我编写的
我正在现有板上开发一个应用程序。应用程序需要频繁的数据(仅10字节)存储,所以我想将外部闪存模拟为 EEPROM因为我的主板没有 EEPROM。我们有外部 spi 闪存。任何人都可以在这个模拟中帮助我
当我从 eeprom 接收数据时遇到问题。 首先我编写了以下代码: #include #include char NAME[5] EEMEM = "a001"; char
我正在尝试使用 atmega324p 从外部 eeprom 读取数据。问题出在 for 循环上。我无法读取 4 页 (4 x 64B) 的 EEPROM 数据。如果我将循环计数器从 4 增加到 5,我
我使用以下代码来访问 eeprom : void EEPROM_write(unsigned int uiAddress,unsigned char ucData) { while(EECR &
我正在尝试为 AVR STK600 板编写一个程序,其中循环显示 LED。当按下开关时,相应的 LED 会亮起(到目前为止,一切顺利)。现在正在向开发板添加另一个功能,尝试使用 USART(RS232
我收到警告: Initialization makes pointer from integer without a cast 这是我的代码: case 4: { uint8_t* adr =
我卡在这里了。我正在尝试使用我的修改版本 this 将板配置保存/加载到 ESP8266 中的 EEPROM示例。 这是我的实际代码: char ssid[32] = ""; char passwor
我想按照 Arduino 函数从 EEPROM 地址读取并返回一个字符串,我可以在我的串行监视器上打印该字符串。 char EEPROM_read(int addr, int length_str)
我正在使用 Arduino Uno 将几个变量保存到 EEPROM 中。我的解决方案是创建一个自定义结构,在其中存储所有需要的变量,然后使用 EEPROM.put 将它们存储到内置 EEPROM 中。
我是一名优秀的程序员,十分优秀!