Increment NOT Working

HI there,

I am currently working on a project having a encoder and I want to transmit data from sensors only when the encoder is in a particular position.

Encoder is programmed so that it will result from (0) to (29) while moved clockwise and (0) to (-29) when it is moved anti- clockwise.

But somehow, the output is only showing in anti - clockwise and no output is shown when encoder is turned clockwise. My assumption is that the increment function is not working correctly. HOW CAN THIS ERROR BE SOLVED ???

Thanks in advance

CODE IS BELOW -

#include <RH_ASK.h>
#include <SPI.h> 
#include <Wire.h>
#include <MechaQMC5883.h>

#define outputA 6
#define outputB 7
 
int counter;
int aState;
int aLastState; 

MechaQMC5883 qmc;
float heading;
String str_head;

RH_ASK rf_driver(2000, 6, 3); 

 
void setup() {
  Wire.begin();
  Serial.begin(9600);
  qmc.init(); 
  rf_driver.init();

  pinMode (outputA,INPUT);
  pinMode (outputB,INPUT);
  aLastState = digitalRead(outputA);
}
 
void loop()
{
   if(counter == 29  || counter == -29){
    counter = 0;
   }
   aState = digitalRead(outputA);

   if (aState != aLastState){     
     
     if (digitalRead(outputB) != aState) { 
       counter ++;
     } else {
       counter --;
     }
     Serial.print("Position: ");
     Serial.println(counter);
   } 
   aLastState = aState;
 
  
  
  
  if( counter == 1 || counter == 0 || counter == -1){
  
  int x, y, z;
  float azimuth;
  qmc.read(&x, &y, &z,&azimuth);

  heading = azimuth;
  Serial.println(heading);
  
  str_head = String(heading);
    
  static char *msg = str_head.c_str();
    
  rf_driver.send((uint8_t *)msg, strlen(msg));
  rf_driver.waitPacketSent();
  
  }
  
}

check_pot.ino (1.11 KB)

Do you have any pullup or pulldown resistors on the inputs to keep them at a known voltage at all times ?

My assumption is that the increment function is not working correctly.

That assumption is incorrect. You can’t blame your errors on a function not working, although many beginners try to.

If you posted your code correctly here I could help more but mobile devices can’t cope with .ino files even though they are basically text files.

Add some Serial.print statements so you can monitor the state of the inputs and state variables involved in the increment/decrement part of the sketch.

Grumpy_Mike:
That assumption is incorrect. You can’t blame your errors on a function not working, although many beginners try to.

If you posted your code correctly here I could help more but mobile devices can’t cope with .ino files even though they are basically text files.

Grumpy_Mike:
That assumption is incorrect. You can’t blame your errors on a function not working, although many beginners try to.

If you posted your code correctly here I could help more but mobile devices can’t cope with .ino files even though they are basically text files.

Sorry for the inconvenience

CODE IS BELOW

#include <RH_ASK.h>
#include <SPI.h>
#include <Wire.h>
#include <MechaQMC5883.h>

#define outputA 6
#define outputB 7

int counter;
int aState;
int aLastState;

MechaQMC5883 qmc;
float heading;
String str_head;

RH_ASK rf_driver(2000, 6, 3);

void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
rf_driver.init();

pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
aLastState = digitalRead(outputA);
}

void loop()
{
if(counter == 29 || counter == -29){
counter = 0;
}
aState = digitalRead(outputA);

if (aState != aLastState){

if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
Serial.print("Position: ");
Serial.println(counter);
}
aLastState = aState;

if( counter == 1 || counter == 0 || counter == -1){

int x, y, z;
float azimuth;
qmc.read(&x, &y, &z,&azimuth);

heading = azimuth;
Serial.println(heading);

str_head = String(heading);

static char *msg = str_head.c_str();

rf_driver.send((uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();

}

}

and here is the code posted correctly

#include <RH_ASK.h>
#include <SPI.h>
#include <Wire.h>
#include <MechaQMC5883.h>

#define outputA 6
#define outputB 7

int counter;
int aState;
int aLastState;

MechaQMC5883 qmc;
float heading;
String str_head;

RH_ASK rf_driver(2000, 6, 3);


void setup()
{
  Wire.begin();
  Serial.begin(9600);
  qmc.init();
  rf_driver.init();
  pinMode (outputA, INPUT);
  pinMode (outputB, INPUT);
  aLastState = digitalRead(outputA);
}

void loop()
{
  if (counter == 29  || counter == -29)
  {
    counter = 0;
  }
  aState = digitalRead(outputA);
  if (aState != aLastState)
  {
    if (digitalRead(outputB) != aState)
    {
      counter ++;
    }
    else
    {
      counter --;
    }
    Serial.print("Position: ");
    Serial.println(counter);
  }
  aLastState = aState;
  if ( counter == 1 || counter == 0 || counter == -1)
  {
    int x, y, z;
    float azimuth;
    qmc.read(&x, &y, &z, &azimuth);
    heading = azimuth;
    Serial.println(heading);
    str_head = String(heading);
    static char *msg = str_head.c_str();
    rf_driver.send((uint8_t *)msg, strlen(msg));
    rf_driver.waitPacketSent();
  }
}

UKHeliBob:
Do you have any pullup or pulldown resistors on the inputs to keep them at a known voltage at all times ?

currently NO

GypsumFantastic:
Add some Serial.print statements so you can monitor the state of the inputs and state variables involved in the increment/decrement part of the sketch.

But there is no reference data that can be monitored to check the correct output. Have a look at the code. Thanks

Kaizen_Inventor:
But there is no reference data that can be monitored to check the correct output. Have a look at the code. Thanks

aState = digitalRead(outputA);

  Serial.println("Outer if condition");
  Serial.print("aState = ");
  Serial.println(aState);
  Serial.print("aLastState = ");
  Serial.println(aLastState);

  if (aState != aLastState)
  {
   bStateDebug=digitalRead(outputB);

   Serial.println("Inner if condition");
   Serial.print("outputB = ");
   Serial.println(bStateDebug);
   Serial.print("aState = ");
   Serial.println(aState);
 

    if (bStateDebug != aState)
    {
      counter ++;
    }
    else
    {
      counter --;
    }
   }

You're welcome.

Kaizen_Inventor:
currently NO

Then put some on or enable the internal pull up resistors.

Kaizen_Inventor:
currently NO

Which means that it is a waste of time testing the state of the input pins as when not explicitlu=y connected to 5V or GND thay will be at an unknown voltage.

The easiest way to deal with this is to use INPUT_PULLUP as the parameter in pinMode() for the inputs rather than INPUT. This enables the internal pullup resistors for the pins. You will also need to change the input wiring so that the pins are taken LOW when activated and change the logic of the program to suit.