hi guys;
i want get this number from my bluetooth module 0,1,2,3,4,5,6...,255
i write this code in arduino:
#include <SoftwareSerial.h>
// Connect bluetooth module on pins 9 and 10 (Rx Tx)
SoftwareSerial bluetoothser(9,10);
int sr;
void setup()
{
Serial.begin(115200);
bluetoothser.begin(9600);
}
void loop()
{
if ( bluetoothser.available() )
{
sr = bluetoothser.read();
Serial.println(sr);
}
}
and i get this:
0
0
0
3
0
0
0
4
0
0
0
5
0
0
0
6
0
0
0
7
0
0
0
8
0
0
0
9
0
0
0
10
0
0
0
11
0
0
0
12
0
0
0
14
0
0
0
15
0
0
0
17
0
0
0
18
0
0
0
20
0
0
0
22
0
0
0
23
0
0
0
24
0
0
0
26
0
0
0
28
0
0
0
31
0
0
0
33
0
0
0
36
0
0
0
38
0
0
0
41
0
0
0
45
0
0
0
48
0
0
0
51
0
0
0
54
0
0
0
58
0
0
0
60
0
0
0
64
0
0
0
66
0
0
0
69
0
0
0
71
0
0
0
74
0
0
0
76
0
0
0
78
0
0
0
80
0
0
0
81
0
0
0
83
0
0
0
85
0
0
0
87
0
0
0
89
0
0
0
91
0
0
0
92
0
0
0
94
0
0
0
95
0
0
0
97
0
0
0
98
0
0
0
100
0
0
0
102
0
0
0
104
0
0
0
106
0
0
0
108
0
0
0
110
0
0
0
111
0
0
0
113
0
0
0
115
0
0
0
116
0
0
0
118
0
0
0
119
0
0
0
121
0
0
0
123
0
0
0
124
0
0
0
126
0
0
0
128
0
0
0
130
0
0
0
131
0
0
0
132
0
0
0
133
0
0
0
135
0
0
0
137
0
0
0
140
0
0
0
142
0
0
0
145
0
0
0
147
0
0
0
150
0
0
0
153
0
0
0
156
0
0
0
159
0
0
0
161
0
0
0
164
0
0
0
167
0
0
0
170
0
0
0
172
0
0
0
174
0
0
0
176
0
0
0
178
0
0
0
181
0
0
0
183
0
0
0
185
0
0
0
187
0
0
0
189
0
0
0
191
0
0
0
193
0
0
0
196
0
0
0
198
0
0
0
201
0
0
0
204
0
0
0
206
0
0
0
209
0
0
0
211
0
0
0
214
0
0
0
218
0
0
0
221
0
0
0
223
0
0
0
225
0
0
0
227
0
0
0
229
0
0
0
231
0
0
0
234
0
0
0
236
0
0
0
238
0
0
0
240
0
0
0
242
0
0
0
243
0
0
0
244
0
0
0
245
0
0
0
246
0
0
0
247
0
0
0
248
0
0
0
249
0
0
0
250
0
0
0
252
0
0
0
253
0
0
0
255
so please how i can get only 0,1,2,3,4,5,6,7,8,9,10,11...255
thanks first
system
January 4, 2016, 4:52pm
2
so please how i can get only 0,1,2,3,4,5,6,7,8,9,10,11...255
Make the sender send 0, 1, 2, etc., rather then 0001, 0002, 0003,...
its an android application she's send bytes[] values
You created the android app, so tell us how it is sending the serial data.
Hint: it should be DELIMITED like the last example we took you through.
system
January 4, 2016, 5:21pm
5
If you can't change the format on the sender, you'll need to learn to deal with that format. It looks to me like the sender is sending 4 characters for each value - '0', '0', '0', '3' for 3, for instance.
Wait until there are 4 values. Read all 4, and store in a 5 element array. Store a NULL in the last position. Use atoi() to convert the array to an int.
Robin2
January 4, 2016, 5:27pm
6
Have a look at the examples in Serial Input Basics . They will read and save a complete message and then you can view it to see what you need to do with it. There is also a parse example which could easily deal with things like '0003'
...R
this is how i send information in my android application:
public void write(byte[] bytes) {
try {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
system
January 4, 2016, 5:44pm
9
this is how i send information in my android application:
That says nothing about how the data is placed in the bytes array.
i convert the int to byte array like this :
public byte[] intToBytes( final int i ) {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(i);
return bb.array();
}
then i send all to the write
write(intToBytes(255));
system
January 4, 2016, 6:12pm
12
Robin2:
Pedant
...R
Is that anything like asshole ? 8)
i know this allocate(4);
but i dont know other technique to convert int to byte
can you help me please
thanks
system
January 4, 2016, 6:16pm
14
but i dont know other technique to convert int to byte
There is nothing wrong with that method. You simply need to make the Arduino read what you are sending - 4 bytes per value, with no end marker.
can you give me an exemple code please
thanks first
system
January 4, 2016, 6:25pm
16
can you give me an exemple code please
char clueless[5];
void loop()
{
if(Serial.available() >= 4)
{
for(byte b=0; b<4; b++)
{
clueless[b] = Serial.read();
}
clueless[4] = '\0';
int dumb = atoi(clueless);
// Do something with dumb
}
}
This is a re-launch of this thread . The OP is stubbornly convinced that the Arduino code must be incorrect, yet it appears that the Android code is not performing as the OP states that it should.
It is not possible to resolve this issue until the OP pays full attention to the issues, and provides clear information and responses to the questions that are posed.
thanks
i do this but i get 0,0,0,0,0...
#include <SoftwareSerial.h>
// Connect bluetooth module on pins 9 and 10 (Rx Tx)
SoftwareSerial bluetoothser(9,10);
char clueless[5];
void setup()
{
Serial.begin(115200);
bluetoothser.begin(9600);
}
void loop()
{
if ( bluetoothser.available() >= 4)
{
for(byte b=0; b<4; b++)
{
clueless[b] = bluetoothser.read();
}
clueless[4] = '\0';
int dumb = atoi(clueless);
Serial.println(dumb);
}
}
Your stated goal is to receive 0,1,2,3... and so on. Please explain whether you are expecting 0,0,0,1,0,0,0,2,0,0,0,3 and so on, instead. If so, why?
system
January 4, 2016, 6:50pm
20
#include <SoftwareSerial.h>
// Connect bluetooth module on pins 9 and 10 (Rx Tx)
SoftwareSerial bluetoothser(9,10);
char clueless[5];
void setup()
{
Serial.begin(115200);
bluetoothser.begin(9600);
}
void loop()
{
if ( bluetoothser.available() >= 4)
{
for(byte b=0; b<4; b++)
{
clueless[b] = bluetoothser.read();
Serial.print("clueless[");
Serial.print(b);
Serial.print("] = ");
Serial.println(clueless[b]);
}
clueless[4] = '\0';
int dumb = atoi(clueless);
Serial.println(dumb);
}
}
Add the 4 Serial.print() statements in the for loop, and show us the output.