[SOLVED] Use SoftwareSerial data to map a Servo

@ScruffR @nrobinson2000
So… I’m not sure if this is what you intended but this is what shows up in the Serial monitor:
822628206
822628206
822628206
822628206
822628206
822628206
822628206
822628206

And occasionally there’s a long line too:

822628206
822628206
822628206
822649485051131028206

So make your Photon do Serial.println(String(YOUR_VARIABLE_FOR_POT)) to tell the Arduino to move the servo.

EDIT:
Try it.

@nrobinson2000 Well I figured when you say

I’d make it like this

Serial1.println(String(DATA_FOR_POT));

with data being the value being sent. Then an error comes up saying

error: ‘DATA_FOR_POT’ was not declared in this scope

And the error comes up even if I have it the way you suggested it above.

Would you please share your Photon code? It would help you recieve more relevant help.

Sure:

int reading1;
String reading1AsAString;

void setup() {
Particle.subscribe(“sense1”, myHandler);
Serial1.begin(9600);
}

void loop() {

}

void myHandler(const char *eventname, const char *data) {

Serial1.println(data);

}

Then the data to the Arduino via SoftwareSerial.
The data it subscribes to is the pot data(0-1023).

I don’t see any 'analogread’s. How is your Photon supposed to read the potentiometer?

So it uses an additional photon?

It’s not reading the data right there. It’s subscribing to the pot data published by another Photon then sending it to the Arduino via tx/rx. and ground. The data will show show up in the Serial port. I’m just trying to get that data(0-1023) to be applied to the Servo.

It’s a process. :joy:

I see. I think the problem is that the Arduino is not receiving strings properly. Try this code from an earlier post, replacing Serial2 with portOne and adding in your other stuff.

@nrobinson2000

It doesn’t compile…

It shouldn’t. You must edit it to what is necessary for your Arduino’s purpose.

I did edit it, and I take back what I said earlier; I receive the same data as before but the Servo still won’t turn accordingly… I’ll share the code.

#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10, 11);
#include <Servo.h>

Servo myServo; // create a servo object
// software serial #2: TX = digital pin 8, RX = digital pin 9
// on the Mega, use other pins instead, since 8 and 9 don’t work on the Mega
SoftwareSerial portTwo(8, 9);

String readString = “”;

void parseCommand(String command)
{
if (command == “foo”)
{

}
}

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// Start each software serial port
portOne.begin(9600);

myServo.attach(6);
}

void loop()
{
// By default, the last intialized port is listening.
// when you want to listen on a port, explicitly select it:
portOne.listen();

// while there is data coming in, read it
// and send to the hardware serial port:

while (portOne.available() > 0) {
delay(3);
char c = portOne.read();
readString += c; // Add the character to the string
}
readString.trim();

if (readString.length() > 0) // If a string has been read…
{
Serial.println("Received: " + readString); // Send the parsed string to Serial for debugging
parseCommand(readString); // Do something with the string…
readString = “”; // Clear the string
}

delay(150);
myServo.write(readString);

}

There’s errors on

So the arduino sucessfully recieves the same string as the photon got from subscribing to the other photon?

Yes.

Get rid of this,

And put

int potVal = map(command.toInt(), 0, 1023, 0, 180);
myServo.write(potVal);

Inside of the parseCommand function, replacing whatever was inside.

@nrobinson2000
Like this?

if (readString.length() > 0) // If a string has been read…
{
Serial.println("Received: " + readString); // Send the parsed string to Serial for debugging
parseCommand(readString); // Do something with the string…
readString = “”; // Clear the string
int potVal = map(command.toInt(), 0, 1023, 0, 180);
> myServo.write(potVal);
}

I get an error that says ‘command’ was not declared in this scope

This is also not necessary.

No, there is a void parseCommand(String command) before setup(). Put it in there, getting rid of the if (command == "foo") stuff.

It works!!! :grinning:
Thanks so much!!! I never would’ve been able to figure that out!!! :grinning: :smile:

1 Like

You’re welcome.

1 Like