pulses counting with encoder

Friends

I have a simple encoder and need a simple code as I am a newebie.

So i use the common wire (the midle) connected to 5v and 1 of the other wires to AIO, so rotating the encoder (very slow) I will have pulses between almost 0 and 1023 bytes.
The code below almost works, when I have 1023 it counts a pulse, but if i stop in 1023 it continoues counting....

Sorry for the simple question for you but I waste a lot changging the conditions and nothing...

I will attach this simple encoder in a very slow motor shaft (60rpm/mint) and i need to count pulses each rotation, but it motor stops, pulses must stop couting too

float val = 0;
float old_val = 0;
int state = 0;
int Eel_Count = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
val = analogRead(A0);
//val = val * 0.00488;

//if ((val = 1023) && (old_val = 1023)){//2.40volts=490bits
//state = 0 - state;
//}
if ((val = 1023) && (old_val < 1000)){//2.40volts=490bits
 state = 1 - state;
}
old_val = val;
if (state == 1) {
 Eel_Count = 1 + Eel_Count;
 Serial.print("Pulse");
 Serial.println(Eel_Count);
 Serial.println(val);


}
delay(1000);
}

cpalha:
Sorry for the simple question for you but I waste a lot changging the conditions and nothing...

Your code looks very weird and most likely you did NOT WRITE what you wanted.

Like that, this is an assigment:

if ((val = 1023) && (old_val < 1000)){//2.40volts=490bits

By writing "val = 1023" you assign 1023 to the variable with the name 'val'.

Possibly you want to test an if condition with the "==" comparison operator (instead doing an assigment with the "=" operator:
if ((val == 1023) && (old_val < 1000)){//2.40volts=490bits

Thanks jurs

Still not work

Can you help doing a more simple code, using 2 digital ports 1 put one in HIGH and conect a wiring from that passing trouth the encoder and goes to another digital port.
This digital port when detects a HIGH signsl counts a pulse or like the code below with AI, but stills not works. Never stops counting even with encoder stoped in 1023

float val = 0;
float old_val = 0;
int state = 0;
int Eel_Count = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
val = analogRead(A0);
//val = val * 0.00488;

if ((val == 1023) && (old_val == 1023)){
state = state;
}
if ((val == 1023) && (old_val < 1000)){
 state = 1 - state;
}
old_val = val;
if (state == 1) {
 Eel_Count = 1 + Eel_Count;
 Serial.print("Pulse");
 Serial.println(Eel_Count);
 Serial.println(val);


}
delay(500);
}

This might help.

Hi jimboza

I know this code and for a simple ON/FF between 2 pins and counting, this code it too much complete a some confused. Even then I will add a load cell reading (but this code I could make).

Best
cp

but if i stop in 1023 it continoues counting..

You are testing for the state of a pin, and you need to be testing for the change of state.

You are correct, that this may be easier to do with digitalRead() rather than analogRead().

The example "State Change Detection" for reading the state change of a digital pin is in the IDE at Examples>02Digital>StateChangeDetection.

Do you understand that reading the encoder in the manner you imply will not detect direction of rotation.

If you are satisfied with that, then reading one pin of the encoder for a rising change will give you a count.

Hi cattledog

almost it, but if i stay with (button pressed) or encoder stoppedd in the same HIGH it counts evenents.
A need a miracle condition that says if oldval=val no count

Mine almost works.....but....

float val = 0;
float old_val = 0;
int state = 0;
int Eel_Count = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
val = analogRead(A0);
//val = val * 0.00488;

if ((val == 1023) && (old_val == 1023)){
state = 0 - state;    //state=0 no change in encoder
}
if ((val == 1023) && (old_val < 1000)){
 state = 1 - state;  //state=1 change in pulse encoder
}
old_val = val;
if (state == 0) {                     //this conditions does not work 
 Eel_Count = 0 + Eel_Count;   //state=0 pulse must not change but changes
 if (state == 1) {
 Eel_Count = 1 + Eel_Count;   //only here pulses must increase
 Serial.print("Pulse");
 Serial.println(Eel_Count);
 Serial.println(val);


}
delay(500);
}}

works now :slight_smile:

float val = 0;//channel A, the common comes from 5v
float val1 = 0;//reading chanel B drops down voltage
float old_val = 0;
int state = 0;
int Eel_Count = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
val = analogRead(A0);
val1 = analogRead(A1);
//Serial.println (val);
//val = val * 0.00488;

{if ((val == 1023) && (old_val == 1023))
state = 0 - state;    //state=0 no change in encoder
}
{
if ((val == 1023) && (old_val < 1000))
 state = 1 - state;  //state=1 change in pulse encoder
}
old_val = val;
{   if (state == 0)                   //this conditions does not work 
 Eel_Count = 0 + Eel_Count; }  //state=0 pulse must not change but changes
 {if (state == 1) 
 Eel_Count = 1 + Eel_Count;   
 Serial.print("Pulse");
 Serial.println(Eel_Count);
 //Serial.println(val);
state=0;

}
delay(100);
}

Hi people

Can I make in my programm 2 loops one for counting the events (needs to be fast) delay(100) and other the serialprint the counts with a delay (1000) and I will seend data to excel by plxdaq and I need pulses each secound (I know that I will have pulses, 1, 22, 34, 47, 78, 89....no matters)

Best regards
cpalha

(needs to be fast) delay(100)

Get real. Fast and delay do not belong in the same sentence.

You need to find Robin2's post on doing multiple things "at once".

PaulS:
Fast and delay do not belong in the same sentence.

That's a good example of where they do