Go Down

Topic: [SOLVED] Problem controlling servo and DC motors thorugh Xbee and Joystick (Read 199 times) previous topic - next topic

garu94

Hi, i'm trying to make two arduino communicate via Xbee. On one of them i mounted a joystick shield with an analog and a button, on the other i have two DC motors which i would like to turno on while pressing the button and off while not pressing it, and a servo, that i would like to control via the analog stick. I make them communicate via Xbee, but i'm not able to make them do both the task, though i have been able to control the servo alone.
Here is the code for the transmitter: https://imgur.com/a/eXqeL
Here's the one for the receiver: https://imgur.com/a/NmZma
I tried changing somehow the delays without success, anyone can help me?

sdturner

First off, please post code here. It's easier than looking at a .jpg, and if you must show us a picture of your code, at least make it a link so we don't have to cut and paste.

Nothing jumps out from the code.

Can you display the received data on a terminal screen? Is the problem the transmission between boards or outputting the data to the motors and servo?

Can you drive the motors and servo by substituting fake data for the received data?

Do you see the enable pins going high or low? Is the problem in your motor driver? Is it wired correctly? I assume IN18 and IN23 are signals to your motor driver board. Are they PWM and Direction?

How are you powering the motor and how much current do they draw? Maybe the motors are drawing too much power and the Arduino is resetting.

Same thing with the servo.

Robin2

Post your code as text so it can be examined in a text editor.

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

TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

garu94

Sorry for the code, I directly copy-paste from Reddit to this forum, not thinking of the Code Tag.
So this is the code for the transmitter Arduino attached to a Joystick, from whom i use only X axis from analog and one button.
Code: [Select]
#include <EasyTransfer.h>
EasyTransfer ET;
int potpin1 = A1;
int pos;
int button_B = 4;

struct SEND_DATA_STRUCTURE{
int servo1val;
bool enable;
};
SEND_DATA_STRUCTURE txdata;

void setup(){
 Serial.begin(9600);
 ET.begin(details(txdata), &Serial);
 pinMode(button_B, INPUT);
}

void loop(){
 pos = map(analogRead(potpin1), 0, 1023, 0, 179);
 txdata.servo1val = pos;
 if(digitalRead(button_B) == 0){
  txdata.enable = 1;
 }
 else if(digitalRead(button_B) == 1){
  txdata.enable = 0;
 }
 
  ET.sendData();
}


And the following one is for the receiver:
Code: [Select]
#include <Servo.h>
#include <EasyTransfer.h>
EasyTransfer ET;

Servo ST1;
//SX
int enSX = 6;
int in14_SX = 5;
int in23_SX = 4;

//DX
int enDX = 8;
int in14_DX = 10;
int in23_DX = 9;

struct RECEIVE_DATA_STRUCTURE{
 int servo1val;
 bool enable;
};

RECEIVE_DATA_STRUCTURE txdata;

void setup(){
 Serial.begin(9600);

 ET.begin(details(txdata), &Serial);
 // Servo
 ST1.attach(7, 1000, 2000);
 //SX
  pinMode(enSX, OUTPUT);
  pinMode(in14_SX, OUTPUT);
  pinMode(in23_SX, OUTPUT);
  //DX
  pinMode(enDX, OUTPUT);
  pinMode(in14_DX, OUTPUT);
  pinMode(in23_DX, OUTPUT);
  //Setup Input motori
  //SX
  digitalWrite(in14_SX, LOW);
  digitalWrite(in23_SX, HIGH);
  //DX
  digitalWrite(in14_DX, LOW);
  digitalWrite(in23_DX, HIGH);

  pinMode(12, OUTPUT);
}

void loop(){
 if(ET.receiveData()){
    digitalWrite(12, HIGH);
   //Servo
   ST1.write(txdata.servo1val);
   //Accensione Motori
   if(txdata.enable == HIGH){
    digitalWrite(enSX, HIGH);  // Enable SX
    digitalWrite(enDX, HIGH);  // Enable DX
    }
    else if(txdata.enable == LOW){
    digitalWrite(enSX, LOW); //Spegni motori
    digitalWrite(enDX, LOW); //Spegni motori
    }
  }
}


Basically, I have two DC motors that I only want to turn on/off by pressing the button. I have so many inputs (In14, In23) because I have two L298N modules, and each of them could possibly run two different motors but actually run only one. 14 and 23 inputs are connected together to use less pins, I think (it was not me to connect the electric circuit of the drone, i'm though the one who has to make it work...).
Motors are working because when i attach the joystick directly to the arduino that control the motors it work, though i'm not able to make it work through Xbee.
Keep in mind that the two Xbee modules are correctly programmed because they can communicate (i was able to control the servo alone, in fact, but then when i tried to also make the two DC motors run, it stopped working  :smiley-confuse: ).

Thank you for help and replying so soon, and sorry if i was not able to reply sooner but in the weekend i was not able to acces the lab

garu94

Okay, that's definitely weird. Today i re-uploaded the sketches and power on everything, and all seemed to work perfectly, both the servo-analog interaction and the button-dc motors one.
Suddenly, after a few minutes, all stopped to work, only the servo seemed to still receive some inputs but defintely not precisely.

TomGeorge

Everything runs on smoke, let the smoke out, it stops running....

garu94

Guys i solved it, i'm completely an idiot. I put delay in the receiver instead of putting it into the transmitter code. Now with a delay(100) in the transmitter code and a few more minor fixes it works perfectly.

Go Up