Martha MigliacioAlex Wong
Published © GPL3+

Using the Pmod JSTK2 with Arduino Uno

Application notes for Pmod JSTK2 and Arduino Uno. The X & Y values are displayed in the serial monitor, lighting LEDs using buttons.

BeginnerShowcase (no instructions)1 hour355
Using the Pmod JSTK2 with Arduino Uno

Things used in this project

Story

Read more

Schematics

Pmod JSTK2 and Arduino Uno Fritzing file

Fritzing file displaying the connection of the Pmod JSTK2 to the Arduino Uno. This is the same connection setup as the Pmod JSTK application.

Pmod JSTK2 and Arduino Uno Fritzing Image

Fritzing image displaying the connection between the Pmod JSTK2 and Arduino Uno. This is the same setup as the Pmod JSTK application.

Code

Pmod JSTK2 and Arduino Uno Code

Arduino
This code will display the X and Y values in the serial monitor as a table and the Pmod JSTK2 LEDs will light up in a different color when the buttons are active.
/************************************************************************
*
* Test of the Pmod 
*
*************************************************************************
* Description: Pmod_JSTK2
* The X and Y values are displayed in the serial monitor as a table
* and the Pmod LED lights up in a different color
* when the buttons are active.
*
* Material
* 1. Arduino Uno
* 2. Pmod JSTK2
*
************************************************************************/

#define CS 10 // affectation of CS pin

#include <SPI.h> // call library

int i;
byte recu[7]; // storage of data of the module
int X;
int Y;
int cmd=0;
void setup()
{
 Serial.begin(9600); // initialization of serial communication
 SPI.begin(); // initialization of SPI port
 SPI.setDataMode(SPI_MODE0); // configuration of SPI communication in mode 0
 SPI.setClockDivider(SPI_CLOCK_DIV16); // configuration of clock at 1MHz
 pinMode(CS, OUTPUT);
}

void loop()
{
 digitalWrite(CS, LOW); // activation of CS line
 delayMicroseconds(15); // see doc: wait 15us after activation of CS line
 for (i=0;i<6;i=i+1)
   {
   recu[i] = SPI.transfer(cmd); // send 6 data to get back data from the module
   delayMicroseconds(10); // see doc: wait 10us after sending each data
   }
digitalWrite(CS, HIGH); // deactivation of CS line
X = recu[0]; // X is on 10 bit format
X |= (recu[1] << 8);
Y = recu[2]; // Y is on 10 bit format
Y |= (recu[3] << 8);

for (i=0;i<6;i=i+1) // Send in serial monitor
   {
   Serial.print("i");
   Serial.print(i);
   Serial.print("=");
   Serial.print(recu[i]);
   Serial.print('\t'); // tabulation
   }
Serial.print("X=");
Serial.print(X);
Serial.print('\t'); // tabulation
Serial.print("Y=");
Serial.println(Y);
delay(10);
switch (recu[4])
{
 case 1: // bouton of joystick is active
  // allumage de la led en rouge
  digitalWrite(CS, LOW); // activation of CS line
  SPI.transfer(0x84);
  delayMicroseconds(15);
  SPI.transfer(255);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  digitalWrite(CS, HIGH); // deactivation of CS line
  delay(1000);
  // extinction de la led
  digitalWrite(CS, LOW); // activation of CS line
  SPI.transfer(0x84);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  digitalWrite(CS, HIGH); // deactivation of CS line
  break;
 case 2: // bouton trigger active
  // allumage de la led en vert
  digitalWrite(CS, LOW); // activation of CS line
  SPI.transfer(0x84);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  SPI.transfer(255);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  digitalWrite(CS, HIGH); // deactivation of CS line
  delay(1000);
  // extinction de la led
  digitalWrite(CS, LOW); // activation of CS line
  SPI.transfer(0x84);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  digitalWrite(CS, HIGH); // deactivation of CS line
  break;
 case 3: // 2 boutons active
  // allumage de la led en bleu
  digitalWrite(CS, LOW); // activation of CS line
  SPI.transfer(0x84);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  SPI.transfer(255);
  delayMicroseconds(15);
  digitalWrite(CS, HIGH); // deactivation of CS line
  delay(1000);
  // extinction de la led
  digitalWrite(CS, LOW); // activation of CS line
  SPI.transfer(0x84); 
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  SPI.transfer(0);
  delayMicroseconds(15);
  digitalWrite(CS, HIGH); // deactivation of CS line
  break;
 default:
 break;
 delay(1000) ;
 }
}

Credits

Martha Migliacio

Martha Migliacio

5 projects • 10 followers
Alex Wong

Alex Wong

5 projects • 21 followers
I work in Digilent and like creating projects
Thanks to Lextronics.

Comments