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);
}
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
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);
}
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).
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);
}}
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);
}
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)