I am relatively new to Arduino, and am wondering if someone could help me store rotary encoder data so that the angular position is always initialized correctly. I am using it for a robot I am building for a course in mechanical engineering, and need it to confirm crank angular displacement. I did see some examples, but they are over my head. As shown in the pic, I originally tried using a prox switch to initialize the encoder count but found some examples using "EEPROM" that I am unable to figure out at the moment.
My code below:
(the value I'm trying to store is "encoderPosCount")
//prox
const int proxPin=11;
//update prox value every 10 ms
const unsigned long eventTimeProx=5;
unsigned long previousTime1=0;
int proxState=0;
int intitialProxPresses=0;
int totalProxPresses=0;
int PreviousProxState=0;
//encoder
int pinA = A5; // Connected to CLK on KY-040
int pinB = A4; // Connected to DT on KY-040
int encoderPosCount = 0;
int pinALast;
int aVal;
boolean bCW;
// dc motor pins
const int enable1=10;
const int in2=7;
const int ref_high=4;
const int in1=8;
// servo pins
const int ref_high2=12;
const int in1_a=6;
const int in2_a=9;
const int enable1_a=5;
const int in1_b=13;
const int in2_b=2;
const int enable1_b=3;
//SOLENOID PINS
//power
const int sol_actuate=A2;
void walkStraight() {
digitalWrite(ref_high, HIGH);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enable1, 120);
}
void stopWalking() {
digitalWrite(ref_high, HIGH);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enable1, 0);
}
void solenoidsDown() {
digitalWrite(sol_actuate,LOW);
}
void solenoidsUp() {
digitalWrite(sol_actuate,HIGH);
}
void turnRight() {
analogWrite(enable1_a,0);
digitalWrite(in1_a,LOW);
digitalWrite(in2_a,HIGH);
}
void turnLeft() {
analogWrite(enable1_a,255);
digitalWrite(in1_a,HIGH);
digitalWrite(in2_a,LOW);
}
void setup() {
// put your setup code here, to run once:
//SOLENOIDS
pinMode(sol_actuate,OUTPUT);
//Solenoid relay power supply
digitalWrite(sol_actuate,HIGH);
//prox
pinMode(proxPin,INPUT);
Serial.begin (9600);
//encoder
pinMode (pinA,INPUT);
pinMode (pinB,INPUT);
pinALast = digitalRead(pinA);
//dc motor
pinMode(enable1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(ref_high, OUTPUT);
pinMode(in1, OUTPUT);
//servo
pinMode(ref_high2, OUTPUT);
pinMode(in1_a,OUTPUT);
pinMode(in2_a,OUTPUT);
pinMode(enable1_a,OUTPUT);
pinMode(in1_b,OUTPUT);
pinMode(in2_b,OUTPUT);
pinMode(enable1_b,OUTPUT);
//servo power supply
digitalWrite(in1_b,LOW);
digitalWrite(in2_b,HIGH);
analogWrite(enable1_b,255);
digitalWrite(ref_high2, HIGH);
// initial servo state (on startup)
delay(2000);
}
bool bool1=0;
bool bool2=0;
int triggerTime1=0;
int triggerTime2=0;
unsigned long msecLast;
enum { Idle, Stop, Straight, TurnRight,TurnLeft,walkAgain };
int state = Straight;
void loop() {
unsigned long currentTime=millis();
if(currentTime-previousTime1>=eventTimeProx) {
proxState=digitalRead(proxPin);
previousTime1=currentTime;
if (proxState != PreviousProxState) {
if (proxState==HIGH) {
totalProxPresses++;
Serial.println(totalProxPresses);
} PreviousProxState=proxState;
}
}
// start encoder count when prox switch trips "totalProxPresses" number of times
if (totalProxPresses>=10) {
//ENCODER READINGS
aVal = digitalRead(pinA);
if (aVal != pinALast){ // Means the knob is rotating
// if the knob is rotating, we need to determine direction
// We do that by reading pin B.
if (digitalRead(pinB) != aVal) { // Means pin A Changed first - We're Rotating Clockwise
encoderPosCount ++;
bCW = true;
} else {// Otherwise B changed first and we're moving CCW
bCW = false;
encoderPosCount--;
}
Serial.print ("Rotated: ");
if (bCW){
Serial.println ("clockwise");
}else{
Serial.println("counterclockwise");
}
Serial.print("Encoder Position: ");
Serial.println(encoderPosCount);
}
pinALast = aVal;
}
if ((encoderPosCount<=15) && (bool1==0)){
walkStraight();
}
if ((encoderPosCount>=15)&&(bool1==0)) {
bool1=1;
stopWalking();
triggerTime1=currentTime;
}
if ((currentTime-triggerTime1>2000)&&(bool1==1)){
solenoidsDown();
bool2=1;
}
if ((currentTime-triggerTime1>2500)&&(bool1==1)){
turnLeft();
}
if ((currentTime-triggerTime1>5000)&&(bool2==1)){
solenoidsUp();
}
if ((currentTime-triggerTime1>5500)&&(bool2==1)){
walkStraight();
}
if (totalProxPresses>20){
stopWalking();
}
}