收集数据并将数据存储到EEPROM

时间:2014-07-30 09:32:13

标签: c arduino

我已经在串口监视器上显示输出,但我想将输出保存到EEPROM。如何循环10次并收集10个数据并将10个数据存储到EEPROM?据我所知,我需要创建一个计数器,最多可以计算10.我需要为变量使用数组吗?还有一个while循环我是对的吗?

编辑:我指的是ARDUINO

这是代码。我正在使用旧的加速度计代码。正如你所见,代码存储在前一个X,Y,Z轴上的一个g-Level。但现在我想存储速度和距离的值。使用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;
}

1 个答案:

答案 0 :(得分:0)

我假设你想要这样的东西

#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...loop

int i=0;
while(i<MAX_VAL){
   EEPROM.write ( i, arrvalues[ i ] );
   i++;
  }