Upload data to arduino without re-loading the program?

Hello y'all!

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!

Welcome to the forum

If you can already send data to the Arduino then you are already using a serial link so what is the problem ?

post your code

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.

Look into Serial data transfer and EEPROM.

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 can see nothing in your code that reads anything from Bluetooth. Come to that, your code has not got a loop() function in it so will not compile

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

Then here it is the full 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);
}


void loop()
{  
  stepper1.run();
  stepper2.run();
}

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

See Serial input basics - updated

for ideas on this

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?

if you want to receive data while the motors are running, you need to put some code in the loop for doing that...

Sure, but the thing is that I don't really know how :frowning:

you were given the answer

Forget you current sketch for now

Attach your Bluetooth device to 2 pins of your choosing, say 10 and 11 as in your sketch then in loop(). Pair your Bluetooth device with the Arduino.

In loop(), if data is available() on those pins read it and print it on the Serial monitor. Does it match what you are sending it ?

Come back when you have that working

just to be clear on @UKHeliBob message

➜ create a Software Serial instance on those pins and configure it the right way

This seems to indicate that the Bluetooth connection is working, but it is not clear

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.