Linear encoder, serial communication, problems and questions

Hi all,

Let me first explain what I'm trying to do:

  • I have an arduino mega that is my main "controller"
  • I have an arduino micro with a linear encoder hooked up to it

The only task for the micro is to read the encoder and pass the value to the mega.
Why not connect the encoder to the mega ? The mega should be doing PID control, I read that the many interrupts from the decoder can interfere in that.

On the micro I use the libraries:

#include <Encoder.h>
#include <math.h>
#include <EasyTransfer.h>

I have the following sketch on the micro:

// includes
#include <Encoder.h>
#include <math.h>
#include <EasyTransfer.h>

//====================================================
//SETUP TRANSFER =====================================
EasyTransfer ET; 

struct SEND_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to send
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  int intPosition;
};

//give a name to the group of data
SEND_DATA_STRUCTURE myData;

//====================================================
//SETUP ENCODER ======================================
Encoder EncoderMM(2, 3);
int oldPosition  = 0;

//int use to determine when to transmit, only when value changes
int intTransmitOld = 0;


void setup() {

  //SERIAL setup ==============================================================
  Serial.begin(9600);

  ET.begin(details(myData), &Serial);
  
}  //end setup

void loop() {

    //get value from encoder
    int newPosition = EncoderMM.read();
    float posMM = newPosition * 0.042333;

    //posMM now contains for example 3.716mm
    //the precision of the position to transmit can be .1mm (... 1.5 , 1.6, 1.7, etc)
    //it should be transmitted as an integers in tenths of an mm *10 
    // math.round(3.716 * 10) = 37
    
    int intTransmitNew = round(posMM*10);

    if (intTransmitNew != intTransmitOld) {

      intTransmitOld = intTransmitNew;
      
      myData.intPosition = intTransmitNew;
      ET.sendData();
      //Serial.println(intTransmitNew);
      
    }

    //sending position every 1/100s of a secound is enough:
    delay(10);
  
} //end loop

Now here is my first scenario:

  • micro stand alone connected via USB
  • encoder connected to 5V, GND, pins 2 and 3 for data readout
  • I upload the sketch with serial monitor close
  • I move the encoder strip
  • TX LED does not blink
  • I open the serial monitor
  • move the encoder strip
  • TX LED blinks
  • I close the serial monitor
  • I move the encoder strip
  • TX LED DOES blink

So it only blinks after starting up the serial monitor from the IDE.
It keeps blinking when moving the encoder after closing the serial monitor

My question is: Is the micro transmitting over TX when the TX-led is not blinking ?

For communication I would like to hook it up like this:
(with only the USB connected to the mega)

My fritzing skills are not the best but:

Micro --- Encoder
5V    --- V+
GND   --- GND
PIN2  --- Signal_1
PIN3  --- Signal_2

Mega --- Micro
5V   --- Vin
GND  --- GND
RX0  --- TX

The fact that the TX LED on the micro does not blink worries me a bit. Will communication be possible this way ?

Thanks for reading :slight_smile:

My question is: Is the micro transmitting over TX when the TX-led is not blinking ?

Yes. The thing that makes the TX and RX LEDs blink is the USB controller in the PC. No PC, no blinking.

Grumpy_Mike:
Yes. The thing that makes the TX and RX LEDs blink is the USB controller in the PC. No PC, no blinking.

So if I hook up these two arduinos, the serial (from TX on micro to RX on mega) will be just fine ? Also if the led is not blinking ? That would be a good thing :slight_smile:
I'll just give it a try tomorrow.

However, just curious, why does the LED only starts blinking when I start up the serial monitor ?
And keeps blinking after I close the serial monitor ?

Thanks

However, just curious, why does the LED only starts blinking when I start up the serial monitor ?
And keeps blinking after I close the serial monitor ?

Because the USB drivers in the PC keep on going once the serial monitor is shut down. Remove the USB lead and replace it an the LEDs should stop.