Cannot read analog prot when using softwareserial

Hi!

I am making a drone and I am testing it with a gps and 4 motors at the moment. When I try to read the TX port from my gps I cannot analog read my throttle (currently a on/off switch, waiting for a potentiometer). Here is my code:

#include <Servo.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>

// Booleans
bool gpsMode = false;

// Create GPS
TinyGPSPlus gps;

// Create SoftwareSerial to read TX pin. RX pin is unnecessary
SoftwareSerial ss(4, -1);

// Create Motors
Servo motor1;
Servo motor2;
Servo motor3;
Servo motor4;



void setup() {
  // Declare motor pins and min/max throttle
  motor1.attach(5, 1000, 2000);
  motor2.attach(6, 1000, 2000);
  motor3.attach(7, 1000, 2000);
  motor4.attach(8, 1000, 2000);

  // Calibrate motors
  motor1.writeMicroseconds(1000);
  motor2.writeMicroseconds(1000);
  motor3.writeMicroseconds(1000);
  motor4.writeMicroseconds(1000);

  // Start logging
  Serial.begin(9600);
  ss.begin(9600);

}

void loop() {
  // Throttle Controll
  int val;
  val = analogRead(A0);
  if (val > 0) {
    val = 200;
  } else if (val == 0) {
    val = 0;
  }
  Serial.println(val);
  val = map(val, 0, 1023, 1000, 2000);
  motor1.writeMicroseconds(val);
  motor2.writeMicroseconds(val);
  motor3.writeMicroseconds(val);
  motor4.writeMicroseconds(val);

  // GPS Mode
  if (gpsMode) {
    while (ss.available() > 0) {
      if (gps.encode(ss.read())) {
        if (gps.location.isUpdated()) {
          Serial.print(gps.location.lat(), 6);
          Serial.print(", ");
          Serial.println(gps.location.lng(), 6);
        }
      }

    }
  }
}

Thanks beforehand! Best regards Max

Try running Serial at a higher baud rate. Leave the ss as it is.

On a UNO SoftwareSerial conflicts with Servo. usually I’d say try with the Servo2 library but I think it’s limited to 2 motors and you need 4… may be switch to altSoftSerial

what board are you using ?

I am using a Arduino nano

I got it working! The values were different when using SoftwareSerial. I hade to check if the value is over or below 1000 instead of larger than 0 or equal to 0.

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