Communication between visual studio and arduino: Unable to print the recived string in the LCD when pressed a button

28 views Asked by At

I have an issue realted to the connection between visual studio and Arduino. The point of my project is that the user is available to write 16 diffrent text on a visual studio windows form. And then this 16 text save it into a user, after the port is connected to the visual stduio, and select button is clicked, it should send the 16 diffrent messages to a respective input string, so after the user presses a button in the keyboard, it should say in a lcd 1602 module what is saved in the input string. The communication between visual studio and arduino works just fine, although I am still unavailable to do two things:

  1. Send 16 diffrent messages: I can send 1 message, but when it gets to sexteen, arduino for some reason just print me the last message send to the LCD 1602 module.

  2. The buttons are not working: I tried to print a random text on the lcd when a button is pressed, and it worked just fine. But when i try to do it with the input string this one is not working correctly, and it does not print anything.

Here are my codes:

Visual studio:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComputerScienceAIForm
{
    public partial class UserLogIn : Form
    {

        MethodsForRecord MFR = new MethodsForRecord();
        
        bool isConnected = false;
        String[] ports;
        SerialPort port;


        public UserLogIn()
        {
            if (Properties.Settings.Default.GlobalLanguage == "English")
            {
                Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
                InitializeComponent();
            }
            else if (Properties.Settings.Default.GlobalLanguage == "Spanish")
            {
                Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("es");
                InitializeComponent();
            }
            else if (Properties.Settings.Default.GlobalLanguage == "French")
            {
                Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr");
                InitializeComponent();
            }
            else
            {
                Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
                InitializeComponent();
            }
            MFR.createArr();

            getAvailableComPorts();

            foreach (string port in ports)
            {
                COMComboBox.Items.Add(port);
                Console.WriteLine(port);
                if (ports[0] != null)
                {
                    COMComboBox.SelectedItem = ports[0];
                }
            }
            Safe.Enabled = false;
        }

        void getAvailableComPorts()
        {
            ports = SerialPort.GetPortNames();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 Main = new Form1();
            Main.Show();
            disconnectFromArduino();
            this.Hide();
        }

            
        }
        private void button2_Click(object sender, EventArgs e)
        
            if (isConnected)
            {
                if (Properties.Settings.Default.RespectiveUser.Equals(Properties.Settings.Default.UserName1))
                {
                    port.Write("#TEXT" + Properties.Settings.Default.U11 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U12 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U13 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U14 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U15 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U16 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U17 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U18 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U19 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U110 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U111 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U112 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U113 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U114 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U115 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U116 + "#\n");
                } else if (Properties.Settings.Default.RespectiveUser.Equals(Properties.Settings.Default.UserName2))
                {
                    port.Write("#TEXT" + Properties.Settings.Default.U21 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U22 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U23 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U24 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U25 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U26 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U27 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U28 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U29 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U210 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U211 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U212 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U213 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U214 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U215 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U216 + "#\n");
                } else if (Properties.Settings.Default.RespectiveUser.Equals(Properties.Settings.Default.UserName3))
                {
                    port.Write("#TEXT" + Properties.Settings.Default.U31 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U32 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U33 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U34 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U35 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U36 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U37 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U38 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U39 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U310 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U311 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U312 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U313 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U314 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U315 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U316 + "#\n");
                } else if (Properties.Settings.Default.RespectiveUser.Equals(Properties.Settings.Default.UserName4))
                {
                    port.Write("#TEXT" + Properties.Settings.Default.U41 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U42 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U43 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U44 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U45 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U46 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U47 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U48 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U49 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U410 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U411 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U412 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U413 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U414 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U415 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U416 + "#\n");
                } else if (Properties.Settings.Default.RespectiveUser.Equals(Properties.Settings.Default.UserName5))
                {
                    port.Write("#TEXT" + Properties.Settings.Default.U51 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U52 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U53 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U54 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U55 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U56 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U57 +"#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U58 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U59 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U510 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U511 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U512 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U513 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U514 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U515 + "#\n");
                    port.Write("#TEXT" + Properties.Settings.Default.U516 + "#\n");
                }
                else
                {
                    MessageBox.Show("Please select a User After Selecting");
                }
                
            }
        }

        private void ConnectB_Click(object sender, EventArgs e)
        {
            if (!isConnected)
            {
                connectToArudino();
            }
            else
            {
                disconnectFromArduino();
            }
        }

        private void connectToArudino()
        {
            try
            {
                isConnected = true;
                string selectedPort = COMComboBox.GetItemText(COMComboBox.SelectedItem);
                port = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One);
                port.Open();
                port.Write("#STAR\n");
                ConnectB.Text = "Disconnected";
                Safe.Enabled = true;            
            }
            catch (Exception err)
            {
                MessageBox.Show("An error has Ocurred: " + err.Message);
            }
            
        }

        private void disconnectFromArduino()
        {
            try
            {
                isConnected = false;
                port.Write("#STOP\n");
                port.Close();
                ConnectB.Text = "Connected";
                Safe.Enabled = false;
            } catch (Exception err)
            {
                MessageBox.Show("Error on disconnecting from Arduino: " + err.Message);
            }
        }

    }

}

Arduino Code:

#include <Wire.h>
#include <Keypad.h>
#include "Arduino.h"
#include "SoftwareSerial.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd (0x27, 16, 2);

String INPUTSTRINGS[16];
boolean StringComplete = false;
String COMMANDSTRINGS [16];

boolean isConnected = false;
const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
  {'4','3','2','1'},
  {'8','7','6','5'},
  {'C','B','A','9'},
  {'G','F','E','D'}
};

byte rowPins[ROWS] = {5,4,3,2}; 
byte colPins[COLS] = {9,8,7,6}; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
}

void loop() {
   if (StringComplete){
     StringComplete = false;
     getCommand();
char key = keypad.getKey();
     if (COMMANDSTRINGS[0].equals("STAR")){
       lcd.clear();
     }
     if (COMMANDSTRINGS[0].equals("STOP")){
       lcd.clear();
       lcd.print("Ready to Connect");
     }
  else if (key =='4'){
     if (COMMANDSTRINGS[0].equals("TEXT")){
       String text = getTextToPrint();
       printText (text);
     } else{
       lcd.print("null text");
     }
 
  }
  else if (key =='3'){
    
    if (COMMANDSTRINGS[1].equals("TEXT")){
       String text = getTextToPrint2();
       printText (text);
     } else{
       lcd.print("null text");
     }
 
  }
  else if (key =='2'){
    
     if (COMMANDSTRINGS[2].equals("TEXT")){
       String text = getTextToPrint3();
       printText (text);
     } else{
       lcd.print("null text");
     }

  }

  else if (key =='1'){
   if (COMMANDSTRINGS[3].equals("TEXT")){
       String text = getTextToPrint4();
       printText (text);
     } else{
       lcd.print("null text");
     }
    
  }

   else if (key == '8'){
    
     if (COMMANDSTRINGS[4].equals("TEXT")){
       String text = getTextToPrint5();
       printText (text);
     } else{
       lcd.print("null text");
     }
  }

  else if (key == '7'){
    
     if (COMMANDSTRINGS[5].equals("TEXT")){
       String text = getTextToPrint6();
       printText (text);
     } else{
       lcd.print("null text");
     }

  }
  else if (key =='6'){
    if (COMMANDSTRINGS[6].equals("TEXT")){
       String text = getTextToPrint7();
       printText (text);
     } else{
       lcd.print("null text");
     }
  

  }
  else if (key =='5'){
      if (COMMANDSTRINGS[7].equals("TEXT")){
       String text = getTextToPrint8();
       printText (text);
     } else{
       lcd.print("null text");
     }
   
  }

  
  else if (key =='C'){
      if (COMMANDSTRINGS[8].equals("TEXT")){
       String text = getTextToPrint9();
       printText (text);
     } else{
       lcd.print("null text");
     }
   
  }

  
  else if (key =='B'){
      if (COMMANDSTRINGS[9].equals("TEXT")){
       String text = getTextToPrint10();
       printText (text);
     } else{
       lcd.print("null text");
     }
  
  }

  
  else if (key =='A'){
      if (COMMANDSTRINGS[10].equals("TEXT")){
       String text = getTextToPrint11();
       printText (text);
     } else{
       lcd.print("null text");
     }
   
  }

  
  else if (key =='9'){
      if (COMMANDSTRINGS[11].equals("TEXT")){
       String text = getTextToPrint12();
       printText (text);
     } else{
       lcd.print("null text");
     }
   
  }

   else if (key =='G'){
      if (COMMANDSTRINGS[12].equals("TEXT")){
       String text = getTextToPrint13();
       printText (text);
     } else{
       lcd.print("null text");
     }
   
  }

   else if (key =='F'){
      if (COMMANDSTRINGS[13].equals("TEXT")){
       String text = getTextToPrint14();
       printText (text);
     } else{
       lcd.print("null text");
     }
   
  }

   else if (key =='E'){
      if (COMMANDSTRINGS[14].equals("TEXT")){
       String text = getTextToPrint15();
       printText (text);
     } else{
       lcd.print("null text");
     }
   
  }

   else if (key =='D'){
     if (COMMANDSTRINGS[15].equals("TEXT")){
       String text = getTextToPrint16();
       printText (text);
     } else{
       lcd.print("null text");
     }
   } 
  
  }
     
}

String getTextToPrint(){
  String value = INPUTSTRINGS[0].substring(5, INPUTSTRINGS[0].length()-2);
  return value;
}

String getTextToPrint2(){
  String value = INPUTSTRINGS[1].substring(5, INPUTSTRINGS[1].length()-2);
  return value;
}

String getTextToPrint3(){
  String value = INPUTSTRINGS[2].substring(5, INPUTSTRINGS[2].length()-2);
  return value;
}

String getTextToPrint4(){
  String value = INPUTSTRINGS[3].substring(5, INPUTSTRINGS[3].length()-2);
  return value;
}

String getTextToPrint5(){
  String value = INPUTSTRINGS[4].substring(5, INPUTSTRINGS[4].length()-2);
  return value;
}

String getTextToPrint6(){
  String value = INPUTSTRINGS[5].substring(5, INPUTSTRINGS[5].length()-2);
  return value;
}

String getTextToPrint7(){
  String value = INPUTSTRINGS[6].substring(5, INPUTSTRINGS[6].length()-2);
  return value;
}

String getTextToPrint8(){
  String value = INPUTSTRINGS[7].substring(5, INPUTSTRINGS[7].length()-2);
  return value;
}

String getTextToPrint9(){
  String value = INPUTSTRINGS[8].substring(5, INPUTSTRINGS[8].length()-2);
  return value;
}

String getTextToPrint10(){
  String value = INPUTSTRINGS[9].substring(5, INPUTSTRINGS[9].length()-2);
  return value;
}

String getTextToPrint16(){
  String value = INPUTSTRINGS[15].substring(5, INPUTSTRINGS[15].length()-2);
  return value;
}

String getTextToPrint11(){
  String value = INPUTSTRINGS[10].substring(5, INPUTSTRINGS[10].length()-2);
  return value;
}

String getTextToPrint12(){
  String value = INPUTSTRINGS[11].substring(5, INPUTSTRINGS[11].length()-2);
  return value;
}

String getTextToPrint13(){
  String value = INPUTSTRINGS[12].substring(5, INPUTSTRINGS[12].length()-2);
  return value;
}

String getTextToPrint14(){
  String value = INPUTSTRINGS[13].substring(5, INPUTSTRINGS[13].length()-2);
  return value;
}

String getTextToPrint15(){
  String value = INPUTSTRINGS[14].substring(5, INPUTSTRINGS[14].length()-2);
  return value;
}

void serialEvent(){
  while(Serial.available()){
    char inChar = (char)Serial.read();
    INPUTSTRINGS[0] += inChar;

    if (inChar == '\n'){
      StringComplete = true;
    }
  }
}

void getCommand()
{
  if(INPUTSTRINGS[0].length()>0)
  {
     COMMANDSTRINGS[0] = INPUTSTRINGS[0].substring(1,5);
  } if (INPUTSTRINGS[1].length()>0){
     COMMANDSTRINGS[1] = INPUTSTRINGS[1].substring(1,5);
  }if (INPUTSTRINGS[2].length()>0){
     COMMANDSTRINGS[2] = INPUTSTRINGS[2].substring(1,5);
     } if (INPUTSTRINGS[3].length()>0){
     COMMANDSTRINGS[3] = INPUTSTRINGS[3].substring(1,5);
     }if (INPUTSTRINGS[4].length()>0){
     COMMANDSTRINGS[4] = INPUTSTRINGS[4].substring(1,5);
     }if (INPUTSTRINGS[5].length()>0){
     COMMANDSTRINGS[5] = INPUTSTRINGS[5].substring(1,5);
     }if (INPUTSTRINGS[6].length()>0){
     COMMANDSTRINGS[6] = INPUTSTRINGS[6].substring(1,5);
     }if (INPUTSTRINGS[7].length()>0){
     COMMANDSTRINGS[7] = INPUTSTRINGS[7].substring(1,5);
     }if (INPUTSTRINGS[8].length()>0){
     COMMANDSTRINGS[8] = INPUTSTRINGS[8].substring(1,5);
     }if (INPUTSTRINGS[9].length()>0){
     COMMANDSTRINGS[9] = INPUTSTRINGS[9].substring(1,5);
     }if (INPUTSTRINGS[10].length()>0){
     COMMANDSTRINGS[10] = INPUTSTRINGS[10].substring(1,5);
     }if (INPUTSTRINGS[11].length()>0){
     COMMANDSTRINGS[11] = INPUTSTRINGS[11].substring(1,5);
     }if (INPUTSTRINGS[12].length()>0){
     COMMANDSTRINGS[12] = INPUTSTRINGS[12].substring(1,5);
     }if (INPUTSTRINGS[13].length()>0){
     COMMANDSTRINGS[13] = INPUTSTRINGS[13].substring(1,5);
     }if (INPUTSTRINGS[14].length()>0){
     COMMANDSTRINGS[14] = INPUTSTRINGS[14].substring(1,5);
     }if (INPUTSTRINGS[15].length()>0){
     COMMANDSTRINGS[15] = INPUTSTRINGS[15].substring(1,5);
     }
}

void printText(String text)
{
  lcd.clear();
  lcd.setCursor(0,0);
    if(text.length()<16)
    {
      lcd.print(text);
    }else
    {
      lcd.print(text.substring(0,16));
      lcd.setCursor(0,1);
      lcd.print(text.substring(16,32));
    } 
}

At the end, it should print in the lcd the respective message saved in each individual button. I have provided here the code which works correctly the communication, the main issue is that the message is printed immidiatly, and as well, it only prints one message instead of the respective 16:

#include <Wire.h>
#include <Keypad.h>
#include "Arduino.h"
#include "SoftwareSerial.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd (0x27, 16, 2);

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
String commandString = "";


boolean isConnected = false;
const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
  {'4','3','2','1'},
  {'8','7','6','5'},
  {'C','B','A','9'},
  {'G','F','E','D'}
};

byte rowPins[ROWS] = {5,4,3,2}; 
byte colPins[COLS] = {9,8,7,6}; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
}

void loop() {



char key = keypad.getKey();

if(stringComplete)
{
  stringComplete = false;
  getCommand();
  
  if(commandString.equals("STAR"))
  {
    lcd.clear();
  }
  if(commandString.equals("STOP"))
  {
    lcd.clear();
    lcd.print("Ready to Connect");    
  }
  else if(commandString.equals("TEXT"))
  {
    String text = getTextToPrint();
    printText(text);
  }
  
  inputString = "";
}

}


void getCommand()
{
  if(inputString.length()>0)
  {
     commandString = inputString.substring(1,5);
  }
}

String getTextToPrint()
{
  String value = inputString.substring(5,inputString.length()-2);
  return value;
}

void printText(String text)
{
  lcd.clear();
  lcd.setCursor(0,0);
    if(text.length()<16)
    {
      lcd.print(text);
    }else
    {
      lcd.print(text.substring(0,16));
      lcd.setCursor(0,1);
      lcd.print(text.substring(16,32));
    }
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}


0

There are 0 answers