Serial doesn't work

I connected an Arduino uno and an Arduino MKR WiFi1010, using the same ground.
The MKR receives messages from a processing app.
The TX and RX pins are connected correctly, but when I write on the serial of my arduino uno using MKR
it seems that the Arduino Uno does not receive anything, as if it needed a second serial to communicate.

thanking you in advance,
regards.

How do you define correctly ? (UNO is 5V, MKR WiFi1010 is 3.3V)

To understand each other,

However, the cable which is connected at pin 10 is connected to pin 0, and the one which is connected to 11 is connected to pin 1.
I define that I don't use the soft serial.

is your UNO powered through USB and do you have the Serial monitor open?

Arduino uno is powered by a power supply

If I don't connect the cable from arduino to pc, I can't see the serial monitor, so I've just answered to the second question

So you show a picture where the MKR is connected to pins 10 and 11 on the Uno, and the caption indicates that SoftwareSerial is being used on pins 12 and 13.

But your setup has the MKR connected to pins 0 and 1 on the Uno, and you're not using SoftwareSerial.

And you're also trying to use the serial monitor to communicate with the Uno, which uses pins 0 and 1. Have I got that right?

I don't use Serial monitor, but I simply use Serial.write() and Serial.read()

How do you know you don’t receive anything ?

Side note: you can power the uno through the jack say with 9V and also have the usb cable connected and use the serial monitor. In that case the uno does not draw its power from USB.

Could be a power cable to your house, if you don't connect the cable from arduino to pc, you can't see the serial monitor... :smiley:

To be fair he never mentioned the monitor. That was from my question

Through a Processing app? Arduino UNO --> Processing --> Arduino MKR like this?

Where is your Serial Handler code?

I use something like this in Processing 4 and Serial.write() doesn't play nicely in my average joe experience

// serial data comms
void serialEvent (Serial myPort) {
  String inString = myPort.readStringUntil('\n'); // read until println() not write()
  if (inString != null) {
    inString = trim(inString);
    idling = false; // disregard this is just an event driver
    test = 0; // disregard as is this is just a reset for me before new String
    val = 0; // disregard and this is another reset like test = 0;
    simMode = true; // disregard this too
    if (inString.equals("intruder")) { // look for the Serial.println("intruder"); from Arduino
      background(0); // disregard, for Processing in my sketch, clear monitor/canvas
      state = 670; // disregard, this sets overall game state in my Processing game
    }

Arduino side

  if (Serial.available() > 0) {
    char in = Serial.read();
// chars sent from Processing are a good way to drive levels or stages in an Arduino sketch
    if ((in == 'e') || (in == 'E')) {
      locked = true;
    }
    else if ((in == 'a') || (in == 'A')) {
      locked = false;
    }
  }
  switch (locked) {
    case false:
      // do stuff
    break;
   case true:
  // do other stuff
   break;
}

My arduino is connected to an evshield which turn a motor on if I press the 'w' key, so if the arduino reveives that, (which I send by the processing app), the motor would turn on, but using the MKR it not work

The order is: Processing-->MKR --> UNO.

The message sent by the processing app is catched by the MKR, and it should send it to UNO using the serial. But it seems that it doesn't work.

Well apparently, it isn't. Code?

So if you type char 'w' into the Arduino Serial Monitor (no Processing app running), does the motor behave as expected?

I don't think so. I have no documentation on the EV Shield (is that a Lego sensor/motor shield) you mention, no code, no nothing. If you want help you have to meet us halfway, bud.

Processing as in Processing.org apps connect to the Arduino from your PC using a Serial connection, right? Unless I'm missing something, that's about the only option unless you're using wifi maybe. If it's a USB wired connection, it's Serial and the message from Processing isn't to the EV Shield, it's sent to the Arduino which then translates that into the commands your EV Shield needs to make the motor run.

Or am I way off base here?

The MKR catch the message correctly, the problem is sending it to the UNO using serial.

If I type 'w' in the Serial monitor of the UNO, the motor run correctly.

I'll explain better. The processing app, sends messages to the MKR using wifi (processing webSocket library).
After that the MKR throught the serial should send it to the arduino UNO, but it seems that it can't catch that, cause the motor doesn't run.
In addiction if I plug the usb wire to access to the serial monitor, I can see that any message had arrived.

Something might wrong in the serial code either on the MKR or on the arduino

It would really help if you were sharing the code and the exact circuit

Yes of course here the code of the MKR.

#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>

/////// WiFi Settings ///////
char ssid[] = "...";
char pass[] = "...";

char serverAddress[] = "...";  // server address
int port = 8025;

WiFiClient wifi;
WebSocketClient client = WebSocketClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
int count = 0;

void setup() {
  Serial.begin(9600);
   while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
  }

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}



void loop() {
  Serial.println("starting WebSocket client");
  client.begin();

  while (client.connected()) {
    // check if a message is available to be received
    int messageSize = client.parseMessage();

    if (messageSize > 0) {
      Serial.println("Received a message:");
      String a = client.readString();
      Serial.write(a.c_str());
    }
    // wait 5 seconds
    delay(5000);
  }

  Serial.println("disconnected");
}

And here the UNO's code.


#include <Wire.h>
#include <EVShield.h>
#include <EVs_EV3Touch.h>
#include <EVShieldAGS.h>
#include <EVs_EV3Infrared.h>

#define NOTE_B3 247
#define distancePin A0
#define ULTRASONIC_TRIG_PIN 11
#define ULTRASONIC_ECHO_PIN 12

EVShield evshield(0x34, 0x36);

char input;
String inputString = ""; 
bool stringComplete = false;
int velocity = 0;
int rotation = 0;
uint16_t value;
uint16_t range;
float distance;

//float batteryVoltage = 0;


void setup() {
  Serial.begin(9600);
  evshield.init(SH_HardwareI2C);
  evshield.bank_a.motorReset();
  evshield.bank_b.motorReset();
  velocity = SH_Speed_Slow;
  pinMode (distancePin, INPUT);
  pinMode(ULTRASONIC_TRIG_PIN, OUTPUT);
  pinMode(ULTRASONIC_ECHO_PIN, INPUT);
}

void loop() {
   value = analogRead (distancePin);
  range = get_gp2d12 (value);
   digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
  long duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);
  distance=(duration/2.0)*0.0343;

  if (stringComplete) {
    Serial.println(inputString);
    inputString = "";
    stringComplete = false;
  }
 
  if (range < 100) {
    Serial.print(distance);
    Serial.println("cm");
    Serial.print(range);
    Serial.println(" mm");
    evshield.bank_b.motorStop(SH_Motor_1, SH_Next_Action_BrakeHold);
    evshield.bank_b.motorStop(SH_Motor_2, SH_Next_Action_BrakeHold);
  } 
  else {
    if (Serial.available() > 0) {
      input = Serial.read();
      if (input == 'w' || input == 'W') {
        if (rotation == 1) {
          evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Reverse, velocity, 90, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);  
        } else if (rotation == -1) {
          evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Forward, velocity, 90, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
        }
        rotation = 0;
        Serial.println(input);
        evshield.bank_b.motorRunUnlimited(SH_Motor_Both, SH_Direction_Forward, velocity);
      } 
      else if (input == 'a' || input == 'A') {
        if (rotation == 1) {
          evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Reverse, velocity, 180, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
        } else if (rotation == 0) {
          evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Reverse, velocity, 90, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
        }
        rotation = -1;
        Serial.println(input);
        evshield.bank_b.motorRunUnlimited(SH_Motor_Both, SH_Direction_Forward, velocity);
      } 
      else if (input == 's' || input == 'S') {
        Serial.println(input);
        evshield.bank_b.motorRunUnlimited(SH_Motor_Both, SH_Direction_Reverse, velocity);
      } 
      else if (input == 'd' || input == 'D') {
        if(rotation==-1){
          evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Forward, velocity, 180, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
        }
        else if(rotation==0){
          evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Forward, velocity, 90, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
        }
        rotation=1;
        Serial.println(input);
        evshield.bank_b.motorRunUnlimited(SH_Motor_Both, SH_Direction_Forward, velocity);
      } 
      else if (input == 'c' || input == 'C') {
        Serial.println(input);
        tone(8, NOTE_B3, 1000 / 4);
        int pauseBetweenNotes = (1000 / 4) * 1.30;
        delay(pauseBetweenNotes);
        noTone(8);
      } 
      else if (input == 17) {  //CTRL
        Serial.println(input);
        if (velocity == SH_Speed_Slow) {
          velocity = SH_Speed_Medium;
        } 
        else if (velocity == SH_Speed_Medium) {
          velocity = SH_Speed_Full;
        }
      } 
      else if (input == 16) {  //SHIFT
        Serial.println(input);
        if (velocity == SH_Speed_Full) {
          velocity = SH_Speed_Medium;
        } 
        else if (velocity == SH_Speed_Medium) {
          velocity = SH_Speed_Slow;
        }
      } 
      else if (input == ' ') {
        Serial.println(input);
        evshield.bank_b.motorStop(SH_Motor_Both, SH_Next_Action_Brake);
      }
    }
  }
}

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

uint16_t get_gp2d12 (uint16_t value) {
    if (value < 10) value = 10;
    return ((67870.0 / (value - 3.0)) - 40.0);
}

Thank you for finally showing us your code.

In your Uno program, you have a serialEvent() function that vacuums up any serial input into an inputString variable.

Then, in your main loop() you are checking for serial input with Serial.available() and trying to read it with Serial.read().

Do you see the problem?

Also, getting back to that earlier diagram, is there anything at all in there that is actually accurate? I'm focusing on R1, which is between the MKR's Tx pin and the Uno's Rx's pin. Please explain why it's necessary to further reduce the level of MKR's 3.3V output? Especially in light of the 328P's minimum Vih of 0.6Vcc?

I recognize that serialEvent from the IDE example sketch of the same name. I didn't have much luck with it, either, some years back.

One thing I determined was that doing this in a while loop is unnecessary, at least it was in my projects.

What I recommend is, make a new Processing sketch entirely and a new Arduino sketch entirely and just get the serial stuff working. Stick to one datatype if you can. Did you write the Processing app? Can we look at it?
In Processing, I find it easy to send chars FROM Processing to control Arduino but Strings get pretty dodgy.
In Arduino, I find it easy to send chars or Strings to Processing, I have that bit of Processing code from post 13 if you're interested. I'll add in the Arduino side so you see what I mean.

There's a lot going on in your code, without knowing your experience and adding in that you're trying to do wifi (another hurdle unto itself), it looks to me like you're swinging for the fences and now you're lost in your code, frustrated. Been there, believe me.

The solution is to get each element working on its own, then adding in new stuff gradually. The more complicated it gets, the more carefully you add stuff in.

One last thing: forget that Arduino example sketch from the IDE. (Sorry, Arduino Mods)
Get to know the much more comprehensive examples courtesy of @Robin2
https://forum.arduino.cc/t/serial-input-basics-updated/382007