I am controlling two dc 12 volt dc motors with gy-521 and using xbee s2c for wireless communication between two arduino uno, with the help of arduino xbee shield. My query is everything is working fine programming and hardware are fine but xbee is giving some delay in operation, so can anyone help me what can be the reason behind this issue, I have even updated the firmware of both the xbee via xctu but still this delay is causing a lot of problems.
Pranjul_t:
My query is everything is working fine programming and hardware are fine but xbee is giving some delay in operation
Post your code in tags and we'll see what's causing it - I'm guessing you have some delay() calls in your code, but I don't know for sure until I see it.
Power_Broker:
Post your code in tags and we'll see what's causing it - I'm guessing you have some delay() calls in your code, but I don't know for sure until I see it.
Thanks for the reply sir, I am not a pro in programming but I have tried doing it, i have put the simplest codes to work,I have already tried removing delays earlier but then gy-521 is getting reset on its own after few seconds every time, that delays in the transmitting codes are just to run gy-521 readings properly, Sir I have attached both the codes, tell me where I am going wrong.
Thanks
handgesture transmitter.ino (1.99 KB)
handgesture receiver.ino (1.27 KB)
TX:
#include <MPU6050.h>
MPU6050 accelgyroIC1(0x68);
MPU6050 accelgyroIC2(0x69);
int16_t ax, ay, az;
int16_t gx, gy, gz;
int16_t ax1,ay1,az1;
int16_t gx1,gy1,gz1;
void setup() {
Serial.begin(9600); //Baud rate must be the same as is on xBee module
Serial.println ("Initializing MPU and testing connections");
accelgyroIC1.initialize ( );
accelgyroIC2.initialize ( );
Serial.println(accelgyroIC1.testConnection( )? "Successfully Connected" : "Connection failed");
Serial.println(accelgyroIC2.testConnection( ) ? "Successfully Connected" : "Connection failed");
Serial.println("Reading Values");
}
void loop() {
accelgyroIC1.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
accelgyroIC2.getMotion6(&ax1, &ay1, &az1, &gx1, &gy1, &gz1);
//Serial.print(ax);
//Serial.print("\t");
//Serial.print(ay);
//Serial.print("\t");
//Serial.print(ax1);
//Serial.print("\t");
//Serial.println(ay1);
if (ax<=11000){
//Map the analog value to pwm value
char data= '3';
Serial.println(data);
delay(100);
}
else if (ax>=11001&&ax<13000)
{
char data= '2';
Serial.println(data);
delay(100);
}
else if (ax>25000)
{
char data= '4';
Serial.println(data);
delay(100);
}
else if (ax<24999&&ax>22000)
{
char data= '5';
Serial.println(data);
delay(100);
}
else if(ay>=9000)
{
char data= '6';
Serial.println(data);
delay(100);
}
else if(ay<8999&&ay>6000)
{
char data= '7';
Serial.println(data);
delay(100);
}
else if(ay<=-9000)
{
char data= '8';
Serial.println(data);
delay(100);
}
else if(ay>-8999&&ay<-6000)
{
char data= '9';
Serial.println(data);
delay(100);
}
}
RX:
const int motor1_pin1 = 5;
const int motor1_pin2 = 6;
const int motor2_pin2 = 10;
const int motor2_pin1 = 9;
void setup() {
Serial.begin(9600);
pinMode(motor1_pin1, OUTPUT);
pinMode(motor1_pin2, OUTPUT);
pinMode (motor2_pin2, OUTPUT);
pinMode (motor2_pin1, OUTPUT);
}
void loop() {
if (Serial.available()>0){
//Read the incoming byte
char data=Serial.read();
delay(20);
Serial.println(data);
if(data == '3')
{
digitalWrite(motor1_pin1,LOW);
digitalWrite(motor1_pin2,HIGH);
}
else if (data=='2')
{
digitalWrite(motor1_pin1,LOW);
digitalWrite(motor1_pin2,LOW);
}
else if (data== '4'){
digitalWrite(motor2_pin2,HIGH);
digitalWrite(motor2_pin1,LOW);
}
else if (data== '5')
{
digitalWrite(motor2_pin2,LOW);
digitalWrite(motor2_pin1,LOW);
}
else if (data== '6')
{
digitalWrite(motor1_pin2,HIGH);
digitalWrite(motor2_pin2,HIGH);
}
else if (data== '7')
{
digitalWrite(motor1_pin2,LOW);
digitalWrite(motor2_pin2,LOW);
}
else if (data== '8')
{
digitalWrite(motor1_pin1,HIGH);
digitalWrite(motor2_pin1,HIGH);
}
else if (data== '9')
{
digitalWrite(motor1_pin1,LOW);
digitalWrite(motor2_pin1,LOW);
}
}
}
Take out the delay() calls. All of them.
Once done, show a printout of your results from the serial monitor.
quote author=Power_Broker link=msg=3995803 date=1545959922]
Take out the delay() calls. All of them.
Once done, show a printout of your results from the serial monitor.
[/quote]
Sir I have removed all the delays, now the gy-521 is getting reset automatically as I have told you earlier.
See this scrren shot of serial monitor of transmiting end when connected in xbee.
Pranjul_t:
Sir I have removed all the delays, now the gy-521 is getting reset automatically as I have told you earlier.
It's not just the gy-521 that's resetting...it looks like the TX Arduino and any peripherals (i.e. IMUs) powered by the Arduino are getting reset as well.
How is your project being powered? Can you show a (detailed) pic or pics of your wiring?
This probably wont fix things, but if I were you, I'd initialize "char data;" above the setup() and only update the value in data like: "data = '3'" INSTEAD of "char data='3'".
I would also get into the habit of properly alighting curly braces and double check indentation - this helps with readability and the ease at which you find bugs.
Make these changes and post the new sketches as you are running them. Also, please provide the requested info. We'll figure this out