Hello this is my first app and I am trying to make a simple program. I want to control 48 individual relays hooked up with an arduino with by 48 buttons on mit app inventor. Every method I have used to send binary has resulted in the arduino receiving a totally differant binary number.
I have tried to just send text but once I get to double digits it see 12 as 1 and 2
void setup() {
Serial.begin(9600); // Inicializar la comunicación serial a 9600 baudios
}
void loop() {
if (Serial.available()) {
int num = Serial.parseInt(); // Read number received as text
// Convertir el número a binario
String binary = String(num, BIN);
while (binary.length() < 8) {
binary = "0" + binary; // Fill 0 left
}
Serial.println(binary); // Print binary number.
}
}
void setup() {
Serial.begin(9600); // Inicializar la comunicación serial a 9600 baudios
}
void loop() {
if (Serial.available()) {
int num = Serial.parseInt(); // Leer el número recibido como texto
// Convertir el número a binario
String binary = "";
for (int i = 0; i < 48; i++) {
if (i == num) {
binary += "1"; // Agregar "1" en la posición correspondiente al número
} else {
binary += "0"; // Agregar "0" en las demás posiciones
}
}
Serial.println(binary); // Imprimir el número binario
}
}