I am working on a project that involves stepper motors. I have a program that calculates the number of steps that the motors have to make (not in Arduino but in another platform) and then sends this number of steps to my Arduino program via Bluetooth. That is okay, it is just the context.
The issue here is that I need the program to run automatically every time a new number of steps is sent, while of course changing the steps variable. I mean, every time I tell the motors that they have to take a certain number of steps, I need the program to actualize the steps variable and to run by itself without me having to re-load it to the board.
I bet this is possible to do with some kind of serial communication, but I haven't used Arduino that much and I don't know how to do it. So I would highly appreciate the help. Thank you in advance!
The serial input basics tutorial will show how to read and parse a packet of data. It shows how to listen for serial input, take the data in a byte at a time and parse the data when a whole packet is received. Also how to arrange the packets for reliable reception.
Packets of data can contain data of several different data types (byte, int, string, ... ) arranged in a comma (or whatever) delimited packet with terminating marker and, optional, start marker.
What you’re asking is quite straightforward.
There are several techniques, you’ll have to choose - but as noted, you first need to understand and handle the serial data.
Then you can run with that until,the next restart, or save it in EEPROM until a new set of values is uploaded.
Well, maybe I have overestimated how well I can use the serial link for the Bluetooth. I had just read documentation about it and I thought I could do it. I can post my code:
#include <SoftwareSerial.h>
#include <AccelStepper.h>
//Bluetooth serial communication
int rx = 11;
int tx = 10;
SoftwareSerial bt = SoftwareSerial(rx,tx);
//
//two stepper motors
int dir1 = 4;
int stp1 = 5;
AccelStepper stepper1(AccelStepper::DRIVER, stp1, dir1);
int steps1 = 2048;
int dir2 = 2;
int stp2 = 3;
AccelStepper stepper2(AccelStepper::DRIVER, stp2, dir2);
int steps2 =1056;
//
void setup()
{
//bluetooth
//pin modes for rx and tx
pinMode(rx, INPUT);
pinMode(tx, OUTPUT);
//serial communication
bt.begin(9600);
delay(5000);
stepper1.setMaxSpeed(200.0);
stepper1.setAcceleration(50.0);
stepper1.moveTo(steps1);
stepper2.setMaxSpeed(200.0);
stepper2.setAcceleration(50.0);
stepper2.moveTo(steps2);
}
Please don't blame me too much because I'm not used to code in arduino at all.
I need to establish the serial communication and somehow upload the variables steps1 and steps2 and run the code without re-loading when these variables are renewed. The other thing here is that (ideally) I need to be able to do this without having the board connected to the pc. So only with the BT connection.
I omitted the loop function because I thought it was not relevant, but of course it has one.
And about the Bluetooth thing, I would appreciate some help too. Maybe my issues with the code are deeper than I thought.
Of course the loop() function is relevant because that is where you will test whether data is available from Bluetooth, read it and use it to set the value of the variables
So it turns out that you want to read the initial position of the steppers from Bluetooth and not later whilst the sketch is running the loop() function
In that case, create a function that reads the Bluetooth and sets the variables and call it from setup(). You will, of course, need to reboot the Arduino in order to read the values
You need to decide how the data from Bluetooth will be formatted
Thanks for the reply! However, my main issue is that I need not having to reboot my Arduino in order to read the values. Is there a way that I can do that?