Saving last value in encoder

Hi! I'm new here writing my first post, so sorry if anything seems messy in my explanation.

I'm writing a code for a relative encoder. Right now I have a Stepper with a mounted wheel which contains my encoder with 20 interruptions. I'm using a CNY70 with an analog connection to sense the values and those are then mapped to a certain range. I'm having trouble saving the last value sensed, since when I want to print the values it prints in a constant string and I just want to print the value whenever the CNY70 Senses a change of colour in the encoder (black or white).

My code here

#include <AccelStepper.h>
#define minimoDelay 200

AccelStepper stepper(1,8,9);

int lectura = 0; //CNY Reading
int CNY70 = A0; //CNY Connecting port
int estadoViejo = 0; //old value
int n=0; // a counter i'm not using
unsigned long primerTiempo = 0; //millis I might use later


void setup() {
   
   Serial.begin(9600);
   stepper.setMaxSpeed(300.0);
   stepper.setSpeed(50); // setting the stepper speed
 
  


}

void loop() {
 
  stepper.runSpeed();
  
  lectura = analogRead(CNY70);
  int estadoActual; //declaring the new state of the CNY
   

  if (lectura < 900) { 
  estadoActual = 1;}
  
  else {  
  estadoActual = 0;} //There range i'm using and giving each interruption a value either 0 or 1

  
  if (estadoActual != estadoViejo)

  {
  Serial.println(estadoActual);

  }

  estadoActual = estadoViejo; //this is were I'm having trouble, since the value keeps printing 1s and not changing even though I'm saving the last value.
}

Well it is very difficult to understand and the code is even stranger because it appears to make no attempt at all to count the number of impulses coming of your opto reflective switch.

How fast is this motor running? Your code as it stands uses an analog read and those take 0.1mS complete.

You need to count the number of changes in your optical switch not the levels to get an idea of movement. Also Serial print is very slow reducing the maximum motor speed you will be able to cope with once you have sorted out counting the changes.

Hi augustinsebastian,

welcome to the arduino-user-forum. Well done posting your code as a code-section.

Did you do some basic tests about the voltage-change by slowly moving the optcial index-wheel by hand?

Can you please post a hand-drawn schematic how you wired the CNY70?
don't post a fritzing-picture. Fritzing-pitures are far afway from the standard how schematics are drawn. A hand-drawn schematic is most liked in this forum. It is:

  • fast to create
  • reduced to the important information

Is the distance between CNY70 and your black/white index-wheel within a range of 4 mm ?

In what mode (fullstep, halfstep, microstep) is your stepper-motor running?

If your stepper-motor has 200 steps per revolution
at should run at 50 steps /sec and your index-wheel has 20 sections

if the optical coditions are good enough to make the fototransistor switch between beeing (highly conductive and "opened" this gives a frequency of
200 rot.per rev. / 50 rot./sec * 20 index/rev.= 80 Hz so you should see a frequency of 80 Hz

The microcontroller-world is not super-standardised like USB-devices. You have to take care of more things than just "does the plug fit into the socket?"

So seriously: how does your wiring look like?
It is not sufficient to simply connect the fototransistor to +5V and an analog-Input-pin

best regards Stefan

encoders normally have 2 outputs allowing them to measure both a change in position as well as direction.

according to the CNY70 datasheet it is a reflective opto sensor. when connected to an digital input with internal pullup enabled (INPUT_PULLUP) there should be a binary input value. not need for an analogRead().

not exactly sure how you intend to use such a device in your application. encoders may be used to confirm the movement of a stepper. at startup, stepper would be move in one direction until it reaches a zero sensor to determine an initial position. is that what you're using the CNY70 for?

For a relative encoder, you may want an out of phase pattern on the other side of the disk and a second sensor. If you are just measuring speed or rotations in a single direction what have could be OK.