Bluetooth communication of large amounts of bytes

I'm designing a laser engraving printer driven by my android application that transmits data via bluetooth. From bluetooth HC-05 comes arduino an image composed of an array of 45000/90000 bytes; When I print them and number them on the serial monitor i see only 1700; If I change the delay value I see even less; what I do not understand? thank you

Giampaolo Sacchetto

//Includo libreria SoftwareSerial
 #include <SoftwareSerial.h>
 
  //definisco pin RX e TX da Arduino verso modulo BT
  #define BT_TX_PIN 2
  #define BT_RX_PIN 3
 
  int VAL=0;
  int LED=9;
  int NUMERO_BYTE=0;

  //istanzio oggetto SoftwareSerial (il nostro futuro bluetooth)
  SoftwareSerial bt =  SoftwareSerial(BT_RX_PIN, BT_TX_PIN);
 
  void setup() {
 
  //definisco modalità pin
  pinMode(BT_RX_PIN, INPUT);
  pinMode(BT_TX_PIN, OUTPUT);
  pinMode(LED,0);
  //inizializzo comunicazione Seriale
  Serial.begin(9600);
 
  //inizializzo comunicazione Bluetooth
  bt.begin(9600);
 
  }
 
  void loop() {
 
  while (bt.available() > 0) {
   
    Serial.println(bt.read());
    NUMERO_BYTE+=1;
    Serial.println(NUMERO_BYTE);
    Serial.println("======");
    VAL=bt.read();
    analogWrite(LED,VAL);
    delay(5);
    VAL=0;
    analogWrite(LED,0);
   
  }
  
  //delay(5);
  
  }

My guess is that for each bt byte that comes in you are printing a bunch of bytes and the serial buffer can't keep up. The serial input buffer is filling faster than it is being emptied. Increase the Serial baud rate to 115200 (or faster) and reduce the stuff that prints and see if there is a difference.
Actually you are reading 2 bt bytes each time. Each read removes a byte from the buffer. Not sure that that is on purpose? Still not printing fast enogh to prevent the buffer from filling.

By changing the transmission speed to 115200 I see 3500 bytes, far from the 45,000 sent by my application. It is probably the buffer that fills faster than it gets empty. For my project I need each of these bytes that will run a laser diode in turn driven by 2 stepper motors. Do you have any suggestions to give me? thank you

delay(5);

Is that still in your code? If you are trying to read bytes as quickly as possible, you can't stop the processor from working for 5 ms every time through loop(). Sorry that I didn't see that before.

Please post your new code.

ho tolto il delay(5);
settato la velocita della seriale a 115200.
inviato dalla mia applicazione un array di 1200 byte;

Il monitor seriale stampa fino al 339° byte; ???

0->166
1->0
2->166
3->0
4->166
5->0
6->166
7->0
8->166
9->0
10->0
11->166
12->0
13->166
14->0
15->166
16->0
17->166
18->0
19->166
20->0
21->166
22->0
23->166
24->0
25->166
26->0
27->0
28->166
29->0
30->166
31->0
32->166
33->0
34->0
35->0
36->0
37->0
38->0
39->0
40->0
41->0
42->0
43->0
44->0
45->0
46->0
47->0
48->0
49->0
50->0
51->0
52->0
53->0
54->0
55->0
56->0
57->0
58->0
59->166
60->0
61->166
62->0
63->166
64->0
65->166
66->0
67->0
68->166
69->0
70->0
71->0
72->0
73->166
74->166
75->166
76->0
77->0
78->0
79->0
80->0
81->0
82->0
83->0
84->0
85->0
86->0
87->0
88->0
89->0
90->0
91->0
92->0
93->0
94->0
95->0
96->0
97->0
98->0
99->0
100->0
101->0
102->0
103->0
104->0
105->0
106->0
107->0
108->0
109->0
110->0
111->0
112->0
113->0
114->0
115->0
116->0
117->0
118->0
119->0
120->0
121->0
122->0
123->166
124->166
125->0
126->0
127->0
128->0
129->0
130->0
131->166
132->166
133->166
134->166
135->166
136->0
137->0
138->0
139->0
140->166
141->166
142->0
143->0
144->0
145->0
146->0
147->0
148->0
149->0
150->0
151->0
152->0
153->0
154->0
155->0
156->0
157->0
158->0
159->0
160->0
161->0
162->0
163->0
164->0
165->0
166->0
167->0
168->0
169->0
170->0
171->0
172->0
173->0
174->0
175->0
176->0
177->0
178->0
179->0
180->0
181->0
182->0
183->0
184->0
185->0
186->0
187->0
188->0
189->0
190->0
191->0
192->0
193->0
194->0
195->0
196->0
197->0
198->0
199->0
200->0
201->0
202->0
203->0
204->0
205->0
206->0
207->0
208->0
209->0
210->0
211->0
212->0
213->0
214->0
215->0
216->0
217->0
218->0
219->0
220->0
221->0
222->0
223->0
224->0
225->0
226->0
227->0
228->0
229->0
230->0
231->0
232->0
233->0
234->0
235->0
236->0
237->0
238->0
239->0
240->0
241->0
242->0
243->0
244->166
245->166
246->166
247->166
248->166
249->0
250->0
251->0
252->0
253->0
254->0
255->0
256->0
257->0
258->0
259->0
260->0
261->0
262->0
263->0
264->0
265->0
266->0
267->166
268->166
269->166
270->0
271->0
272->0
273->0
274->0
275->0
276->0
277->166
278->0
279->0
280->166
281->0
282->166
283->0
284->166
285->0
286->0
287->0
288->0
289->0
290->0
291->0
292->0
293->0
294->0
295->0
296->0
297->0
298->0
299->0
300->0
301->0
302->0
303->0
304->0
305->0
306->0
307->0
308->0
309->0
310->0
311->0
312->166
313->166
314->166
315->166
316->166
317->166
318->166
319->166
320->166
321->166
322->166
323->166
324->166
325->166
326->166
327->166
328->166
329->166
330->166
331->166
332->166
333->166
334->166
335->166
336->166
337->166
338->166
339->166

Questo è il codice usato;

/*
 * Arduino Bluetooth HC-05 
 
 */ 
 
 //Includo libreria SoftwareSerial
 #include <SoftwareSerial.h>
 
  //definisco pin RX e TX da Arduino verso modulo BT
  #define BT_TX_PIN 2
  #define BT_RX_PIN 3
 
  int VAL=0;
  int LED=9;
  int NUMERO_BYTE=0;

  //istanzio oggetto SoftwareSerial (il nostro futuro bluetooth)
  SoftwareSerial bt =  SoftwareSerial(BT_RX_PIN, BT_TX_PIN);
 
  void setup() {
 
  //definisco modalità pin
  pinMode(BT_RX_PIN, INPUT);
  pinMode(BT_TX_PIN, OUTPUT);
  pinMode(LED,0);
  //inizializzo comunicazione Seriale
  Serial.begin(115200);
 
  //inizializzo comunicazione Bluetooth
  bt.begin(9600);
 
  }
 
  void loop() {
 
  while (bt.available() > 0) {
   
   Serial.print(NUMERO_BYTE);
   Serial.print("->");
   Serial.println(bt.read());
   
   
   NUMERO_BYTE+=1;
   VAL=bt.read();
    
   analogWrite(LED,VAL);
   //delay(5);
   VAL=0;
   analogWrite(LED,0);
   
  }
  
  
  
  }

There is a function Serial.availableForWrite() which will tell you how much space there is in the Serial output buffer.

...R

Ho inserito la funzione sia in setup che in loop per avere una lettura iniziale e una lettura ad ogni byte che ricevo:

lettura iniziale 63 byte;

durante la lettura dei byte lo spazio scende progressivamente da 63 a 0 ;

This is the English language section of the Forum.

You can click Report to Moderator and ask to have the Thread moved to the Italian (?) section.

...R

Sorry, I repeat in English (with the translator)

I set the serial transmission to 115200 baud (the maximum I can set);
I entered the Serial.availableForWrite () function;

I have sent a 1200-byte array from my application;
The function returns the value of 63 and progressively decreases to 0;
The serial monitor prints up to 253 ° bytes.

If I send a 45,000 bytes array of serial print monitors up to 4663 bytes.

How many bytes the buffer can contain? 63?

I need all the bytes sent by my application to turn on a laser diode (now represented by the LED) moved by 2 stepper motors.

Do you have any suggestions to do this?

thank you

/*
 * Arduino Bluetooth HC-05 
 
 */ 
 
 //Includo libreria SoftwareSerial
 #include <SoftwareSerial.h>
 
  //definisco pin RX e TX da Arduino verso modulo BT
  #define BT_TX_PIN 2
  #define BT_RX_PIN 3
 
  int VAL=0;
  int LED=9;
  int NUMERO_BYTE=0;
  int N_BYTE_DISP=0;

  //istanzio oggetto SoftwareSerial (il nostro futuro bluetooth)
  SoftwareSerial bt =  SoftwareSerial(BT_RX_PIN, BT_TX_PIN);
 
      void setup() {
     
          //definisco modalità pin
          pinMode(BT_RX_PIN, INPUT);
          pinMode(BT_TX_PIN, OUTPUT);
          pinMode(LED,0);
          //inizializzo comunicazione Seriale
          Serial.begin(115200);
         
          //inizializzo comunicazione Bluetooth
          bt.begin(9600);
         
      }
 
      void loop() {
        
          while (bt.available() > 0) {
          Serial.print(NUMERO_BYTE);
          Serial.print("->");
          Serial.println(bt.read());
       
       
          NUMERO_BYTE+=1;
           VAL=bt.read();
            
          analogWrite(LED,VAL);
           
          VAL=0;
          analogWrite(LED,0);
           
          N_BYTE_DISP=Serial.availableForWrite();
          Serial.println(N_BYTE_DISP);
       
      }
 
  }

I am confused.

Are you trying to send data FROM your Arduino or receive data INTO your Arduino.

Your program only has Serial.print() commands which send data FROM the Arduino. But your description says

I need all the bytes sent by my application to turn on a laser diode (now represented by the LED) moved by 2 stepper motors.

which makes me think you need to receive data INTO your Arduino.

If you need to receive data in your Arduino Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The system in the 3rd example will be the best.

I don't think you will need the complete 45,000 bytes all at once - where would the Arduino store them? So arrange your PC program so it sends a few bytes (the data for one movement) whenever the Arduino requests them.

...R

The sketch is just a test to see that I get all the data (45,000 bytes) coming from my application

Serial.print only serves me to see physically that reception has taken place.

I attach a schema of the project I would like to do

The android app that is sending data has already been completed. I miss the whole part from the arduous side.

My application sends the byte array (a 45,000 bit bitmap) all at once.
Do I have to expect additional memory from the arduino side?

Image from Reply #10 so we don't have to download it. See this Image Guide

...R

From you diagram it looks like you are proposing to send an image to the Arduino from your phone and have the Arduino figure out how to move the stepper motors and operate the laser so as to draw the picture.

Is the image a series of rows of black and white dots that must be burned (or not) by the laser? If so the program on your phone needs to send the data one row at a time, let the Arduino "draw" that row and when it is done it can ask for the data for the next row.

I guess you could send the whole image to the Arduino if it has an SD Card on which it can store the image and later recall the image row by row for the purpose of controlling the laser.

...R

That's what I want to do.
Instead of the sd card I can use an EEPROM?
I think of 64KB EEPROM so I can load all of my bitmap.

Thanks for your help

Or use an FRAM. EEPROM non-volatility, but with SRAM write speeds.