Hey everyone! I am new to arduino, but have a lot of experience with electrical work. I am programming a joystick to my arduino and I need help with the code. I have no idea how to code at all, so can anyone get code for my need? Here is what I need. I have an arduino pro micro, and I need code to make pins 2-15 all individual button or switches inputs, I need A0-A3 to all be button and switches inputs, and finally, if TX and RX can be used as individual button or switch inputs, that would be great. I have absolutely zero experience coding so I was wanting to know if someone could code this real quick or if someone already has.
-Thanks!
Welcome. What did a Google search turn up? Are you aware of the keyboard library?
Do you intend to learn how to do this, or pay someone to do it?
What Arduino model are you using? After Pin 13 the numbering depends on the model.
I mean, I probably am either going to find an open source project with this or possibly pay someone. I really not trying to full on learn how to code, mainly because of the projects I work on. Me and a bunch of my friends build F-16 full simulator cockpits, with most, even all real F-16 components. I do more wiring and electrical, even metal, and other odd things. Some of my friends do other things and we all share our findings in an open source forum. So I will probably pay someone, because I haven't found anything yet. And for what model of arduino I am using, it is a pro micro I found on ebay. It has already soldered leads for wires and when I plug it in it says arduino leonardo. I am not quite sure what exact model, because I literally have been messing with it for not that long.
-Thanks!
Generic joystick routine:
How to make Arduino Pong Game-Arduino Project - Arduino Project Hub
My version: https://www.youtube.com/watch?v=6PYrtSO9D_k
/*Binary sketch size: 13,766 bytes (of a 28,672 byte maximum)
This example code is in the public domain.
Based upon:http://arduino.cc/en/Tutorial/TFTPong
Created by Tom Igoe December 2012
Modified 15 April 2013 by Scott Fitzgerald
Modified 15 June 2014 by M. Ray Burnette for ILI9340 TFT & X/Y joystick
Joystick used: http://www.ebay.com/itm/New-JoyStick-Breakout-Module-Shield-PS2-Joystick-Game-Controller-For-Arduino-/200955114766
LCD TFT 2.2": http://www.ebay.com/itm/2-2-inch-2-2-SPI-TFT-LCD-Display-module-240x320-ILI9341-51-AVR-STM32-ARM-PIC-/200939222521
LCD Note: Exact version used is an older version of the above, ILI9340.
Color definitions for TFT SPI 2.2" Display:
ILI9340_BLACK 0x0000
ILI9340_BLUE 0x001F
ILI9340_RED 0xF800
ILI9340_GREEN 0x07E0
ILI9340_CYAN 0x07FF
ILI9340_MAGENTA 0xF81F
ILI9340_YELLOW 0xFFE0
ILI9340_WHITE 0xFFFF
*/
#include "Adafruit_ILI9340.h"
#include "Adafruit_GFX.h"
#include <SPI.h>
// SPI pins Sparkfun Pro Micro 32U4
#define mosi 16
#define miso 14
#define sclk 15
#define cs 10
#define dc 9
#define rst 8
void(* resetFunc) (void) = 0; //declare reset function @ address 0
// Use hardware SPI
Adafruit_ILI9340 TFT = Adafruit_ILI9340(cs, dc, rst);
// variables for the position of the ball and paddle
int paddleX ;
int paddleY ;
int oldPaddleX ;
int oldPaddleY ;
int ballDirectionX = 1;
int ballDirectionY = 1;
int ballSpeed = 10; // lower numbers are faster
int ballX, ballY, oldBallX, oldBallY;
// screen stuff used to be private now global
int myWidth ;
int myHeight ;
boolean OneTime ;
char Arduino = 57 ;
char Player = 57 ;
void setup()
{
pinMode( 2, INPUT) ; digitalWrite( 2, HIGH); // turn on pullup resistor
// initialize the display
TFT.begin();
myWidth = TFT.width() ;
myHeight = TFT.height();
TFT.fillScreen(ILI9340_BLACK); // clear display
TFT.setRotation(3); // landscape
// black background
TFT.setTextColor(0xF9B0, ILI9340_BLACK) ;
TFT.setTextSize(1); // Small 53 char / line
TFT.print("A game by Ray Burnette") ;
TFT.setTextSize(2); // Small 26 char / line
TFT.setCursor(40, 40) ; TFT.print("Blazing Paddle") ;
TFT.setTextColor(ILI9340_YELLOW, ILI9340_BLACK) ;
TFT.setCursor(0, 59); TFT.print("<--FASTER-+-SLOWER-->") ;
TFT.setCursor(0, 174); TFT.print(" Left or Right joystick ") ;
TFT.setCursor(0, 195); TFT.print(" to adjust ball speed. ") ;
TFT.setCursor(0, 220); TFT.print("Press down to continue...") ;
paddleX = oldPaddleX ;
paddleY = oldPaddleY = myHeight / 3 ;
while( UserInput() )
{
// map( value, fromLow, fromHigh, toLow, toHigh)
paddleX = map(analogRead(A0), 0, 1023, 0, myWidth) ;
if ( oldPaddleX != paddleX )
{
TFT.fillRect(oldPaddleX, oldPaddleY, 20, 5, ILI9340_BLACK) ;
}
TFT.fillRect(paddleX, paddleY, 20, 5, ILI9340_WHITE);
oldPaddleX = paddleX;
}
randomSeed(analogRead(0)); // X axis value
TFT.begin();
myWidth = TFT.width() - 85 ; // smaller court for score keeping
// myWidth = TFT.width() ; // full-screen court
myHeight = TFT.height();
TFT.setRotation(3); // landscape
TFT.fillScreen(ILI9340_BLACK); // clear display
ballX = random(20, 220) ;
ballY = random( 5, 235) ;
// partition court for score keeping to right side of screen
// drawLine( x0, y0, x1, y1, uint16_t color);
TFT.drawLine(myWidth + 20, 0, myWidth + 20, myHeight, ILI9340_CYAN ) ;
// drawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint16_t bg, uint8_t size);
TFT.drawChar(myWidth + 30, 55, 'M', ILI9340_CYAN, ILI9340_BLACK, 2) ;
TFT.drawChar(myWidth + 45, 55, 'e', ILI9340_CYAN, ILI9340_BLACK, 2) ;
TFT.drawChar(myWidth + 30, 128, 'Y', ILI9340_YELLOW, ILI9340_BLACK, 2) ;
TFT.drawChar(myWidth + 45, 128, 'o', ILI9340_YELLOW, ILI9340_BLACK, 2) ;
TFT.drawChar(myWidth + 60, 128, 'u', ILI9340_YELLOW, ILI9340_BLACK, 2) ;
}
void loop()
{
// map the paddle's location to the position of the potentiometers
// map( value, fromLow, fromHigh, toLow, toHigh)
paddleX = map(analogRead(A0), 0, 1020, 1, myWidth - 20) ; //- 20/2;
paddleY = map(analogRead(A1), 0, 1020, 5, myHeight - 5) ; //- 5/2;
// set the fill color to black and erase the previous
// position of the paddle if different from present
if ( (oldPaddleX != paddleX) || (oldPaddleY != paddleY) )
{
TFT.fillRect(oldPaddleX, oldPaddleY, 20, 5, ILI9340_BLACK) ;
}
// draw the paddle on screen, save the current position
TFT.fillRect(paddleX, paddleY, 20, 5, ILI9340_WHITE);
oldPaddleX = paddleX;
oldPaddleY = paddleY;
// update the ball's position and draw it on screen
if (millis() % ballSpeed < 2)
{
moveBall();
}
if ( Scoring(Arduino, Player) ) // either score has gone to 0
{
TFT.setTextColor(ILI9340_GREEN, ILI9340_BLACK) ;
TFT.setTextSize(3);
TFT.setCursor(5, 30) ; TFT.print(" END OF GAME") ;
TFT.setCursor(5, 180) ; TFT.print("PRESS JOYSTICK") ;
TFT.setCursor(5, 100) ;
if (Arduino == '0')
{
TFT.print(" YOU WIN !") ;
}
else
{
TFT.print(" I WIN !!!") ;
}
while( UserInput() ) {}
resetFunc(); //call reset function to reboot Arduino code
}
}
void moveBall() // this function determines the ball's position on screen
{
OneTime = true ;
// if the ball goes offscreen, reverse the direction:
if (ballX > myWidth || ballX < 1 )
{
ballDirectionX = -ballDirectionX;
}
if (ballY >= TFT.height() || ballY < 1)
{
ballDirectionY = -ballDirectionY;
if( OneTime ) {
Player-- ; // deduct 1 from player
OneTime = false ;
}
}
// check if the ball and the paddle occupy the same space on screen
if (inPaddle(ballX, ballY, paddleX, paddleY, 20, 5))
{
ballDirectionX = -ballDirectionX;
ballDirectionY = -ballDirectionY;
if (OneTime ) {
Arduino-- ; // deduct 1 from Arduino
OneTime = false ;
}
}
// update the ball's position
ballX += ballDirectionX;
ballY += ballDirectionY;
// erase the ball's previous position
if (oldBallX != ballX || oldBallY != ballY)
{
TFT.fillRect(oldBallX, oldBallY, 7, 7, ILI9340_BLACK) ; // ball orig 5*5 made larger to see on denser TFT
}
// draw the ball's current position
// fillRect( x0, y0, w, h, uint16_t color);
TFT.fillRect(ballX, ballY, 7, 7, ILI9340_GREEN) ;
oldBallX = ballX ;
oldBallY = ballY ;
}
// this function checks the position of the ball to see if it intersects with the paddle
boolean inPaddle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight)
{
boolean result = false;
if ((x >= rectX && x <= (rectX + rectWidth)) &&
(y >= rectY && y <= (rectY + rectHeight)))
{
result = true;
}
return result;
}
boolean UserInput()
{
// map ( value, fromLow, fromHigh, toLow, toHigh)
ballSpeed = map(analogRead(A0), 0, 1023, 1, 20) ;
return digitalRead(2);
}
boolean Scoring(char Arduino, char Player)
{
boolean endGame = false ;
static char previousArduino, previousPlayer ;
uint16_t x = 270 ;
uint16_t y = 75 ;
uint16_t y1 = 150 ;
if( Arduino == 48 || Player == 48) endGame = true ; // char 48 == int 0
if ( Arduino != previousArduino || Player != previousPlayer )
{
// drawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint16_t bg, uint8_t size);
TFT.drawChar(x, y, Arduino, ILI9340_CYAN, ILI9340_BLACK, 3);
TFT.drawChar(x, y1, Player, ILI9340_YELLOW, ILI9340_BLACK, 3);
previousArduino = Arduino ;
previousPlayer = Player ;
}
return endGame ;
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.