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
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.
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);
}
}
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.
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.