EEPROM

Hi everyone,

I am a writing code for interfacing “ROTARY ENCODER” with Arduino Uno board. In this my aim is to read a value from rotary encoder. Once the encoder starts moving and when it stop rotation that time the current value will be stored in inbuilt EEPROM.

My problem is that, once I read a value from EEPROM through address(the user will provide address via serial monitor) it will show long number instead of stored value. In this my starting address will be 0.

Below I attached a program code for this.

#include <EEPROM.h>


int encoderPos=0,A,B,N=0,TD,given,address=0,ad;
unsigned long duration1,duration2;
const int encoderPinA=A0;
const int encoderPinB=A1;
boolean encoderALast=LOW,encoderA;
void setup() {
Serial.begin(9600);
pinMode(encoderPinA,INPUT);
pinMode(encoderPinB,INPUT);
digitalWrite(encoderPinA,HIGH);
digitalWrite(encoderPinB,HIGH);
A=encoderPos;
}

void loop() {
encoderA=digitalRead(encoderPinA);
if((encoderALast==HIGH) && (encoderA==LOW)){
 if(digitalRead(encoderPinB)==LOW)
 {
   encoderPos--;
   B=encoderPos;
 }
 else{
   encoderPos++;
   B=encoderPos;
 }
 Serial.print(encoderPos);
                   Serial.print("  ");
                   Serial.print(B);
                   Serial.println();
                     duration1=pulseIn(encoderPinA,HIGH);
                     duration1=pulseIn(encoderPinA,HIGH);
                     if(duration1==duration2){
                         Serial.print("A= ");
                         Serial.print(A);
                         Serial.print(" ");
                         TD=B-A;
                         Serial.print("DIFFERENCE= ");
                         Serial.print(TD);
                         Serial.print(" ");
                        A=B;
                        Serial.print("B=");
                        Serial.print(B);
                        Serial.println();   
                        EEPROM.put(address,TD);  
                        address++;                   
                   }
      if(Serial.available()>0){
       
                 ad=Serial.parseInt();
                 EEPROM.get(ad,TD);  
                 Serial.println();
                 Serial.print("GIVEN ADDRESS=");
                 Serial.print(ad);
                 Serial.print("\t");
                 Serial.print("DATA");
                 Serial.print(TD);
                 Serial.println();
      }
     
} 
encoderALast= encoderA;
}

Moderator: Code tags added

int encoderPos=0,A,B,N=0,TD,given,address=0,ad;

Getyourspacekeyfixed. There is no excuse for jamming the declarations together without white space.

                      duration1=pulseIn(encoderPinA,HIGH);
                      duration1=pulseIn(encoderPinA,HIGH);
                      if(duration1==duration2){

Are both values supposed to be stored in duration1?

Inconsistent placement of curly braces sucks. Put EVERY { on a line BY ITSELF.

Your indenting sucks. Use Tools + Auto Format, so you can fake knowing what you are doing.

Serial.print("GIVEN ADDRESS=");
                  Serial.print(ad);
                  Serial.print("\t");
                  Serial.print("DATA");
                  Serial.print(TD);

Why is the value in TD jammed up against DATA? Spaces are cheap. Here are some for you to copy and paste where you need them.

          duration1=pulseIn(encoderPinA,HIGH);
                      duration1=pulseIn(encoderPinA,HIGH);
                      if(duration1==duration2){

You never set duration2 to anything. So yo are comparing duration1 to nothing.

Even if we make the obvious change to this code, how is the duration of some physical process going to be identical down to MICROseconds? You need to allow a tolerance of at least a few microseconds. Maybe thousands of microseconds.

@Ganesh2807
Please don't Hijack threads and/or cross post.
Your post hikacked has been removed.

Read How to use this forum.

hi surbyte,
thanks for your kind information