i have to save data every movement from 2 (arduino uno & encoder) as slave to 1 arduino mega as master.
can anyone help me??
my type data on "delta Motor" is "long".
Images from Original Post so we don't have to download them. See this Simple Image Guide
EDIT OP seems to have changed the image files. However they are now visible in the Original Post
...R
Please don't post pictures of text, just copy and paste the text. Also please post the complete program.
What is on the GoogleDrive link?
My first thought is why do you need 3 Arduinos? Couldn't everything be done on the Mega?
If that is not practicable then you need to give us an example of what must be in a message sent from an Uno and tell us how often those messages must be sent.
In the meantime have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable.
You can send data in a compatible format with code like this
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker
Each Uno could be connected to a different HardwareSerial port on the Mega.
...R
Hi,
Welcome to the forum.
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Thank.. you.... ![]()
Robin2:
Please don't post pictures of text, just copy and paste the text. Also please post the complete program.What is on the GoogleDrive link?
My first thought is why do you need 3 Arduinos? Couldn't everything be done on the Mega?
If that is not practicable then you need to give us an example of what must be in a message sent from an Uno and tell us how often those messages must be sent.
In the meantime have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable.
You can send data in a compatible format with code like this
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker
Each Uno could be connected to a different HardwareSerial port on the Mega. ...R
I want to save my value from my encoder at every movement. I'm using arduino mega because if i'm using 1 Arduino uno + 2 rotary encoder, arduino cant interrupt at the same time.
I did try 1 arduino mega + 2 encoder. and its doesnt work.
this is my code for 1 encoder + 1 uno
//Wiring Diagram
//A (black) D2
//B (white) D3
//V0 (Blue) Gnd
//V+ (brown) 5V
const byte intr_pin_A = 2;
const byte intr_pin_B = 3;
volatile long state_motor_L = 0;
volatile bool newISR = false;
long workingMotorCount;
long Time;
double teta;
int posisiSebelum = 0;
int posisiSekarang = 0;
int deltaPosisi;
float motorCOUNTradian;
int VR;
int VL;
float fluks;
int x_dot;
//unsigned long previousMilis = 0;
unsigned long duration,previousMilis;
unsigned long waktuSekarang = 0;
long old_count;
long new_count;
long delta_motor;
unsigned long period = 1000;
void setup()
{
pinMode(intr_pin_A, INPUT);
pinMode(intr_pin_B, INPUT);
attachInterrupt(0, enc_loop, RISING);
Serial.begin(9600);
VL = 0; // asumsi
}
void loop()
{
if (newISR == true) {
newISR = false;
noInterrupts();
workingMotorCount = state_motor_L;
motorCOUNTradian = workingMotorCount * 0.06283183531; // 3,6 degree = 0.06283183531 radian
interrupts();
new_count = motorCOUNTradian;
delta_motor = new_count - old_count; // omega R
old_count = new_count;
Serial.print("delta_motor"); Serial.println(delta_motor);
delay(1000);
}
}
void enc_loop()
{
newISR = true;
if (digitalRead(intr_pin_B) == LOW) {
state_motor_L++; // CW
}
else {
state_motor_L-- ; // CCW
}
}
the value will change if i twist shaft's rotary encoder
and this is my result



roysuryosantoso:
I'm using arduino mega because if i'm using 1 arduino uno + 2 rotary encoder, arduino cant interrupt at the same time.
Why not?
How many pulses per second are the encoders producing? What is causing the encoders to rotate?
If a single Arduino is not able to keep up with all of the pulses from the two encoders then you can be pretty certain that it is not possible for 2 separate Arduinos to send every single pulse to a 3rd Arduino.
I reckon if you describe your project someone will probably be able to make some useful suggestions.
...R
What is causing the encoders to rotate?
in my trial, my twist are causing the encoders to rotate
in my project, Motor are causing the encoders to rotate?
How many pulses per second are the encoders producing?
i dont know, this is the database of my encoder.


You will have to get an idea on how fast the pulses come in: run a test with your motors at full speed, count pulses for a second, print out how many that are. You may get a couple thousand a second, no problem for a single Arduino to read two of those. In fact, it'll be MUCH easier to read both on a single Arduino to get the difference, than trying to do it your way. There are encoder libraries out there that can make it really easy.
Furthermore, you won't miss interrupts. If an interrupt comes in while one is being processed, it'll just be handled when the one is done. That's also why you have to keep your ISRs short.
Furthermore, you won't miss interrupts. If an interrupt comes in while one is being processed, it'll just be handled when the one is done. That's also why you have to keep your ISRs short.
speaking of interrupt, can you show me the interrupt's code for my 2 encoder??
roysuryosantoso:
speaking of interrupt, can you show me the interrupt's code for my 2 encoder??
Post the code you are now using the read the encoder.
Have you googled "Arduino encoder"?
...R
roysuryosantoso:
speaking of interrupt, can you show me the interrupt's code for my 2 encoder??
Which pins do you use?
Do you intend to use pin change interrupts or external interrupts?
Post the code you are now using the read the encoder.
Have you googled "Arduino encoder"?
this is my code for 2 encoder in 1 arduino uno
//Wiring Diagram encoder 1 as motor A
//A (black) D2
//B (white) D3
//V0 (Blue) Gnd
//V+ (brown) 5V
//Wiring Diagram encoder 2 as motor b
//A (black) D8
//B (white) D9
//V0 (Blue) Gnd
//V+ (brown) 5V
const byte intr_pin_A = 2; //A (black) --> encoder 1
const byte intr_pin_B = 3; //B (white) --> encoder 1
const byte intr_pin_C = 8; //A (black) --> encoder 2
const byte intr_pin_D = 9; //B (white) --> encoder 2
volatile long state_motorA_L = 0;
volatile long state_motorB_L = 0;
volatile bool newISR_MA = false; // MA = MotorA
volatile bool newISR_MB = false; // MB = MotorB
long workingMotorCount_MA; // MA = MotorA
long workingMotorCount_MB; // MB = MotorB
long Time;
double teta;
int posisiSebelum = 0; // previous position
int posisiSekarang = 0; // current position
int deltaPosisi; // delta position
float motorCOUNTradian_MA; // MA = MotorA
float motorCOUNTradian_MB; // MB = MotorB
int VR;
int VL;
float fluks;
int x_dot;
long old_count_MA; // MA = MotorA, old count motor A is means, the last position angle.
long old_count_MB; // MA = MotorA, old count motor B is means, the last position angle.
long new_count_MA; // MA = MotorA, old count motor A is means, the current position angle.
long new_count_MB; // MA = MotorA, old count motor B is means, the current position angle.
long delta_motor_MA; // MA = MotorA, delta (new count - old count)
long delta_motor_MB; // MA = MotorB, delta (new count - old count)
unsigned long period = 1000;
unsigned long duration, previousMilis;
unsigned long waktuSekarang = 0; //current time (right now)
void setup()
{
pinMode(intr_pin_A, INPUT);
pinMode(intr_pin_B, INPUT);
pinMode(intr_pin_C, INPUT);
pinMode(intr_pin_D, INPUT);
attachInterrupt(0, enc_loop_MA, RISING);
attachInterrupt(0, enc_loop_MB, RISING);
Serial.begin(9600);
Serial.begin(9600);
}
void loop()
{
rodaKANAN(); // right wheel
rodaKIRI(); // left wheel
previousMilis = millis();
if (millis() > previousMilis) {
previousMilis = previousMilis;
waktuSekarang = millis();
duration = waktuSekarang - previousMilis; // waktu sekarang = current time (right now)
}
}
void enc_loop_MA()
{
newISR_MA = true; // Motor A
if (digitalRead(intr_pin_B) == LOW) {
state_motorA_L++; // CW
}
else {
state_motorA_L-- ; // CCW
}
}
void enc_loop_MB() // motor B
{
newISR_MB = true;
if (digitalRead(intr_pin_D) == LOW) {
state_motorB_L++; // CW
}
else {
state_motorB_L-- ; // CCW
}
}
void rodaKANAN() // right wheel
{
if (newISR_MA == true) {
newISR_MA = false;
noInterrupts();
workingMotorCount_MA = state_motorA_L;
motorCOUNTradian_MA = workingMotorCount_MA * 0.06283183531; // 3,6 degree = 0.06283183531 radian
interrupts();
new_count_MA = motorCOUNTradian_MA;
delta_motor_MA = new_count_MA - old_count_MA; // omega (Radian)
old_count_MA = new_count_MA;
Serial.print("delta_motor_MA");Serial.println(delta_motor_MA);
}
}
void rodaKIRI( )// left wheel
{
if (newISR_MB == true) {
newISR_MB = false;
noInterrupts();
workingMotorCount_MB = state_motorB_L;
motorCOUNTradian_MB = workingMotorCount_MB * 0.06283183531; // 3,6 degree = 0.06283183531 radian
interrupts();
new_count_MB = motorCOUNTradian_MB;
delta_motor_MB = new_count_MB - old_count_MB; // omega (Radian)
old_count_MB = new_count_MB; // iterasi
Serial.print("delta_motor_MB");Serial.println(delta_motor_MB);
}
}
}
}
i have searched but it cant help.
Which pins do you use?
2,3,8,9
Do you intend to use pin change interrupts or external interrupts?
the answer is i dont know, if i can use 2 encoder in 1 arduino uno. i can do that, if it doesnt work. i will intend.
Hi,
Your code does not compile, too many }.
With your code in the IDE, select "TOOLS" then "Auto Format".
It will indent all your code properly and allow you to see the { and } structure.
Tom... ![]()
There is a digitalWriteFast library (that includes a digitalReadFast) which is nearly as fast as direct port reading but much easier to use.
If you are only reading the encoder when the motor is powered then you will already know the direction and there should be no need for any digitalRead()s within the ISR.
...R