1

The gist of the problem is that I'm trying to have an Arduino with a breadboard that has 5 buttons and 4 LEDs each led is linked with a button and the fifth button "sends" the lights that were switched on with a char array to the receiving Arduino, in this Arduino, this Arduino algo has a breadboard with 4 LEDs of the same color and with the char array I try to switch the same LEDs that were switched on in the first Arduino, my problem is when I send the char array and receive it in the second Arduino and try to run a function that runs through the array and find which points in the char array are different than 0 and turn on a led it never turns any LEDs, I know the logic is right but I don't understand why it doesn't work.

enter image description here

This is the design I am working with in tinkercad and will also link the project if you wish to run it on there, I assume that the problem is from tinkercad and not the code though I may be horribly wrong.

This is the sender sketch

#include <SoftwareSerial.h>

#define rxPin 9
#define txPin 8


// Iniciamos los pins de los 4 leds de sender y reciver
const int ledRed = 13;
const int ledBlue = 12;
const int ledGreen = 11;
const int ledWhite = 10;

// iniciamos los pins de los 5 botones
const int pushbuttonRed = 7;
const int pushbuttonBlue = 6;
const int pushbuttonGreen = 5;
const int pushbuttonWhite = 4;
const int pushbuttonEnviar = 3;

// Declaramos el estado de los 4 leds de sender y reciver
int ledStateRed = 0;  
int ledStateBlue = 0;
int ledStateGreen = 0;
int ledStateWhite = 0;

char c;
char leds[7] = {'H','0','0','0','0','F','\0'};

//String cadena_final = "";
SoftwareSerial receiver =  SoftwareSerial(rxPin, txPin);

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  Serial.begin(9600); 
  receiver.begin(9600);
  pinMode(ledRed, OUTPUT);
  pinMode(ledBlue, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledWhite, OUTPUT);
  pinMode(pushbuttonRed, INPUT);
  pinMode(pushbuttonBlue, INPUT);
  pinMode(pushbuttonGreen, INPUT);
  pinMode(pushbuttonWhite, INPUT);
  pinMode(pushbuttonEnviar, INPUT);
}

void loop() {   

    // Boton Rojo
    if(digitalRead(pushbuttonRed) == 1) { 
      digitalWrite(ledRed, HIGH);
      ledStateRed += 1;
      leds[1] = '1';
      if(ledStateRed % 2 == 0){
        digitalWrite(ledRed, LOW);
        leds[1] = '0';
        ledStateRed = 0;
      }
      delay(100);
    }

    // Boton Azul
    if(digitalRead(pushbuttonBlue) == 1) { 
      digitalWrite(ledBlue, HIGH);
      ledStateBlue += 1;
      leds[2] = '1';
      if(ledStateBlue % 2 == 0){
        digitalWrite(ledBlue, LOW);
        ledStateBlue = 0;
        leds[2] = '0';
      }
      delay(100);
    }

    // Boton Verde
    if(digitalRead(pushbuttonGreen) == 1) { 
      digitalWrite(ledGreen, HIGH);
      ledStateGreen += 1;
      leds[3] = '1';
      if(ledStateGreen % 2 == 0){
        digitalWrite(ledGreen, LOW);
        ledStateGreen = 0;
        leds[3] = '0';
      }
      delay(100);
    }

    // Boton Blanco
    if(digitalRead(pushbuttonWhite) == 1) { 
      digitalWrite(ledWhite, HIGH);
      ledStateWhite += 1;
      leds[4] = '1';
      if(ledStateWhite % 2 == 0){
        digitalWrite(ledWhite, LOW);
        ledStateWhite = 0;
        leds[4] = '0';
      }
      delay(100);
    }

    // Boton Enviar
    if(digitalRead(pushbuttonEnviar) == 1) { 
      // Reinicio los leds de sender para enviar otro mensaje
      ledStateWhite = 0;
      ledStateGreen = 0;
      ledStateBlue = 0;
      ledStateRed  = 0;
      digitalWrite(ledWhite, LOW);  
      digitalWrite(ledRed, LOW);
      digitalWrite(ledBlue, LOW);
      digitalWrite(ledGreen, LOW);
      delay(100);
      Serial.write(leds,7);
      leds[1] = '0';
      leds[2] = '0';
      leds[3] = '0';
      leds[4] = '0';
    }
}

void printArray(char A[]){
    int i = 0;
    for(i=0;i<6;i++){
        Serial.print(A[i]);
    }
}

This is the receiver sketch

#include <SoftwareSerial.h>

#define rxPin 9
#define txPin 8

char leds2[7] = {'\0'};

// Iniciamos los pins de los 4 leds 
const int Red = 4;
const int Blue = 5;
const int Green = 6;
const int White = 7;

// Creamos el boton que limpia pantalla
const int buttonClear = 10;

char c;
String recData;

SoftwareSerial receiver =  SoftwareSerial(rxPin, txPin);

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT); 
  receiver.begin(9600);
  pinMode(Red, OUTPUT);
  pinMode(Blue, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(White, OUTPUT);

  Serial.begin(9600);
}

void loop() { 
    int i=0;

    if (receiver.available()) {

      delay(100); //allows all serial sent to be received together
      while(receiver.available() && i<6) {
        leds2[i] = receiver.read();   // starts with i=0 
        delay(100);
        i++;
      }
      leds2[i]='\0';
      //digitalWrite(Red,HIGH);
    }
    if(i>0) {
      lightLeds(leds2);
    }
}

void lightLeds(char A[]){
    int i;

    for(i=0;i<6;i++){
        if(A[1]=='1' || A[1]==1) digitalWrite(Red, HIGH);
        if(A[2]=='1' || A[2]==1) digitalWrite(Blue, HIGH);
        if(A[3]=='1' || A[3]==1) digitalWrite(Green, HIGH);
        if(A[4]=='1' || A[4]==1) digitalWrite(White, HIGH);
    }
}

void printArray(char A[]){
    int i = 0;
    for(i;i<6;i++){
        Serial.print(A[i]);
    }
}


2

Your "working logic" is quite unlogic:

In sender you define a char array:

char leds[6] = {'H','0','0','0','0','F'};

which should be

 char leds[7] = {'H','0','0','0','0','F','\0'}; // terminate the array

Here each '0' represents a led as can be seen in subroutine Boton Enviar Instead of assigning a state to each position 0/1 so leds[2]='1' would be e.g. led blue ON you write a number to it (example walk through for blue):

if(digitalRead(pushbuttonBlue) == 1) { 
      digitalWrite(ledBlue, HIGH);
      ledStateBlue += 1;
      leds[2] = '5';  // Here should be leds[2] = '1';
      if(ledStateBlue % 2 == 0){
        digitalWrite(ledBlue, LOW);
        ledStateBlue = 0;
        leds[2] = '0';

so if blue and and you send (if using the terminator)

 leds[] = {'H','5','0','0','0','F'.'\0'};

On the receiver side the following happens:

   while(receiver.available() && i<6) {
        leds2[i++] = receiver.read();   // starts with i=1 
        delay(100);
      }
      leds2[i++]='\0';
      digitalWrite(Red,HIGH);
    }
    if(i>0) {
      lightLeds(leds2);

so your led2 looks like

 {XX,'H','5','0','0','0','F'} //XX is undefined garbage might be '\0' so the array ends here

Solution define array as follows:

char leds2[7] ={'\0'};
.....
   while(receiver.available() && i<6) {
        leds2[i] = receiver.read();   // starts with i=0 
        delay(100);
        i++;
      }
      leds2[i]='\0';
      // digitalWrite(Red,HIGH); // USE builtin led not one of the used as "end marker"
    }
    if(i>0) {
      lightLeds(leds2)

The light logic should go for the space a value is and not the value:

void lightLeds(char A[]){
    int i;

    for(i=0;i<6;i++){
        if(A[2]=='1' || A[2]==1) digitalWrite(Blue, HIGH);
        if(A[2]=='0' || A[2]==0) digitalWrite(Blue, LOW);
       ....
    }
}

To get the mix of different logics apart place both progs in an editor (eg. notepad++) side by side and look for differences in the logic. There is more than one error in the programs, fix them as proposed and it wll work (Read the comments in my code for further details).
EDIT
The OP had still problems with his code. I tookthe example and let it run hardware based on two UNOs, it worked but SoftwareSerial is very bad. No problem on the hardware with AltSoftSerial.h. In TinkerCAD no chance, the moment I switched to HW-Serial it worked like a charm, so here is the refined code for SENDER:

// Time variables
unsigned long startTime = 0;
unsigned long intervalTime = 1000; // send once a second

// Iniciamos los pins de los 4 leds de sender y reciver
const int ledRed = 13;
const int ledBlue = 12;
const int ledGreen = 11;
const int ledWhite = 10;

// iniciamos los pins de los 5 botones
const int pushbuttonRed = 7;
const int pushbuttonBlue = 6;
const int pushbuttonGreen = 5;
const int pushbuttonWhite = 4;
const int pushbuttonEnviar = 3;

// Declaramos el estado de los 4 leds de sender y reciver
int ledStateRed = 0;
int ledStateBlue = 0;
int ledStateGreen = 0;
int ledStateWhite = 0;

char c;
char leds[7] = {'H', '0', '0', '0', '0', 'F', '\0'};

//String cadena_final = "";
SoftwareSerial sender =  SoftwareSerial(rxPin, txPin);

void setup() {
  pinMode(ledRed, OUTPUT);
  pinMode(ledBlue, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledWhite, OUTPUT);
  pinMode(pushbuttonRed, INPUT);
  pinMode(pushbuttonBlue, INPUT);
  pinMode(pushbuttonGreen, INPUT);
  pinMode(pushbuttonWhite, INPUT);
  pinMode(pushbuttonEnviar, INPUT);
  sender.begin(19200);
  DEBUG_INTERFACE.begin(9600);
  DEBUG_INTERFACE.println("DEBUG ON");
}

void loop() {

  // Boton Rojo
  if (digitalRead(pushbuttonRed) == 1) {
    digitalWrite(ledRed, HIGH);
    ledStateRed += 1;
    leds[1] = '1';
    if (ledStateRed % 2 == 0) {
      digitalWrite(ledRed, LOW);
      leds[1] = '0';
      ledStateRed = 0;
    }
  }

  // Boton Azul
  if (digitalRead(pushbuttonBlue) == 1) {
    digitalWrite(ledBlue, HIGH);
    ledStateBlue += 1;
    leds[2] = '1';
    if (ledStateBlue % 2 == 0) {
      digitalWrite(ledBlue, LOW);
      ledStateBlue = 0;
      leds[2] = '0';
    }
  }

  // Boton Verde
  if (digitalRead(pushbuttonGreen) == 1) {
    digitalWrite(ledGreen, HIGH);
    ledStateGreen += 1;
    leds[3] = '1';
    if (ledStateGreen % 2 == 0) {
      digitalWrite(ledGreen, LOW);
      ledStateGreen = 0;
      leds[3] = '0';
    }
  }

  // Boton Blanco
  if (digitalRead(pushbuttonWhite) == 1) {
    digitalWrite(ledWhite, HIGH);
    ledStateWhite += 1;
    leds[4] = '1';
    if (ledStateWhite % 2 == 0) {
      digitalWrite(ledWhite, LOW);
      ledStateWhite = 0;
      leds[4] = '0';
    }
  }

  // Boton Enviar
  if (digitalRead(pushbuttonEnviar) == 1) switchAll();

    if (millis() - startTime > intervalTime) {
    DEBUG_INTERFACE.println(leds);
    sender.println(leds);
    startTime = millis();
  }
}

void switchAll() {
  // Reinicio los leds de sender para enviar otro mensaje
  ledStateWhite = 0;
  ledStateGreen = 0;
  ledStateBlue = 0;
  ledStateRed  = 0;
  digitalWrite(ledWhite, LOW);
  digitalWrite(ledRed, LOW);
  digitalWrite(ledBlue, LOW);
  digitalWrite(ledGreen, LOW);
  DEBUG_INTERFACE.println(leds);
  leds[1] = '0';
  leds[2] = '0';
  leds[3] = '0';
  leds[4] = '0';
}

and the code for RECEIVER:

// Iniciamos los pins de los 4 leds
const int Red = 4;
const int Blue = 5;
const int Green = 6;
const int White = 7;

// Creamos el boton que limpia pantalla
const int buttonClear = 10;

char rc;
const int maxChar = 6;
static byte charCount = 0;
char leds2[7] = {'\0'};
bool newData = false;

#define DEBUG_INTERFACE Serial
#define receiver Serial

void setup() {
  receiver.begin(9600);
  pinMode(Red, OUTPUT);
  pinMode(Blue, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(White, OUTPUT);

  DEBUG_INTERFACE.begin(9600);
  DEBUG_INTERFACE.println("DEBUG ON");
}

void loop() {
  receiveStartEndMarkers();
  if (newData) {
    DEBUG_INTERFACE.print(" LED Array received: ");
    DEBUG_INTERFACE.println(leds2);
    lightLeds(leds2);
    newData = false;
  }
}

void lightLeds(char A[]) {
  for (int i = 0; i < 6; i++) {
    if (A[1] == '1' || A[1] == 1) digitalWrite(Red, HIGH);
    if (A[2] == '1' || A[2] == 1) digitalWrite(Blue, HIGH);
    if (A[3] == '1' || A[3] == 1) digitalWrite(Green, HIGH);
    if (A[4] == '1' || A[4] == 1) digitalWrite(White, HIGH);
    if (A[1] == '0' || A[1] == 0) digitalWrite(Red, LOW);
    if (A[2] == '0' || A[2] == 0) digitalWrite(Blue, LOW);
    if (A[3] == '0' || A[3] == 0) digitalWrite(Green, LOW);
    if (A[4] == '0' || A[4] == 0) digitalWrite(White, LOW);
  }
  DEBUG_INTERFACE.print(A); DEBUG_INTERFACE.println(" Leds switched");
}

void receiveStartEndMarkers() {
  static boolean recvInProgress = false;
  char startMarker = 'H';
  char endMarker = 'F';

  if (receiver.available() > 0) {
    rc = receiver.read();
    //DEBUG_INTERFACE.print(rc);
    if (recvInProgress) {
      if (rc != endMarker) {
        leds2[charCount] = rc;
        charCount++;
        if (charCount > maxChar) {
          charCount = maxChar;
        }
      }
      else {
        leds2[charCount] = rc;
        charCount++;
        leds2[charCount] = '\0'; // terminate the string
        recvInProgress = false;
        charCount = 0;
        newData = true;
      }
    }
    else if (rc == startMarker) {
      recvInProgress = true;
      leds2[charCount] = rc;
      charCount++;
    }
  }
}

Working code looks like this - change to HW-Serial 0(RX), 1(TX) to 1(TX), 0(RX):
enter image description here So as a result DO NOT USE SoftwareSerial.h in TinkerCAD - its not working at the moment, use HW serial for communication examples or you will get crazy if working code suddenly outputs garbagge. When using real hardware use AltSoftSerial (full duplex mode). If you plan to use your design with real hardware use resistors for the LEDs (search for resistor calculator LED) as different colored leds need different resistor values.

| improve this answer | |
  • I've added all of the suggested changes, still the problem persists, the lights from the second arduino will not light up. Also i cannot find a continuation in the comments as you suggest in the end of your post. – LuiGiovanni May 13 at 9:25
  • There, the code has been edited – LuiGiovanni May 13 at 9:34
  • Sure, here is the tinkercad link: tinkercad.com/things/37CXjQkn6Bi-copy-of-4-buttons-4-leds/… – LuiGiovanni May 13 at 10:02
  • Riddle solved see my edit above example under your link is fully working now-have fun and if ok mark as solved Thanks – Codebreaker007 May 13 at 16:33
  • I'm sorry, it seems that both receiver and sender are the same code. – LuiGiovanni May 13 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.