Hi all, I've been playing around with the MKS Servo42C. The encoder attached to the stepper has a screen on the back that already automatically reads the degree/position from 0 point, error (shaft), and pulses received.
I wrote a quick program to turn the shaft 180deg back and forth. The encoder screen on the back outputs the 3 variables continuously. I'd like my Uno to read these 3 variables as they're outputted on the encoder, but am not sure how to go about this. Do I need to use the UART pins? If so, can someone point me to any example code to reference?
as a starter you can try to just read out one of those with something like this:
(compliles, NOT tested!)
/*
Software serial multple serial test
Receives from the hardware serial, sends to software serial.
Receives from software serial, sends to hardware serial.
The circuit (UNO):
* RX is digital pin 2 (connect to TX of other device)
* TX is digital pin 3 (connect to RX of other device)
Note:
Not all pins on the Mega and Mega 2560 support change interrupts,
so only the following can be used for RX:
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
Not all pins on the Leonardo support change interrupts,
so only the following can be used for RX:
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
created back in the mists of time
modified 25 May 2012
by Tom Igoe
based on Mikal Hart's example
This example code is in the public domain.
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
uint8_t cmd1[3] = {0xE0,0x30,0x10}; //read encoder value
uint8_t j =0;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
// set the data rate for the SoftwareSerial port (change baudrate to match you device)
mySerial.begin(38400);
//send read command
for(uint8_t i=0; i<3; ++i){
mySerial.write(cmd1[i]);
}
}
void loop() // run over and over
{
if (mySerial.available()){
Serial.print(mySerial.read(), HEX); //print out reply from device
Serial.print(" ");
++j;
}
if(j==8){
Serial.println("");
j =0;
delay(100); //arbitrary 100ms between read requests
//send read command
for(uint8_t i=0; i<3; ++i){
mySerial.write(cmd1[i]);
}
}
}