0

Sorry about a newbie. 1. what mening of:

 if(Xstr[XstrLength-1] != '|'){
 Xstr[XstrLength] = '|';
 Xstr[XstrLength+1] = '0円';
  1. the setup: char Xstr[60]; what means? and then: itoa(Yint,Ystr,10);

Code:

#include <VirtualWire.h>
//gamepad setup
int up_button = 2;
int down_button = 4;
int left_button = 5;
int right_button = 3;
int start_button = 6;
int select_button = 7;
int joystick_button = 8;
int joystick_axis_x = A0;
int joystick_axis_y = A1;
int buttons[] = {up_button, down_button, left_button, right_button, start_button, select_button, joystick_button};
void setup()
{
 //gamepad setup
 for (int i; i < 7; i++)
 {
 pinMode(buttons[i], INPUT);
 digitalWrite(buttons[i], HIGH);
 }
 Serial.begin( 9600 );
 //transmitter setup
 vw_set_tx_pin(12); // Sets pin D12 as the TX pin
 vw_set_ptt_inverted(true); // Required for DR3100
 vw_setup(2000); // Bits per sec
}
int counter = 0;
int Xint;
int Yint;
char Xstr[60];
char Ystr[60];
void loop()
{
 // Read and store Sensor data
 Xint = analogRead(joystick_axis_x);
 Yint = analogRead(joystick_axis_y);
 // Convert integer data to Char array directly 
 itoa(Yint,Ystr,10);
 itoa(Xint,Xstr,10);
 int XstrLength = String(Xstr).length();
 if(Xstr[XstrLength-1] != '|'){
 Xstr[XstrLength] = '|';
 Xstr[XstrLength+1] = '0円';
 }
 char combinedArray[String(Xstr).length() + String(Ystr).length() + 1];
 sprintf(combinedArray, "%s%s", Xstr, Ystr);
 vw_send((uint8_t *)combinedArray, strlen(combinedArray));
 vw_wait_tx(); // Wait until the whole message is gone 
 Serial.println(combinedArray);
}
Majenko
106k5 gold badges82 silver badges139 bronze badges
asked Mar 15, 2019 at 20:00

1 Answer 1

2
  1. This adds some end of string characters.

    if(Xstr[XstrLength-1] != '|'){ Xstr[XstrLength] = '|'; Xstr[XstrLength+1] = '0円'

First it checks if Xstr last character is a '|', if not: Place a | directly after the last character And append a '0円', which is a symbol to 'end' a string

A string in C/C++ is normally build up like this, e.g. "Hello" contains of the bytes:

'H', 'e', 'l', 'l', 'o', 0円

Where each of the above are ASCII characters/values and the last 0円 a 0 byte.

  1. char Xstr[60]; defines a character array (string) that can contain 60 characters. Note however, that the ending 0円 also needs to be in this string, so you can actually only put 59 characters inside.

  2. itoa(Yint,Ystr,10); converts Yint which is an integer value to a string (where each digit has each own byte), and 10 is the length.

answered Mar 15, 2019 at 21:21
5
  • Thank you Michel Keijzers. I confused with char Xstr[60] and itoa(Yint,Ystr,10), actually they not same thing. Commented Mar 15, 2019 at 22:14
  • Gladly done, If the answer helps you, please consider upvote and accept my answer. Commented Mar 15, 2019 at 22:43
  • HI, when used a MCP4261, there is function "digitalPotWrite" , can I use "digitalWrite" replace it? Thanks. Commented Mar 16, 2019 at 20:05
  • Because the "digitalPotWrite" need be void definition, can't be used inside setup. Commented Mar 16, 2019 at 20:17
  • digitalPotWrite is a function to communicate with the MCP4261 over SPI. digitalWrite is a function to set an IO pin HIGH or LOW. The two have nothing at all to do with each other. Commented Mar 18, 2019 at 21:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.