Hello!
i'm a sculptor studying technology and art using some kind like openframeworks, processing touchdesigner.
My big plan is control lots of precise motor (16~)
So i just practicing with make a simple motor control system.
composition of my little system is like
"main controller" (arduino nano or mac with openframeworks) send data (integer 0~256, 0~1024)
--i2c->
"arduino + stepper motor controller"
--direction, step->
"motor movement"
the timing or speed of the motor is not a big issue in this project.
but at least i have to see multiple motors moving!
the code in the below doesn't work at all.
i think maybe master doesn't sending data is the problem..
i already checked with i2c address scanner, and they could detect slave device as 0x08
So i think the main problem is not a wiring or circuit connection (i also checked motor moving with isolated condition)
Is there anyone would help this poor newb?
Thank you.
//------------------------------------------------slave code
#include <AccelStepper.h>
#include <Wire.h>
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
int Data;
void setup() {
Wire.begin(8);
Wire.onReceive(receiveEvent);
stepper.setMaxSpeed(600);
stepper.setAcceleration(1100);
}
void receiveEvent(){
Data = Wire.read();
Serial.println(Data);
}
void loop() {
int a = Data;
//long i = a;
stepper.moveTo(a);
stepper.runToPosition();
delay(1000);
stepper.moveTo(0);
}
//----------------------------------------------------master code
#include <Wire.h>
#define SLAVE_ADDR 8
int x = 255;
int y = 1;
void setup() {
Wire.begin();
}
void loop() {
delay(50);
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(x);
Wire.endTransmission();
}
no.. nothing shows up on serial monitor..
i didn't used serialprint in this code
how can check in this situation with serial monitor?
and is that help in this occasion??
i already checked motor, driver and controller connection!
its connection is same as below link image
and uses same driver A4988 https://images.app.goo.gl/QprDof4VLawut8p59
Your I2C receive event handler has a print statement in it. I'm not sure if print works inside the event handler. Do you get anything on the serial monitor if you print out the value of Data in the main loop?
Remove the Serial.println() from the receiveEvent() as markd833 wrote.
Add a flag in the receiveEvent(). For example: bool newRxData; See the tutorial by Robin2.
In the loop(), you should only do something when the flag shows that new data has arrived. When there is no new data, then do nothing.
Turn the delay(50) into delay(1000) in the loop() in the Master as Robin2 wrote.
Add Serial output for both the Master and the Slave. Show something as soon as the sketch starts:
void setup()
{
Serial.begin( 9600);
Serial.println( "The Master sketch has started");
...
}
The I2C bus has three wires: SDA, SCL, GND. Don't use a cable.
Show us the new Master and Slave sketch, each between code-tags.
That's about it.
Did you know that you can develop both the Master and Slave sketch side by side on your screen ?
Start the Arduino IDE, select a board, and you can also open the serial monitor.
Then start another instance of the Arduino IDE, select the other board, and you can also open the serial monitor for that board.
Their movements are not perfect, but at least they communicate each other and moves by sending signal!
Yours help that the solution i didn't know before was
i2c wire -> my i2c wire connection setting was VCC, GND, SCL, SDA. so i cutted out vcc connection
(i can't say vcc connection was the problem.. but i found almost other people didn't connected each vcc, some oppinion could help this issue I'm curious!)
The code in the slave's stepper operation command was not completed
so i completed adding "stepper.runToPosition();" in the end
int a = Data;
stepper.moveTo(a);
stepper.runToPosition();
/*delay(1000);
stepper.moveTo(0);
stepper.runToPosition(); //<-------- i forgot to do this!
delay(1000);
I corrected trasmit delay time to catched up by stepper motor movement!
Deleted serial print function from receiveEvent
Serial.println(Data);
thanks every guys for helping me!
also i want to know
how can I control stepper motor using only shot of one pulse(?) signal to don't disturb motor moving update timing!
this "slow motor catch up" issue will make whole system slow and efficientless.
Any advice will help me!
Thank you again, you're all guys are too kind to newb!
raven_oh:
how can I control stepper motor using only shot of one pulse(?) signal to don't disturb motor moving update timing!
this "slow motor catch up" issue will make whole system slow and efficientless.
I mean signal sending and motor moving is too restricted to each others operation
I used these "one arduino i2c with multiple slave arduino system" to solving this problem!
when start transmitting(when turned on master arduino) signal throungh i2c, master repeatly send message to slave.
so i just want control signal output to send only one time, not endlessly!
I don't know if the explanation is good, but do my best to explain what i want to do is
<Ex. case 1 > that i wanted
master signal sends one time -> slave receive -> master stop sends -> slave's motor moves
<Ex. case 2 > also i wanted
master signal sends -> slave receive -> slave's motor moves -> master sends again when motor didn't finished movement but slave don't interrupted by master sending signal
is there any post or lecture for help this kind of problem?
I apologize about my english ability is not good enough!
raven_oh:
I don't know if the explanation is good, but do my best to explain what i want to do is
It would help to understand your Reply #8 if you can give an example of the messages that the master will send to the slave for each case, and what you want the slave to do with the data it receives.
The idea in my mind at the moment is that the master sends, for example, -120 and the slave is expected to move the motor 120 steps counter-clockwise.
But I'm not at all sure that that is the sort of thing you have in mind.