how to use 2 joysticks for 2 individual motors ? URGENT !

Hello …

Im in need of using 2 joysticks along with 2 motors ( total 4 … in parallel) … i want to use one joystick for left side motor and other for right side … im running a setup of 2 nrf24l01 modules … 2 arduinos… one ps2 controller … ln298 motor driver …

im done with the tx and rx of code … rx is in the form of arrrays … like 0 0 0 0 0 0 0 0 0 0 0 0 0 0 123 123 123 123

please help me out with the code for forward backward for right and left side motors … both joysticks must be oprational at single instance …

please post the complete code …

thanking you …

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

 
const int m11 = 2;
const int m12 = 3;
const int m21 = 4;
const int m22 = 9;
const int pwmx = 6;
const int pwmy = 5;
int fspeed;
 
byte joystick[20];
 
byte rx_address[5] = {0xD0,0xD0,0xD0,0xD0,0xD0};
 
uint32_t lastCommandTime = 0;
 
uint8_t data;
 
void setup()
{
	pinMode(m11, OUTPUT); //m11-forward
        pinMode(m12, OUTPUT);  
        pinMode(m21, OUTPUT);   //m21-forward
        pinMode(m22, OUTPUT);   //m22-backward
        pinMode(pwmy, OUTPUT);
        pinMode(pwmx,OUTPUT);
 
	Serial.begin(57600);
        Mirf.spi = &MirfHardwareSpi;
	Mirf.init();
	Mirf.setRADDR(rx_address);
	Mirf.payload = 20;
	Mirf.config();
}
 
void loop()
{
	if(!Mirf.isSending() && Mirf.dataReady())
	{
		Mirf.getData(joystick);
                data = ~joystick[2];
		Serial.print("-->\t"); 
                for(data = 0; data<20; data++)
                { 
                Serial.print(joystick[data]); 
                Serial.print(" "); 
                } 
                Serial.println();
		
       if (joystick[18] == 123) analogWrite(pwmx, 0);
       if (joystick[19] == 123) analogWrite(pwmy, 0);
 

 

    if (joystick[18] < 130)
   
  {
    fspeed = map(joystick[18], 130, 0, 0, 255);  // right forward
    digitalWrite(m11,LOW);
    digitalWrite(m12, LOW);
    analogWrite(pwmx, fspeed);
    digitalWrite(m21,HIGH);
    digitalWrite(m22, LOW);
    analogWrite(pwmy, fspeed);
  }
  
  else if (joystick[18] > 130) 
  {
    fspeed = map(joystick[18], 130, 255, 0, 255);  // right backward   
    digitalWrite(m11, LOW);
    digitalWrite(m12, LOW);
    analogWrite(pwmx, fspeed);
    digitalWrite(m21,LOW);
    digitalWrite(m22,HIGH);
    analogWrite(pwmy, fspeed);
  }
  

 
 else if (joystick[19] < 130) 
  {
    fspeed = map(joystick[19], 130, 0, 0, 255);  // left forward
    digitalWrite(m11,HIGH);
    digitalWrite(m12, LOW);
    analogWrite(pwmx, fspeed);
    digitalWrite(m21,LOW);
    digitalWrite(m22, LOW);
    analogWrite(pwmy, fspeed);
  }
 else if (joystick[19] > 130) 
  {
    fspeed = map(joystick[19], 130, 255, 0, 255);  // left back
    digitalWrite(m11,LOW);
    digitalWrite(m12, HIGH);
    analogWrite(pwmx, fspeed);
    digitalWrite(m21,LOW);
    digitalWrite(m22, LOW);
    analogWrite(pwmy, fspeed);
  }
		Mirf.flushRx();
		lastCommandTime = millis();
	}
 
	
}

Why are you sending 20 bytes of data to communicate 4 ints?

n13geek: please post the complete code ...

Well, that's a new twist! :)

PaulS: Why are you sending 20 bytes of data to communicate 4 ints?

PaulS: Why are you sending 20 bytes of data to communicate 4 ints?

PaulS: Why are you sending 20 bytes of data to communicate 4 ints?

thats not the required part... im done with the rx and tx ... just need to know the controls for joysticks !

Below is some servo pot code that might be somewhat similar to what you want.

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo");  
}

void loop()
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band 
    myservo1.write(newval1); //position the servo
    Serial.print(newval1); //print the new value for testing 
    Serial.print("a,");
    oldval1=newval1; //set the current old value
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print(newval2);
    Serial.print("b,");
    oldval2=newval2;
  }

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval1 < (oldval3-2) || newval3 > (oldval3+2)){  
    myservo3.write(newval3);
    Serial.print(newval3);
    Serial.print("c,");
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval4 < (oldval4-2) || newval4 > (oldval4+2)){  
    myservo1.write(newval4);
    Serial.print(newval4);
    Serial.print("d,");
    oldval4=newval4;
  }

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179); 
  if (newval5 < (oldval5-2) || newval5 > (oldval5+2)){  
    myservo5.write(newval5);
    Serial.print(newval5);
    Serial.print("e,");
    oldval5=newval5;
  } 
  delay(50);  //to slow loop for testing, adjust as needed
}

just need to know the controls for joysticks !

The Arduino can not control a joystick. It is an input device.

What do YOU want to do with the data from each joystick? If you don’t know, why are you sending the data?