I've found some code to read a simple quadrature encoder and it works . . . yay!
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
int encoder0PinA = 2;
int encoder0PinB = 3;
void setup() {
pinMode (encoder0PinA, INPUT);
pinMode (encoder0PinB, INPUT);
Serial.begin (9600);
}
void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--;
} else {
encoder0Pos++;
}
Serial.print (encoder0Pos);
Serial.print ("/");
}
encoder0PinALast = n;
}
What I'm trying to do is make a function to read to the encoder that I can call later in another piece of code that I've written but it's not working. Here's my attempt:
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
int encoder0PinA = 2;
int encoder0PinB = 3;
void setup()
{
pinMode (encoder0PinA, INPUT);
pinMode (encoder0PinB, INPUT);
Serial.begin (9600);
}
void loop()
{
Serial.println(spd());
}
int spd()
{
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH))
{
if (digitalRead(encoder0PinB) == LOW)
{
encoder0Pos--;
}
else
{
encoder0Pos++;
}
return encoder0Pos;
}
encoder0PinALast = n;
}
Can someone explain how you actually go about creating a function (how it works) and where I'm going wrong
What does not work? (And look at where the return is versus updating encoder0PinALast and ask yourself what is returned if the first condition is false )
J-M-L:
What does not work? (And look at where the return is versus updating encoder0PinALast and ask yourself what is returned if the first condition is false )
It just produces a series of 1's and the odd -1 and doesn't count up or down
blh64:
As it turns out, there is an "Encoder" library that does just that and is installable by the library manager
I don't want to import the whole library into my code as I just need a simple read function
the question was just rhetorical... the true point was in "look at where the return is versus updating encoder0PinALast and also ask yourself what is returned if the first condition is false "
jimmer:
If you tell us the ppr and rpm, and the highest and lowest values you want to count to, I might have something suitable.
it's 20ppr and I'll just be turning it by hand from 0-60 counts. If you have something that would be great.
I've looked into interrupts and if I'm totally honest don't understand how to implement them also how do I create a global value as I've run into this issue before?
Beginners should avoid writing code with interrupts, unless you are willing to buckle down for a lengthy study session.
The use of interrupts introduces many more problems than it solves, and beginners usually end up back on the forum, asking what went wrong. The best approach is to use tried and true library code and get on with your project.
Danzxz:
What I'm trying to do is make a function to read to the encoder that I can call later in another piece of code that I've written but it's not working. Here's my attempt:
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
int encoder0PinA = 2;
int encoder0PinB = 3;
int spd()
{
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH))
{
if (digitalRead(encoder0PinB) == LOW)
{
encoder0Pos--;
}
else
{
encoder0Pos++;
}
return encoder0Pos; //<---- move this line ...
}
encoder0PinALast = n;
// .... to here
}
Can someone explain how you actually go about creating a function (how it works) and where I'm going wrong
Thanks
You should return the value right at the end of the function - return returns immediately, so the vital setting of encoder0PinALast is lost with your version