Hi Guys and Gals,
I am having a strange behavior from Arduino for a project where I need to send a rotary value with every tick. To control each tick and each direction I have used one of the standard codes posted without interrupts as the processor will be needed to do other tasks as the code progresses.
The issue is in each if statements in the code the system should send me one increment either increasing or decreasing. But maybe it is too fast so it completes the loop faster than I think therefore does the loop one more time and sends me 2 values instead of one. The incrementation of the values are correct nevertheless quantity of the value to be sent by arduino is expected to be only one.
Basically I am expecting a count starting from 0 to 360 and with each turn in one direction it should increment by one or decrement by one. Attached is the code and all help will be appreciated.
BTW: I have tried to put delays without access after each loop.
Thank you in advance
const int smallRot_PinA = 4;
const int smallRot_PinB = 5;
int smallRot_position;
int smallRot_PinA_currentState = LOW;
int smallRot_PinA_lastState = HIGH;
void update_small_encoder(){
smallRot_PinA_currentState = digitalRead(smallRot_PinA);
if((smallRot_PinA_lastState == LOW) && (smallRot_PinA_currentState == HIGH)) {
if (digitalRead(smallRot_PinB) == LOW){
smallRot_position--;
} else {
smallRot_position++;
}
while (smallRot_position >= 360) {smallRot_position -= 360;};
while (smallRot_position < 0) {smallRot_position += 360;};
Serial.print("n1sr=");
Serial.println(smallRot_position);
Serial.println("firstloop");
Serial.println(smallRot_PinA_lastState);
Serial.println(smallRot_PinA_currentState);
}
void setup() {
// pinMode(bigRot_PinA, INPUT_PULLUP);
// pinMode(bigRot_PinB, INPUT_PULLUP);
pinMode(smallRot_PinA, INPUT_PULLUP);
pinMode(smallRot_PinB, INPUT_PULLUP);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(xfrButton, INPUT_PULLUP);
Serial.begin(19200);
}
void loop() {
if(Serial.available() > 0){
serialData = Serial.readStringUntil('\n');
checkSerialData();
}
if(configDone == true){
update_small_encoder();
// update_big_encoder();
// checkButtonState();
}
}