Hey guys,
can somebody help me with this problem? Attached i have a photo
of the Display skipping to 0 and 5000 all the time.

#define B_PHASE 2
#define INDUCTIVE_SENSOR 3
double autodepth;
double cbdwell;
double cbdepth;
boolean auto_case = false;
volatile unsigned long RPMN1count = 0;
volatile unsigned long RPMN1period = 0;
unsigned long RPMN1 = 0;
unsigned long prev_rpm = 0;
volatile double Position = 0;
double prev_pos = 0;
double RealPos = 0;
unsigned long tx_millis;
unsigned long relay_millis = 0;
unsigned long cnt = 0;
boolean is_relay_on = false;
const int RELAY_PIN = 7;
const double POSITIONAL_SHIFT = 0.1978022F;
enum ModeM {
Auto,
cbdep,
cbdw
}; ModeM Mode;
void setup() {
pinMode(A_PHASE, INPUT_PULLUP);
pinMode(B_PHASE, INPUT_PULLUP);
pinMode(RELAY_PIN, OUTPUT);
RPMN1count = micros();
attachInterrupt(1, N1, FALLING);
attachInterrupt(digitalPinToInterrupt(B_PHASE), interrupt, RISING);
tx_millis = millis();
relay_millis = millis();
Serial.begin(9600);
/*
Serial.print("baud=115200");
Serial.print("\xFF\xFF\xFF");
Serial.end();
delay(500);
Serial.begin(115200);
*/
}
void loop() {
// Position calculation
RealPos = Position * POSITIONAL_SHIFT;
if ((RealPos < 0.2) && (RealPos > -0.2)) {
RealPos = 0;
}
// RPM calculation
if ((micros() - RPMN1count) > (3 * RPMN1period)) {
RPMN1 = 0;
}
else if ( RPMN1period != 0) {
//noInterrupts();
RPMN1 = (60000000UL / RPMN1period); //Two signals per rotation.
//interrupts();
}
// Mode control
if (Mode == Auto) {
automode();
}
// Communication
check_data();
if ((millis() - tx_millis) > 100) {
if (prev_pos != RealPos) {
Serial.print("t13.txt=\"");
Serial.print(RealPos, 1);
Serial.print("\"");
Serial.print("\xFF\xFF\xFF");
Serial.print("t14.txt=\"");
Serial.print(Position);
Serial.print("\"");
Serial.print("\xFF\xFF\xFF");
prev_pos = RealPos;
}
if ( prev_rpm != RPMN1) {
Serial.print("page1.n1.val="); // Changing the value of box n1
Serial.print(RPMN1);
Serial.print("\xFF\xFF\xFF");
prev_rpm = RPMN1;
}
tx_millis = millis();
//Serial.println();
}
}
void N1() {
RPMN1period = micros() - RPMN1count;
RPMN1count = micros();
}
void interrupt() {
if (digitalRead(A_PHASE) == HIGH)
Position++;
else
Position--;
cnt++;
}
void check_data() {
if (Serial.available()) {
String data_from_display = "";
delay(30);
while (Serial.available()) {
data_from_display += char(Serial.read());
}
if (data_from_display == "ZERO") {
Position = 0;
RealPos = Position * POSITIONAL_SHIFT;
Serial.print("t13.txt=\"");
Serial.print(RealPos, 1);
Serial.print("\"");
Serial.print("\xFF\xFF\xFF");
} else if (data_from_display.charAt(0) == 'a') {
autodepth = (data_from_display.substring(1)).toDouble();
Mode = Auto;
//Serial.println(autodepth,5);
} else if (data_from_display.charAt(0) == 'b') {
cbdepth = (data_from_display.substring(1)).toDouble();
Mode = cbdep;
//Serial.println(cbdepth,5);
} else if (data_from_display.charAt(0) == 'c') {
cbdwell = (data_from_display.substring(1)).toDouble();
Mode = cbdw;
//Serial.println(cbdwell,5);
}
}
}
void automode() {
if (!is_relay_on) {
if ((autodepth == RealPos) && (RealPos != 0)) {
digitalWrite(RELAY_PIN, LOW);
is_relay_on = true;
relay_millis = millis();
}
}
if (is_relay_on) {
if ((millis() - relay_millis) > 2000) {
digitalWrite(RELAY_PIN, HIGH);
is_relay_on = false;
}
}
}