I'm trying to convert a char (containing only 0/1) into a vector where each single char is stored as an integer. He use Vector library for Arduino, which is not as well documented as basic c++ STL. here is the function :
void reception_emission(char* message) {
Vector <int> integers;
Serial.println(message);
Serial.println("suite");
// Convertir la chaîne de caractères en un vecteur d'entiers
for (int i = 0; i<strlen(message); ++i) {
if (message[i] == '0' || message[i] == '1') {
//integers.push_back(message[i] - '0')
Serial.println(message[i]);
Serial.println("!!");
const int nb=message[i] - '0';
Serial.println(nb);
Serial.println("////");
integers.push_back(nb);
} // Convertir le caractère en entier
}
Serial.print("size: ");
Serial.println(integers.size());
// Appeler la fonction emission pour chaque entier dans le vecteur
for (int i = 0; i < integers.size(); ++i) {
//Serial.println(char(integers[i]));
emission(laserPin, micros(), integers[i]);
void_blank(laserPin, micros());
}
}
I call it using :
reception_emission("01001010101");
I print many outputs to see what is exactly the problem. On the serial monitor, I have :
0
!!
0
////
0
!!
0
////
1
!!
1
////
0
!!
0
////
1
!!
1
////
0
!!
0
////
1
!!
1
////
0
!!
0
////
1
!!
1
////
size: 0
it means that : every char is well converted in integer and stored in "nb".
But, at the end, you can see that the vector interger is still empty.
So, what is the problem here ? did not find anything yet.
thank you.
Unlike
std::vector,Vectordoes not provide its own memory storage. You have to give it storage yourself.According to the readme at https://github.com/janelia-arduino/Vector/tree/master:
As such, your
integersvector is always empty because it has nothing to store its values into.Try something more like this instead:
That being said, if you don't mind tweaking your logging a little then you can just get rid of the
Vectoraltogether as you don't actually need it in this code: