Hello johnwasser thanks a lot for your reply.
This is what i have so far , the first code works quite accurate apart from a repeting error every certain time.
It gives one or 2 values wrong and then keeps with the correct ones , i copy pieces of the outputs values so you see:
55.8984375000
53.7890625000 --wrong
13.7109375000 --wrong
55.8984375000
55.8984375000
55.8984375000
55.8984375000
46.4062500000 --wrong
13.7109375000 --wrong
55.8984375000
55.8984375000
also it seems in this code i,m not benefit from the 13,25 bits of the encoder
#define CLOCK_PIN 5
#define DATA_PIN 6
void setup()
{
//setup our pins
pinMode(DATA_PIN, INPUT);
pinMode(CLOCK_PIN, OUTPUT);
//give some default values
digitalWrite(CLOCK_PIN, HIGH);
Serial.begin(115200);
}
//variables to keep track of position
int reading = 0;
float angle = 0;
void loop()
{
reading = readPosition();
if (reading >= 0)
{ // Now it,s got a resolution of 1024
angle = ((float)reading/1024) * 360.0;
Serial.println(angle,DEC);
//Serial.println(reading,DEC);
}
delay(200);
}
//read the current angular position
int readPosition()
{
unsigned int position = 0;
//shift in our data
delayMicroseconds(1);
byte d1 = shiftIn(DATA_PIN, CLOCK_PIN);
byte d2 = shiftIn(DATA_PIN, CLOCK_PIN);
//get our position variable
position = d1;
position = position << 8;
position |= d2;
position = position >> 6; // by lowering this value i seem to be getting more resolution not sure if true or fake.
return position;
}
//read in a byte of data from the digital input of the board.
byte shiftIn(byte data_pin, byte clock_pin)
{
byte data = 0.0;
for (int i=7; i>=0; i--)
{
digitalWrite(clock_pin, LOW);
delayMicroseconds(1);
digitalWrite(clock_pin, HIGH);
delayMicroseconds(1);
byte bit = digitalRead(data_pin);
data |= (bit << i);
}
return data;
}
when using your code which looks more interesting for benefit from 13 and 25 bits i get this output which seem to be doing something but causing sort of random amount of values :
Samples dont match: sample1=1294415, sample2=5185852
Reading: 4294950991
Samples dont match: sample1=5874022, sample2=5873942
Reading: 4294943078
Reading: 388
Reading: 4294934950
Samples dont match: sample1=7643602, sample2=7661891
Reading: 4294943186
Reading: 16905
Samples dont match: sample1=9413181, sample2=9421375
Reading: 4294943293
Samples dont match: sample1=9038375, sample2=10396282
Reading: 4294961703
Samples dont match: sample1=11338420, sample2=11338326
Reading: 692
Samples dont match: sample1=12395252, sample2=12394874
Reading: 8948
i played a bit with your code and i seem to be getting sort of reasonable data apart from some error similar to the one of the other code i described above, some other error also a little flickering when no move .and also that it is not multiturn data single turn so far:
values go from 0-4096 for full revolution 360º.
Reading: 275
Reading: 264
Reading: 275
Reading: 260
Reading: 275
Reading: 260
Reading: 275
Reading: 260
Reading: 275
Reading: 260
Reading: 275
Here is the your modify code :
const int CLOCK_PIN = 5;
const int DATA_PIN = 6;
const int BIT_COUNT = 25;
void setup() {
//setup our pins
pinMode(DATA_PIN, INPUT);
pinMode(CLOCK_PIN, OUTPUT);
//give some default values
digitalWrite(CLOCK_PIN, HIGH);
Serial.begin(115200);
}
void loop() {
unsigned long reading = readPosition();
Serial.print("Reading: ");
Serial.println(reading, DEC);
delay(200);
}
//read the current angular position
int readPosition() {
// Read the same position data twice to check for errors
unsigned long sample1 = shiftIn(DATA_PIN, CLOCK_PIN, BIT_COUNT);
unsigned long sample2 = shiftIn(DATA_PIN, CLOCK_PIN, BIT_COUNT);
unsigned long sample3;
delayMicroseconds(25); // Clock mus be high for 20 microseconds before a new sample can be taken
// if (sample1 != sample2) {
// Serial.print("Samples dont match: sample1=");
// Serial.print(sample1);
// Serial.print(", sample2=");
// Serial.println(sample2);
// }
sample3 = sample1;
//sample3 = sample3 << 25;
//sample3 = sample3 << 5;
//sample3 |= sample2;
//sample3 = (sample3 >> 12) & 0x0FFF;
sample3 = sample3 >> 13; // this is giving and output from 0-4095 which is correct apart from the random erros
//but singleTurn only though not to sure how to split get the multiturn data i presume is the rest of the message ?
return sample3 ;
}
//read in a byte of data from the digital input of the board.
unsigned long shiftIn(const int data_pin, const int clock_pin, const int bit_count) {
unsigned long data = 0;
for (int i=0; i<bit_count; i++) {
data <<= 1;
digitalWrite(clock_pin,LOW);
delayMicroseconds(1);
digitalWrite(clock_pin,HIGH);
delayMicroseconds(1);
data |= digitalRead(data_pin);
}
return data;
}
basicly what i am missing is a clean output without error and the multiturn data .
let me know if any ideas or solutions.
thanks again .