Serial communication problems 2

Hi guys, I would like to ask if you can do a multi communication
with arduino one, or other. I'm trying to check a couple of relays
with app inventor but at a time send data with mySerial.write (val);
to cellular, receive commands with keys always from the cell phone with
com = Serial.read (); and retrieve data from a potentiometer with
analogread (A5) depending on the value turns each relay on or off.
To say mySerial works for me but Serial does not and on the command keys
with reading com = Serial.read (); from errors, etc. i included the program
Thank you very much

#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
char com;
void setup() {
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
mySerial.begin(9600);
Serial.begin(9600);
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
}
void loop() {
byte val = analogRead(A5);
if( Serial.available() ) {
if (val > 150){
digitalWrite(2, LOW);
delay(100);
digitalWrite(4, HIGH);
}
if (val < 150) {
digitalWrite(4, LOW);
delay(100);
digitalWrite(2, HIGH);
}
if (val == 150) {
digitalWrite(2, HIGH);
delay(100);
digitalWrite(4, HIGH);
}
if( mySerial.available() ) {
com = mySerial.read();
if (com == 'J') {
digitalWrite(2, LOW);
}
if (com == 'M') {
digitalWrite(2, HIGH);
}
if (com == 't') {
digitalWrite(4, LOW);
}
if (com == 'u') {
digitalWrite(4, HIGH);
}
}
Serial.write(val);
delay(1000);
}
}

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

Here is your code posted in code tags and indented with autoformat. Notice that the software serial port is only checked if Serial.available() returns true. Is this the logic that you expect?

#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
char com;
void setup()
{
   pinMode(2, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
   mySerial.begin(9600);
   Serial.begin(9600);
   digitalWrite(2, HIGH);
   digitalWrite(4, HIGH);
}
void loop()
{
   byte val = analogRead(A5);
   if ( Serial.available() )
   {
      if (val > 150)
      {
         digitalWrite(2, LOW);
         delay(100);
         digitalWrite(4, HIGH);
      }
      if (val < 150)
      {
         digitalWrite(4, LOW);
         delay(100);
         digitalWrite(2, HIGH);
      }
      if (val == 150)
      {
         digitalWrite(2, HIGH);
         delay(100);
         digitalWrite(4, HIGH);
      }
      if ( mySerial.available() )
      {
         com = mySerial.read();
         if (com == ‘J’)
         {
            digitalWrite(2, LOW);
         }
         if (com == ‘M’)
         {
            digitalWrite(2, HIGH);
         }
         if (com == ‘t’)
         {
            digitalWrite(4, LOW);
         }
         if (com == ‘u’)
         {
            digitalWrite(4, HIGH);
         }
      }
      Serial.write(val);
      delay(1000);
   }
}

Also this line:

SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);

should be

SoftwareSerial mySerial(rxPin, txPin);

and you do not need to set the pinModes for RX and TX are set with the begin function and do not need to be set by the user.

My tutorial on Arduino to Arduino/PC via Serial has complete sketches and circuits for connecting two Arduinos together via Serial.
The code does auto connect/reconnect and send/receive is synced so it works well with Software Serial which cannot send/receive at the same time.
Also if you limit you messages to <=60 chars the code is very tolerant of very long delays in either loop. (Not that you should be using delays( ) but in any case)

Thank you so much for your availability. Serial.available I needed to enable the relay with analogRead but it doesn't work so I use this condition if myseria.available () otherwise without this condition the relay was always active. But at a time it makes me problem with the "com" that manually activates the relays with the keys of the mobile phone but it works badly. If I make two different programs for one byte side val = analogRead (A5); and for the other com = mySerial.read (); works perfect.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.