Pushbutton with XBee (transmitting code)

Hello!!
I am a french student that uses XBees but struggles with it!

So… I am using XBees series 1. One in a boat with a motor shield and an XBee shield (with motors), the other in this gamepad:Wireless GamePad V2.0 for Arduino-DFRobot which has pushbuttons, my goal is that when I press on a button (for example the UP button), a motor is doing something.

There’s a communication between the the XBees already, they are in transparent mode.

My problem is that I have no idea about what to do to do what I said above…
Can I get any help?

I have done the following code for a Servo Motor:

#include <Servo.h> 
Servo myservo;

void setup() 
{ 
  myservo.attach(9);
} 

void loop() 
{
  myservo.write(90);
  delay(2000);

  myservo.write(10);
  delay(2000);
}

What I want is when I press the UP button, informations transmit so the code makes the motor move. Any help? :frowning:
Thanks!

You have an Arduino with a switch and an XBee. You have some code on that Arduino. You need to show that code.

You need to explain how the XBee is connected to the Arduino.

You have an Arduino with a servo and an XBee. You need to explain how the XBee is connected to the Arduino.

Communication between the Arduino and the XBee will be done using an instance of either the HardwareSerial class (Serial, for instance) or the SoftwareSerial class (NOT to be called mySerial or I'll come pound you to a pulp) and the print(), available(), and read() methods.

Typically, you'd send some value when a switch becomes pressed (start doing this) and another when the switch becomes released (stop doing this).

Thanks for the reply!
So first of all here’s the code form the gamepad (inside that gamepad, there’s an arduino leonardo in which there’s a place to plugin the first XBee), I’ve only changed the baudrate as I remember c:

int buttonState[17];
int joystick[4];
int AnalogButton[2];

int inputCommand = 0;

#define shackMotorPin 2

void setup()
{
  Serial1.begin(9600);  //Init the Serial baudrate
  InitIO();             // Initialize the inputs/outputs and the buffers
}

void InitIO(){ 
  for(int i = 0; i < 17; i++) 
  pinMode(i, INPUT); 
  pinMode(shackMotorPin,OUTPUT);
  digitalWrite(shackMotorPin,LOW);  // Stop shacking of the gamepad
}

unsigned long timer = 0;

void loop()
{
  if(millis() - timer > 500){  // manage the updating freq of all the controlling information
    DataUpdate();  //read the buttons and the joysticks data
    printData();   //print the datas and states
    timer = millis(); 
  }
  
  if(Serial1.available()){
    char input = Serial1.read();
    
    switch(input){
      case 's':
        Serial1.println("Shack");
        inputCommand = input;
        digitalWrite(shackMotorPin,HIGH);
        break;
      
      case 'x':
        Serial1.println("Stop");
        inputCommand = input;
        digitalWrite(shackMotorPin,LOW);
        break;
        
      default:
        break;
    }
  }
}

void DataUpdate(){
  
  for(int i = 3; i < 17; i++)  buttonState[i] = digitalRead(i);
  buttonState[0] = analogRead(0);
  buttonState[1] = analogRead(1);
  for(int i = 0; i < 4; i++)  joystick[i] = analogRead(i);
  for(int i = 4; i < 6; i++)  AnalogButton[i-4] = analogRead(i);
  
}

String Buttons[17] = {
  "J2","J1","","S2","S1","UP","LEFT","DOWN","RIGHT","1","4","2","3","RZ1","RZ2","LZ1","LZ2"};
  // Buttons Nmes

void printData(){
//  for(int i = 0; i < 17; i++)  Serial.print(buttonState[i]),Serial.print(" ");
//  for(int i = 0; i < 4; i++)  Serial.print(joystick[i]),Serial.print(" ");
//  for(int i = 0; i < 2; i++)  Serial.print(AnalogButton[i]),Serial.print(" ");
//  Serial.println("");
  Serial1.print("Button Pressed:");
  for(int i = 0; i < 2; i++)  if(buttonState[i] < 100)  Serial1.print(Buttons[i]),Serial.print(",");
  for(int i = 3; i < 17; i++)  if(buttonState[i] == 0)  Serial1.print(Buttons[i]),Serial.print(",");
  Serial1.println("");
  Serial1.print("Analog Sticks:");
  for(int i = 0; i < 4; i++)  Serial1.print(joystick[i]),Serial1.print(",");
  for(int i = 0; i < 2; i++)  Serial1.print(AnalogButton[i]),Serial1.print(",");
  Serial1.println("");
  Serial1.println(inputCommand);
}

Then there’s an arduino uno which has a motor shield on top and an xbee shield on top of that motor shield.

I’ll try what you said in your message!! Thank you!

Ugh please cive me some hints :frowning:
I’m having a headache right now… pretty sure it’s not even hard to do, I just don’t get it…
The servo is a Futaba S3003 by the way.

I did something like this (inspired from the gamepad (first code above)

#include <Servo.h>
Servo myservo;
String data[30];

  if(Serial.available()){
    for (i=0;i<30;i++)
    data[i] = Serial.read();
    
      void setup()
{
  myservo.attach(9);
}

void loop()
{
  myservo.write(data[6]);
  delay(2000);

  myservo.write(0);
  delay(2000);
} 

      break;

Pretty sure everything’s wrong with it but yeah… I tried