EEPROM.write number then EEPROM.put into a formula

Hello

I am Having difficulty using the EEPROM commands for my project.

i have a accelerometor that is hooked up to my Arduino uno r3 the accelerometer is an adafruit MMA8541.

the code right now is simplified just for the z axis but once it works will be applied to x and y.

the code is meant to read acceleration in z. ( its on the table now so its about 9.81m/s^2 (1g)

then when i push a button it EEPROM.writes the current acceleration and remembered that number i.e 9.81 called valz ( in code this is event.acceleration)

then a for loop for 50 times that using the EEMPROM.put command will subtract the valz from any event.acceleration.z in the for loop.

when i move the accelerometer it doesn't do the subtraction i want it just makes new z = 0

right now i think the Float valz = event,acceloration.x is setting them equal to each other for the rest of the code when i just want valz to rememeber at button press. i tried == instead f = but this didn't work any help would be great than you

code and serial monitor screenshot below
Thank you

#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
Adafruit_MMA8451 mma = Adafruit_MMA8451();

#include <EEPROM.h>  
int addr = 0;  

const int buttonPin = 2; 
int buttonState = 0;   
      
int measurementCount =1;



void setup(void) {
  Serial.begin(9600);
   pinMode(buttonPin, INPUT_PULLUP);
   
     
  Serial.println("Adafruit MMA8451 test!");
  if (! mma.begin()) {
    Serial.println("Couldnt start");
    while (1);
  }
  Serial.println("MMA8451 found!");
  }



void loop() {
  /* Get a new sensor event */ 
  sensors_event_t event; 
  mma.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
 
  Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");
  Serial.println("m/s^2 ");
  
    buttonState = digitalRead(buttonPin);
    Serial.println(buttonState); 
    if(buttonState == LOW){      

     float valz = event.acceleration.z;
      EEPROM.write(addr, valz);
      Serial.print("valz");
      Serial.print(valz);
      Serial.println();


     
     for(measurementCount; measurementCount<100; measurementCount++) {

     float NewZ;    //NEW Z 
     NewZ == event.acceleration.z - EEPROM.get(addr, valz);
     Serial.print("NewZ    ");
     Serial.print(NewZ);
     Serial.println();

  
      Serial.print("measurementcout");
     Serial.println(measurementCount);
      }

    }
  delay(500);
  
}

Your topic was MOVED to its current forum category as it is more suitable than the original

Don’t use write, use put() (and get() to read it back)

1 Like

Why not this:

 for(measurementCount = 0; measurementCount<100; measurementCount++) {`
1 Like

yea that would be better

Unfortunately this doesn't work i think it might be the float thats causing trouble but i dont know

What exactly is "this" ?

Please post your code as previously requested

Apologies
changing the EEPROM.write(addr, valz); to emprom.put (addr, valz);

 EEPROM.put(addr, valz);

i think this line below is setting the rest of the acceloraton in z to the float valz.

 float valz = event.acceleration.z;

Try and use serial monitor plus Serial.println for strategic variables like EEPROM address, values for and after valz calculations etc.

It looks like you are only reading the sensor once, near the beginning of loop.

Why are you using EEPROM for this???

1 Like

im open to suggestions on better ways to do this but i was using eeprom cause i want to it to remember the first acceleration in z when the button is pressed so i can subtract it from all other accelerations in z for the rest of the for loop

valz will still retain the value in the subsequent for loop, no need for the eeprom.

Without looking at the library, my assumption is you need to call mma,getEvent within the for loop in order to get a new reading.

Thank you this did work, your a life saver

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.