Go Down

Topic: RaspberryPi Serial with Arduino Servo controll (Read 66 times) previous topic - next topic

TyroneR

Hello all,
I am new to arduino, and my problem is that I am trying to control two servos that are attached to my arduino with a raspberry pi through usb serial.
I am able to control one servo with no problem. This code works fine.

Code: [Select]


#include <Servo.h>

int pos =0;
void setup()
{
  myservo.attach(5);
  Serial.begin(115200)
}
 void loop()
 {
  if (Serial.available()>0){
    pos = Serial.read();
  }
  myservo.write(pos)
 }


 
Here I am trying to control two servos by sending a character through serial to one of the servos with a value to move a servo. I have been stuck on this for days and need some help.


Code: [Select]


#include <Servo.h>
Servo servoX;
Servo servoY;

void setup()
{
  myservo.attach(5);
  Serial.begin(115200)
}
 void loop()
 {
  if (Serial.available()>0){
   char ch = Serial.read();
  if (ch == 'X');
  else if (ch == 'Y') ;
  }
  servoX.write('X'); // move to value that is assigned to X from serial
  servoY.write('Y'); // move to value that is assigned to Y from serial
 }



AWOL

#1
Jul 02, 2016, 04:58 pm Last Edit: Jul 02, 2016, 05:01 pm by AWOL
Code: [Select]
if (ch == 'X');
Oops

Code: [Select]
servoX.write('X');
Can't see the point of writing a constant value to a servo.
Did you mean to write the value of the variable "ch", or something mapped from it?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

TyroneR


TyroneR

From the raspberry pi I would send X(180) through the serial to the arduino. The arduino will then wait for a character and then read its value.

Robin2

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. Look especially at the 3rd example and the parse example.

You could send the data for the two servos like this "<123, 97>"

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

TyroneR

Thank you , but as a newbie I'm still at a lost. Has anyone done anything like this and can send me a code?

Robin2

Much of the code you need is in the link I already gave you. Have you studied it?

You may also find some useful stuff in this Python - Arduino demo which was written prior to the code in the other link.

...R
Planning and Implementing a Program

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy