C 收集数据并存储到EEPROM
我已经在串行监视器上显示了输出,但我想将输出保存到EEPROM。如何循环10次,收集10个数据并将10个数据存储到EEPROM?为了我的理解,我需要创建一个计数器,最多可以数到10。我的变量是否需要使用数组?还有一段时间,对吗 编辑:我指的是ARDUINO 这是密码。我用的是旧的加速计代码。如你所见,代码存储在前一个X、Y、Z轴的g级。但是现在我想存储速度和距离的值。使用while循环让输出运行10次,然后将其存储到EEPROMC 收集数据并存储到EEPROM,c,arduino,C,Arduino,我已经在串行监视器上显示了输出,但我想将输出保存到EEPROM。如何循环10次,收集10个数据并将10个数据存储到EEPROM?为了我的理解,我需要创建一个计数器,最多可以数到10。我的变量是否需要使用数组?还有一段时间,对吗 编辑:我指的是ARDUINO 这是密码。我用的是旧的加速计代码。如你所见,代码存储在前一个X、Y、Z轴的g级。但是现在我想存储速度和距离的值。使用while循环让输出运行10次,然后将其存储到EEPROM #include <math.h> #include
#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;
}
#包括
#包括
#包括
#定义O011
#定义O110
#定义O2 9
#定义O3 6
#定义O4 5
#定义O5 3
#定义i0a0
#定义i1a1
#定义i2a2
#定义I3 A3
#定义I4 A4
#定义I5 A5
//这些常数不会改变。它们是用来命名的
//对于所使用的管脚:
常量int analogInPin1=I0;//加速度计的第一个引脚连接到的模拟输入引脚
const int analogInPin2=I1;//加速度计的第二个引脚连接到的模拟输入引脚
常量int analogInPin3=I2;//z轴
常数浮点LPF_α=0.16//LPF_α=dT/(RC+dT)
常数浮点HPF_α=0.83//HPF_α=RC/(RC+dT)
常量浮点gNlowB=0.038,gNlowB=0.008;
常量浮点gNhighB=1,gNhighB=1;
浮动值X、值Y、值Z//来自ADC的原始数据
//acc date wrt至地球的g值,gN为标准值
//gNavg在LPF之后(由于接地而产生的常数)。如果不移动,GNIN应为0。。。
//这些数据在各个轴上进行平均
浮动gX、gY、gZ、gXA、gYA、gZA、gXP、gYP、gZP;
浮动gXins、gYins、gZins、gNins、gXinsP、gYinsP、gZinsP、gNinsP;
浮动gXinsA=0,gYinsA=0,gZinsA=0,gNinsA=0;
浮动GN、GNP、GNA、GNIN、GNNSA=0;
浮动gNinsMax,gNinsMax;
浮点数actgN_acc=0,actgN_acc=0;
浮动actgN,actgN;
浮动动作距离=0,动作距离=0;
浮点数gX_hpf=0,gY_hpf=0,gZ_hpf=0,GN_hpf=0;
//这些数据在早期转换为标准值,并进行平均/惯性测量
浮点数gN_hpf=0;
浮动水平=0,距离=0,水平=0,距离=0;
浮斧,aY,aZ//倾角
浮动vdd=4.91//测量的5V电源电压。抄表更准确
//数据表中的灵敏度为0.5V/g。传感器单位为克/值。1024=vdd
//这组数据可以校准并存储在EEPROM中
浮动传感器=(vdd/1024)/0.5000;
浮动传感器x=传感器//如果没有校准,默认值来自数据表
浮动敏感度=敏感度;
浮动传感器z=传感器;
浮动中间=1023/2;
浮动中间x=中间;
浮动中间Y=中间;
浮动中间z=中间;
无符号长时间,preTime=0,curTime//dT需要整合
浮动dT=0,aT=0;
int noAvg=10//10次读数的平均值
int dispCnt=1,actCnt=0//显示1/10的数据,否则人类无法读取。actCnt必须从dispCnt偏移
int键响应;
结构计算数据
{int writed;//99如果已写入
浮动mx,my,mz,sx,sy,sz;
} ;
calData_t calData;
结构gVector\t{
浮动gx,gy,gz;
};
gVector_t gV,gVNorm;
浮动读数ACC(浮动和数值X、浮动和数值Y、浮动和数值Z、字符一、整数noAvg);
无效calOne(字符一、浮动和中间、浮动和传感器);
无效LPF(浮动和d、浮动和dAvg、浮动和DIN)//LPF 1数据
最大无效值(浮动和最大值、浮动和d);
无效重置水平(浮动和a、浮动低B、浮动和v);
int readkey(){
//从串行监视器读取1个字符
INTC;
而(!Serial.available());
c=Serial.read();//读取1字节仅读取1字节
延迟(100);
而(Serial.available()>0){
Serial.read();//保持读取。清除缓冲区,直到Serial.available()==0
}
返回c;
}
无效校准(){
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};
虽然(!eeprom_is_ready());//等待eeprom准备就绪
cli();
eeprom_写入_块((常数void*)和calData,(void*)0,sizeof(calData));
Serial.println(“写入EEPROM的校准数据…”);
sei();
}
void dispCalData(){
Serial.println(“当前中间值和灵敏度为:”);
连载打印(“sensX:”);
系列印刷品(sensX,5);
Serial.print(“\tsensY:”);
系列印刷品(sensY,5);
Serial.print(“\tsensZ”);
系列印刷品(sensZ,5);
Serial.print(“\t不带cal的字符:”);
序列号:println(sens,5);
Serial.print(“midX:”);
串行打印(midX,5);
Serial.print(“\tmidY:”);
连续打印(中期,5);
串行打印(“\tmidZ”);
连续打印(midZ,5);
Serial.print(“\tmid不带cal:”);
序列号:println(mid,5);
延迟(5000);
}
无效计算一(字符一、浮动和中间、浮动和传感器){
//当你校准z=+1g时,你可以校准x=y=0g
//这只需要执行6校准而不是9校准
//但是,如果存在cal错误,代码中会更加混乱,很难跟踪。
浮点数Pos1g=0,Neg1g=0;
String String1=String(One)+“=0g校准:放置“+String(One)+”=0g并点击“y”并返回”;
String2=String(One)+“=+1g校准:放置+”+String(One)+“朝上并点击‘y’&返回”;
String String3=String(One)+“=-1g校准:放置+”+String(One)+“向下并点击‘y’&返回”;
序列号println(String1);
keyResponse=readkey();
if(keyResponse=='y'){
延迟(1000);
mid=readAcc(ValueX、ValueY、ValueZ、One、100);
}
序列号println(String2);
keyResponse=readkey();
if(keyResponse=='y'){
延迟(1000);
Neg1g=readAcc(ValueX,ValueY,ValueZ,One,100);
}
序列号println(String3);
keyResponse=readkey();
if(keyResponse=='y'){
延迟(1000);
Pos1g=readAcc(ValueX,ValueY,ValueZ,一,100);
}
如果(Pos1g==0 | | Neg1g==0){
else{sens=2/(Pos1g-Neg1g);}
}
浮点读ACC(浮点和数值X、浮点和数值Y、浮点和数值Z、字符一、整数noAvg){
//通过平均值读取acc数据(来自ADC)。通过指针返回值。
//参数1允许您返回1轴acc读数。
浮动X=0;浮动Y=0;浮动Z=0;
对于(int i=0;i#define MAX_VAL 10
int arrvalues[MAX_VAL] = {0,10,20,30,40,50,60,70,80,90};
int i;
for ( i = 0; i < MAX_VAL ; ++i )
EEPROM.write ( i, arrvalues[ i ] );
int i=0;
while(i<MAX_VAL){
EEPROM.write ( i, arrvalues[ i ] );
i++;
}