Sending a value on an arduino via another arduino and unity.

Ok, here is my problem. I have a setup where i use an arduino mega, an arduino uno and unity 2019.2.1f1 in order to control 2 LEDS. I have the arduino mega connected at COM6 and at 9600 baud rate. I want to press a key in unity, and send a specific character to the arduino mega, and then for the mega to transfer the value to the uno, and act accordingly. In my case, i have unity send w,a,s or d when one of these buttons gets pressed, on the mega. Then, i want the mega to send the same value to the uno board, and then light an LED. The problem is that it doesnt work and i need help. If you are confused, i will post the code.

Unity code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;

public class SerialTest : MonoBehaviour
{
   public SerialPort SP = new SerialPort("COM6", 9600);

 private void Start()
 {
 SP.Open();
 SP.ReadTimeout = 16;
 }

 private void Update()
 {
 if (Input.GetKeyDown(KeyCode.W))
 {
 SP.Write("w");
 }
 else if (Input.GetKeyDown(KeyCode.A))
 {
 SP.Write("a");
 }
 else if (Input.GetKeyDown(KeyCode.S))
 {
 SP.Write("s");

 }
 else if (Input.GetKeyDown(KeyCode.D))
 {
 SP.Write("d");
 }
 }
}

Arduino Mega Code:

#include <SoftwareSerial.h>

SoftwareSerial softSerial(8, 9);  // RX, TX 
const int FirstPin = 2;
const int LastPin = 25;
const int URX = 8;
const int UTX = 9;
char myCol[20];

String Poses;

int Test;

void setup()  {
  Serial.begin(9600); 
  softSerial.begin(9600);
  Poses = "";
  Test = 0;

  for(int i = FirstPin; i <= LastPin; i++)
  {
    if(i != URX || i != UTX || i != 20 || i != 21)
    {
     pinMode(i, INPUT); 
    }
    else
    {
      continue;
    }
  }
} 

void CheckPawns()
{
  for(int y = FirstPin; y <= LastPin; y++)
  {
   if(y == 13 || y == 8 || y == 9 || y == 20 || y == 21)
    {
     Poses += "";
    }
    else
    {
     int state;
     state = digitalRead(y);
     if(state == HIGH)
     {
       Poses += "1"; 
     }
     else
     {
       Poses += "0";
     }
    }
  }
}
void loop()  { 
  CheckPawns();
  //Serial.println(Poses);
  Poses = "";
  if(Serial.available())
  {  
  int lf = 10;
   Serial.readBytesUntil(lf, myCol, 1);
   
    if(strcmp(myCol,"w")==0){
       softSerial.write('w');
   }
  if(strcmp(myCol,"a")==0){  
      softSerial.write('a');
   }
   if(strcmp(myCol,"s")==0){  
       softSerial.write('s');
   }
   if(strcmp(myCol,"d")==0){  
       softSerial.write('d');
   }
   
  }
  delay(1000);
}

Arduino Uno Code:

#include <SoftwareSerial.h>

SoftwareSerial softSerial(8, 9);
int LedA = 7;
int LedB = 6;
char myCol[20];

void setup() {  
   Serial.begin (9600);
   softSerial.begin(9600);  
   pinMode(LedA, OUTPUT);    
   pinMode(LedB, OUTPUT);  
   digitalWrite(LedA, LOW);
   digitalWrite(LedB, LOW);
}


void loop() {
  if(softSerial.available())
  {
    int Test = softSerial.read();
    if(Test == 'w'){
       digitalWrite(LedA, LOW);
       digitalWrite(LedB, HIGH);
   }
   else if(Test == 'a'){  
       digitalWrite(LedA, HIGH);
       digitalWrite(LedB, HIGH); 
   }
   else if(Test == 's'){  
       digitalWrite(LedA, HIGH);
       digitalWrite(LedB, LOW); 
   }
   else if(Test == 'd'){  
       digitalWrite(LedA, LOW);
       digitalWrite(LedB, LOW); 
   }
  } 
}

Any help would be appreciated.

  1. It is not clear to me what this accomplishes:
     Poses += "";
  1. String is usually deprecated for Arduinos. c-strings may be safer.

  2. You need to determine whether your problem is from Unity to the Mega or from the Mega to the Uno. Serial.println(...) may help with that. Using the output pins can also be helpful.

Hello Mikaelgameryolo,
Welcome to the forum.
For posting your code correctly on your first post ++Karma;

I'm going to be of limited help, but here goes.

I know nothing of Unity. Never heard of it until I saw your post. Do you know Unity well? Is it safe to assume that the Unity code is sending what you think it is sending to the serial port?

What is your Arduino and C or C++ experience? Reading your Arduino code I get the impression that you know some other language but not C++ and are trying to adapt the other language to C++ without really understanding what works in C++. Your set up code on the Mega is way too complicated.

You need to break the problem down. If the Unity system is sending to the Mega then put code on to the Mega to send the data to the serial monitor. Only when that is right should you try sending it to the Uno.

How familiar are you with using serial on an Arduino? Have you worked through Serial basics ?

Why are you even doing this in such a complicated way? Why 3 devices at all? What do you gain by sending to a Uno via a Mega rather than directly to the Uno?

PerryBebbington:
Hello Mikaelgameryolo,
Welcome to the forum.
For posting your code correctly on your first post ++Karma;

I'm going to be of limited help, but here goes.

I know nothing of Unity. Never heard of it until I saw your post. Do you know Unity well? Is it safe to assume that the Unity code is sending what you think it is sending to the serial port?

What is your Arduino and C or C++ experience? Reading your Arduino code I get the impression that you know some other language but not C++ and are trying to adapt the other language to C++ without really understanding what works in C++. Your set up code on the Mega is way too complicated.

You need to break the problem down. If the Unity system is sending to the Mega then put code on to the Mega to send the data to the serial monitor. Only when that is right should you try sending it to the Uno.

How familiar are you with using serial on an Arduino? Have you worked through Serial basics ?

Why are you even doing this in such a complicated way? Why 3 devices at all? What do you gain by sending to a Uno via a Mega rather than directly to the Uno?

Ok, so, i need to give some background to help. I have a 1-year experience with Arduino, and 3 years with c#, the language that i used in unity. I am 16 years old, with no academic education yet, but i am following this path. My goal is for school, i am making a project around the theme of chess, and i have decided to make a robot that will play chess with a player. So, now that the background has been given, i will break down the code.

I will explain shortly what i have come up with. So, in chess, there is an 8x8 grid, with white and black pieces. My plan is to make each position on that grid act like a switch, and using a piece, i can make current flow through that square, completing a circuit which tells me that there is a piece there, for example at H7. So, i have placed a mega with 64 inputs that checks each square, and if there is a piece in a square, that means that current flows there, so it returns a 1. If not, it returns a 0. Then, i add all 64 values on a string, which then i pass in unity(The Poses String in the arduino code). Then, unity handles the logic and everything and moves a piece on a computer drawn chess set(Not the script that i have provided). Then, it passes a value on the mega, and then the mega passes it to the uno, that makes the move(with stepper motors ect, but that is not relevant).

The script that i have provided using unity doesnt do anything else than just giving a character, like w,a,s or d. Then, the provided arduino script for the mega reads the character, and sends the same character to the uno. Then, the uno acts accordingly to the third provided script.My problem is that, for some reason, that does not happen, and i have been baffled with this problem for almost a week now, so i tried to ask some professionals for help. So, if you can help, i would appreciate it.

Ok, so, i need to give some background to help.

Thank you for the background.

Now please address the points and suggestions made by vaj4088 and myself.

My problem is that, for some reason, that does not happen, and i have been baffled with this problem for almost a week now.

You should in that case have plenty to tell us about what you have tried, what happened when you did and what specifically you are stuck with.

At the moment you have used a lot of words to tell us very little about your own attempts to solve the problem, which comes across to us as 'I can't do this will you do it for me'. That might sound unfair but it is the impression I am getting. Please act on the advice already given and report what you learned, then you will most likely get more help.

I tried to ask some professionals for help

Just to avoid misunderstandings, we are here as amateurs trying to help out as volunteers. Sure, there are many here who work in electronics and computers one way or another, but also many for whom this is a hobby and whose day jobs are quite different.

PerryBebbington:
Thank you for the background.

Now please address the points and suggestions made by vaj4088 and myself.

You should in that case have plenty to tell us about what you have tried, what happened when you did and what specifically you are stuck with.

At the moment you have used a lot of words to tell us very little about your own attempts to solve the problem, which comes across to us as 'I can't do this will you do it for me'. That might sound unfair but it is the impression I am getting. Please act on the advice already given and report what you learned, then you will most likely get more help.

Alright, i will do my best. Currently, as far as i know, the problem lies between the mega and uno communication. By far, i have tried to use the mega to send the value over, as with the scripts provided. What the given scripts do, if you remove the pieces check by the mega, is making unity check if the user has pressed W,A,S or D, and if so, it will send the according letter through the mega port. For example, if you press A, unity will send "a" to the mega board. Then, the arduino mega detects that, and sends the same thing over to uno, or thats what it is supposed to be doing. So, in this example, if you have pressed "a", the uno should recieve an "a" and light both LEDs. Note that the LEDs act as a testing method. Another solution i have come up with, although it is a bit bulky, is to connect both arduinos to the computer under different ports. So, in my case, unity will send and recieve data from the COM6 port(mega), and only send data at COM3 port.

By far, i have only tried the first solution, with no success. Although, everytime i press W,A,S,D, the orange light on the mega board lights up, and that could indicate that the data gets transferred, but not recieved.
I should also mention that the mega and uno are connected with UART communication. So, could switching over to I2C fix the problem, or is that incorrect?

Also, to answer the question of why i am making this with 3 devices, it is because i want the mega to make the pieces check, and the uno to move the stepper motors to actually move the piece. As for my c and c++ experience, i dont have much, if any, i have only used c++ with the arduino IDE. And my familiarity with Serial is limited, but i have worked through the basics.

I hope that answers all of the questions above, and hopefully helped you understand my situation better. And this might sound offensive, but i dont mean it like that, but if you are still unable to help, no worries, i can retry another day.

You need to connect the Mega and the Uno to your PC on separate USB ports and use the serial monitor to see what is being received by each. If you don't know how to do this then start by trying the examples in the IDE, they show you how. You need to move from vague statements like:

As far as i know, the problem lies between the mega and Uno communication

To knowing for certain where the problem lies. Using the serial monitor to print what the Maga receives will tell you for certain if it has or has not received what you hope it has received, then you will know where to look.

And this might sound offensive, but i dont mean it like that, but if you are still unable to help, no worries, i can retry another day.

I'm not offended, but I am finding helping you frustrating as it's difficult to get relevant information from you and you don't seem to want to try what is suggested.

PerryBebbington:
You need to connect the Mega and the Uno to your PC on separate USB ports and use the serial monitor to see what is being received by each. If you don't know how to do this then start by trying the examples in the IDE, they show you how. You need to move from vague statements like:
To knowing for certain where the problem lies. Using the serial monitor to print what the Maga receives will tell you for certain if it has or has not received what you hope it has received, then you will know where to look.

I'm not offended, but I am finding helping you frustrating as it's difficult to get relevant information from you and you don't seem to want to try what is suggested.

Alright, that sounds like a solid plan. However, i cant check what the mega recieves with printing it, because i cant have both unity and the COM serial monitor open. I have come up with a possible solution. I could hook up the mega with 2 leds and say that if the desired value has been received, the first led will light, and if the same value has been sent, the second one will light. Then, i could connect the uno to COM3, and see from there. I am going to do this tomorrow morning, because it is late night over here. Also, i am very sorry to frustrate you, and i appreciate the fact that you havent given up yet. I will try my best to help.

PerryBebbington:
You need to connect the Mega and the Uno to your PC on separate USB ports and use the serial monitor to see what is being received by each. If you don't know how to do this then start by trying the examples in the IDE, they show you how. You need to move from vague statements like:
To knowing for certain where the problem lies. Using the serial monitor to print what the Maga receives will tell you for certain if it has or has not received what you hope it has received, then you will know where to look.

I'm not offended, but I am finding helping you frustrating as it's difficult to get relevant information from you and you don't seem to want to try what is suggested.

Ok, i dont know how to quote something specific from a reply, so i am just going to rewrite it.

"You need to move from vague statements like:[My statement] to knowing for certain where the problem lies."

So, after some testing, i spotted the problem. To find it, i connected 2 leds on the mega on pins 40 and 41, and i said that if you press the A key, send 'a' through softSerial.write('a'), and light one of the LEDS. I did the rest for the others, with each time doing something diferent, so i can tell. That test turned out successful, so the mega is indeed sending the right characters.

So, i did the same for the uno, but with a slight change. Instead of lighting an LED if the character is being recieved, i set it to blink once if softSerial is available. When i booted and sent a character though, the led did not blink. That means that the problem is that softSerial is not available when the uno starts, and thats why it does not recieve anything. I am trying to resolve the problem currently.

Edit: Ok, here is the problem:
After some research, i found out that with the usage of the SoftwareSerial library, i cannot recieve and transmit data at the same time, which is what i am trying to do. My other two options are to
a)Change the library
b)Connect the mega and uno seperately
I am probably going to go with b, because the other library that i can use, AltSoftSerial, seems way too complex for my level, as i saw from the examples.

I don't know how to quote something specific from a reply, so i am just going to rewrite it.

Copy and paste the text you want, select it and click the 'insert a quote' icon, the one that looks like a speech balloon 6 from the right above the editing window.

Thank you for the update, glad you are making progress.