Pages: [1]   Go Down
Author Topic: Arduino Mega 2560 serial with other device  (Read 509 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need to establish communication between arduino mega 2560 with external serial device called SKPS( http://www.cytron.com.my/viewProduct.php?pcode=SKPS&name=PS2%20Controller%20Starter%20Kit)
Actually my project is about to control arduino output via PS2 wireless controller, SKPS used as communication bridge between PS2 controller with arduino. SKPS consist of TX,RX,5V,GND and RESET pin that should cross connect to TX and RX pin of arduino.

This is the sample code i done but didt works
Code:
//skps protocol
#define p_Up     4
#define p_Right     5
#define p_Down     6
#define p_Left     7
#define p_L2     8
#define p_R2     9
#define p_L1     10
#define p_R1     11
#define p_Triangle  12
#define p_Circle    13
#define p_Cross     14
#define p_Square    15

const int rx= 19;
const int tx= 18;

void setup()
{
  Serial1.begin(9600);      //Set serial baud rate as 9600

 
  //Set the mode for each digital pins whether input or output
  pinMode(rx, INPUT);
  pinMode(tx, OUTPUT);
 
  pinMode(53,OUTPUT);
}

void loop ()
{
   if(skps(p_Up)==0)            //Check whether Up button is pressed
  {
    digitalWrite(53,HIGH);               
  }
 
  else if(skps(p_Down)==0) //Check whether Down button is pressed
  {
    digitalWrite(53,LOW);
  }
}

unsigned char receive_data(void)  //Function to wait for a byte receive from UART
{
  unsigned char temp;
  while(!Serial1.available());    //Wait until data received
  temp=Serial1.read();
  return temp;                    //Return the received data
}

unsigned char skps(unsigned char data) //Function to send out a byte via UART
{
  Serial1.write(data);             //Send new data
  return receive_data();          //Return received data
}
 

appreciate if someone can help in this
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  //Set the mode for each digital pins whether input or output
  pinMode(rx, INPUT);
  pinMode(tx, OUTPUT);

The Serial1.begin() occupied these two pins and shortly after you're re-assigning them to the GPIOs, so the serial interface won't work.

Remove these lines from your code and try again.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i tried to remove the command but still didt works.
I think the command is needed because of the cross connection of external serial device with arduino board
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think the command is needed because of the cross connection of external serial device with arduino board
They are not. Whatever diddling of those pins is needed is done by the HardwareSerial class.

Why are you connecting to the RESET pin? Are you expecting the external device to reset the Mega?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

to make things clear, please refer to the picture attached. my main reference of connection.
For this moment i just want to figure out and solve simple serial communication between arduino and external device (SKPS),
As shown in previous programming, i'm trying to light up digital pin 53 after button at PS2 controller pressed.
I didt connect the RESET pin of external device with Arduino, leave it unconnected.
I connect 5V arduino to 5V external device, arduino GND to external device GND, external device TX to arduino RX and external device RX to arduino TX.
I think the connection should be ok but the problem at the communication part


* picture.jpg (133.69 KB, 864x694 - viewed 26 times.)
« Last Edit: March 23, 2013, 10:31:46 am by JonathanLaw » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The picture shows a Duemillanove and not a Mega2560. If you connected it like the picture shows, the sketch is wrong (Serial1 instead of Serial). Why don't copy the traffic of Serial1 to Serial for debugging (given you have connected the hardware to Serial1)?
Logged

Pages: [1]   Go Up
Jump to: