Help with fixing error message

Hello All,

I'm trying to submit this code to my Arduino UNO and there is an error message saying

"_2_absolutes:100: error: 'long unsigned int shiftIn(int, int, int)' previously defined here

unsigned long shiftIn(const int data_pin, const int clock_pin, const int bit_count) {

^

exit status 1
redefinition of 'long unsigned int shiftIn(int, int, int)' "

Essentially the code below is two codes ( one a duplicate of the other, one for each encoder) with both able to read absolute encoders outputting gray code. I know this code works just fine separated into two codes, but I have no idea how to change the code slightly to make this error message go away.

I was wondering if someone could help me alter the code to get it to work.

const int CLOCK_PIN = 5;
const int DATA_PIN = 6;
const int BIT_COUNT = 15; // this's the percision of rotary encoder.
volatile long encoder0Pos = 0;
long newposition;
long oldposition = 0;
unsigned long newtime;
unsigned long oldtime = 0;
long vel;

const int CLOCK_PIN2 = 7;
const int DATA_PIN2 = 8;
const int BIT_COUNT2 = 15; // this's the percision of rotary encoder.
volatile long encoder0Pos2 = 0;
long newposition2;
long oldposition2 = 0;
unsigned long newtime2;
unsigned long oldtime2 = 0;
long vel2;

void setup() {
  pinMode(DATA_PIN, INPUT);
  pinMode(CLOCK_PIN, OUTPUT);

  digitalWrite(CLOCK_PIN, HIGH);

  pinMode(DATA_PIN2, INPUT);
  pinMode(CLOCK_PIN2, OUTPUT);

  digitalWrite(CLOCK_PIN2, HIGH);

  Serial.begin(115200);
}

void loop() {
  float reading = readPosition();
  Serial.println(reading, 2);
  newposition = reading;
  newtime = millis();
  vel = (newposition - oldposition) * 1000.1 / (newtime - oldtime);
  Serial.print ("speed = ");
  Serial.println (vel);
  oldposition = newposition;
  oldtime = newtime;

  float reading2 = readPosition2();
  Serial.println(reading2, 2);
  newposition2 = reading2;
  newtime2 = millis();
  vel2 = (newposition2 - oldposition2) * 1000.1 / (newtime2 - oldtime2);
  Serial.print ("speed2 = ");
  Serial.println (vel2);
  oldposition2 = newposition2;
  oldtime2 = newtime2;

  delay(250);
}


unsigned int grayToBinary322(unsigned int num2)
{
  num2 = num2 ^ (num2 >> 16);
  num2 = num2 ^ (num2 >> 8);
  num2 = num2 ^ (num2 >> 4);
  num2 = num2 ^ (num2 >> 2);
  num2 = num2 ^ (num2 >> 1);
  return num2;
}

unsigned int grayToBinary32(unsigned int num)
{
  num = num ^ (num >> 16);
  num = num ^ (num >> 8);
  num = num ^ (num >> 4);
  num = num ^ (num >> 2);
  num = num ^ (num >> 1);
  return num;
}

//read the current angular position
float readPosition() {
  unsigned long graysample = shiftIn(DATA_PIN, CLOCK_PIN, BIT_COUNT);
  delayMicroseconds(20);  // Clock must be high for 20 microseconds before a new sample can be taken

  unsigned long binarysample = grayToBinary32(graysample);

  return ((binarysample * 360UL) / 32768.0); // ouptut value from 0 to 360 with two point percision
}

float readPosition2() {
  unsigned long graysample2 = shiftIn(DATA_PIN2, CLOCK_PIN2, BIT_COUNT2);
  delayMicroseconds(20);  // Clock must be high for 20 microseconds before a new sample can be taken

  unsigned long binarysample2 = grayToBinary322(graysample2);

  return ((binarysample2 * 360UL) / 32768.0); // ouptut value from 0 to 360 with two point percision
}

//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; // shift all read data left one bit.

    //digitalWrite(clock_pin,LOW);
    PORTD &= ~(1 << 5); // clock pin goes low
    delayMicroseconds(1);
    //digitalWrite(clock_pin,HIGH);
    PORTD |= (1 << 5); // lock pin goes high
    delayMicroseconds(1);

    data |= digitalRead(data_pin); // cat the new read bit to the whole read data.
  }
  return data;
}

unsigned long shiftIn(const int data_pin2, const int clock_pin2, const int bit_count2) {
  unsigned long data2 = 0;

  for (int i = 0; i < bit_count2; i++) {
    data2 <<= 1; // shift all read data left one bit.

    //digitalWrite(clock_pin,LOW);
    PORTD &= ~(1 << 7); // clock pin goes low
    delayMicroseconds(1);
    //digitalWrite(clock_pin,HIGH);
    PORTD |= (1 << 7); // lock pin goes high
    delayMicroseconds(1);

    data2 |= digitalRead(data_pin2); // cat the new read bit to the whole read data.
  }
  return data2;
}

let me know and thank you for your time :slight_smile:

You have posted code without using code tags. This creates some obstacles for other forum members. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please read this post:

How to use this forum - please read.

However, the reason you are getting the error message, is the exact thing that the message says. You are redefining an existing function. What do you expect? If you have two functions with the same name, which one should be called?

Building on the above @Hyde, you can have functions with the same names (a.k.a. overloading) but they cannot have the same prototype:

unsigned long shiftIn(const int data_pin2, const int clock_pin2, const int bit_count2);
unsigned long shiftIn(float i, const int clock_pin2, const int bit_count2);
unsigned long shiftIn(const int data_pin2, const int clock_pin2, const int bit_count2, const int bitCount3);


void setup() 
{
}

void loop() 
{
}

also, as a side note regarding delayMicroseconds():

Caveats and Known Issues

This function works very accurately in the range 3 microseconds and up. We cannot assure that delayMicroseconds will perform precisely for smaller delay-times.