Calibration in EEProm

Hello,

i´m using a forcesensor and would like to execute a one-Time calibration. That calibration value should be written in the eeprom and be used as a reference every time the arduino is started.

My problem is, that the calibration is done every time i start the arduino even with the “calPin” pulled to low.

#include <EEPROM.h>
 
int calPin = 2;
int sensorPin = A2;
int ledPin = 3; //PIN 0 für Tiny

int sensorValue = 0;        // Wert der vom Sensor gelesen wird
int calValue = 0;

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);

  analogWrite(ledPin, 0); // LED aus
 
 
  if (calPin = HIGH){
   calValue = analogRead(sensorPin);  // Sensor lesen

   EEPROMWriteInt(1, calValue);
  }
  
}

void loop() {
  sensorValue = analogRead(sensorPin);  // Sensor lesen

    if (sensorValue >= EEPROMReadInt(1)){     // Wenn Voll: LED an
   analogWrite(ledPin, 70);
   }
  
    if (sensorValue <= EEPROMReadInt(1) - 50){     // Wenn Leer: LED aus
   analogWrite(ledPin, 0);
   }

   
 delay(10);  // 10 Millisekunden warten um den ADC zu Nullen
 
Serial.print("sensorValue: ");
 Serial.println(sensorValue);
 Serial.print("EEProm: ");
 Serial.println(EEPROMReadInt(1));
}


void EEPROMWriteInt(int address, int value)
{
  byte two = (value & 0xFF);
  byte one = ((value >> 8) & 0xFF);
  
  EEPROM.update(address, two);
  EEPROM.update(address + 1, one);
}
 
int EEPROMReadInt(int address)
{
  long two = EEPROM.read(address);
  long one = EEPROM.read(address + 1);
 
  return ((two << 0) & 0xFFFFFF) + ((one << 8) & 0xFFFFFFFF);
}

Should be     if (calPin == HIGH){

Auch! Thank you!

That changed something but it´s not working right. Now i get everytime the same eeprom value in my serialmonitor reading but i con not update the value pulling pin 2 HIGH.

Why is the eeprom not updating?

Thanks!

You never actually read calPin.

This is not the way to read a pin:

if (calPin == HIGH){

All that's doing is comparing the value of calPin (which is defined as 2) to HIGH (which is defined as 1). To read a pin, you need to use digitalRead(). Please study this documentation: https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/ and this tutorial: https://www.arduino.cc/en/Tutorial/DigitalReadSerial

@pert

Thank you so much!

Now it works like it should!

Thats the version that works:

#include <EEPROM.h>
 
int calPin = 2;
int sensorPin = A2;
int ledPin = 3; //PIN 0 für Tiny

int sensorValue = 0;        // Wert der vom Sensor gelesen wird
int calValue = 0;

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(calPin, INPUT);
  
  analogWrite(ledPin, 0); // LED aus
 
 int calState = digitalRead(calPin);
 
  if (calState == HIGH){
   calValue = analogRead(sensorPin);  // Sensor lesen

   EEPROMWriteInt(10, calValue);
  }
  
}

void loop() {
  sensorValue = analogRead(sensorPin);  // Sensor lesen

    if (sensorValue >= EEPROMReadInt(10)){     // Wenn Voll: LED an - Wert für großen Sensor
   analogWrite(ledPin, 70);
   }
  
    if (sensorValue <= EEPROMReadInt(10) - 50){     // Wenn Leer: LED aus - Wert für großen Sensor
   analogWrite(ledPin, 0);
   }

   
 delay(10);  // 10 Millisekunden warten um den ADC zu Nullen
 Serial.print("sensorValue: ");
 Serial.println(sensorValue);
 Serial.print("EEProm: ");
 Serial.println(EEPROMReadInt(10));
}


void EEPROMWriteInt(int address, int value)
{
  byte two = (value & 0xFF);
  byte one = ((value >> 8) & 0xFF);
  
  EEPROM.update(address, two);
  EEPROM.update(address + 1, one);
}
 
int EEPROMReadInt(int address)
{
  long two = EEPROM.read(address);
  long one = EEPROM.read(address + 1);
 
  return ((two << 0) & 0xFFFFFF) + ((one << 8) & 0xFFFFFFFF);
}