move object on LCD screen via analogue/joy stick input

Hey there,

basically I have a small ball displaying on a Nokia 5110 LCD screen. I want to be able to move the ball on the screen in relation to the movement of the analogue stick and the edge of the screen being a boundary that the ball cannot pass.

If anyone could help me with this it would be greatly appreciated, thanks in advance

You need to figure out how you're going to erase the previous position of the ball and redraw it in the new;if the ball is moving at less than it's own diameter per frame, it may be more visually appealing to only erase those parts of it no longer visible.

Just set up a pair of counters for x and y, and use these as the coordinates of the centre of your ball.
How your object behaves depends on how you treat the counters.

Without seeing code, it's hard to give anything other than general guidelines.

This is my code so far, very basic. I am new to this

#include <Adafruit_PCD8544.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <EEPROM.h>

// analog
int pin_x = 0;
int pin_y = 1;

// digital
int pin_switch = 8;

Adafruit_PCD8544 display = Adafruit_PCD8544(3,4,5,6,7); //Initialise display object

void setup() {
Serial.begin(9600);

display.begin();
display.clearDisplay();
display.setContrast(25);
display.display();

pinMode(pin_switch, INPUT);
}

void loop() {

int x = analogRead(pin_x);
int y = analogRead(pin_y); // read in analogue stick values
int b = digitalRead(pin_switch);

Serial.print("X:");
Serial.print(x);
Serial.print(",");
Serial.print("Y:");
Serial.print(y); // print values of analogue stick
Serial.print(",");
Serial.print("B:");
Serial.print(b);
Serial.print("\n");
delay(100);

display.drawCircle(x,y,1,BLACK); // draw cirlcle at position of x and y

}

ckind001:
This is my code so far, very basic. I am new to this

You don't scale the analog inputs so the position on the screen can range for 0,0 to 1023,1023. I'm guessing your sceeen is smaller than that so you may want to use something like map() to adjust the raw analog inputs to match the limits of your screen. map() - Arduino Reference
You don't erase the previous dot so your moving dot is going to leave a trail of ghosts. The simplest way to fix that is to erase the whole screen before displaying the new dot but that is going to cause a severe flicker. The normal way is to write the background color (or image) over the area containing the old dot just before drawing the new dot. That will flicker less since the drawing takes less time than a full erase. You should ONLY do the erase and re-draw if the dot has actually MOVED. If it is sitting in the same place as last time there is no need to erase it and re-draw it which will cause it to flicker.

So, I guess now you've got just a continuous smear of ball-trail?
Now create a couple of global variables called "lastX" and "lastY".
Each time around "loop()", see if the new values of x and y are significantly different to these globals, and if they are, draw the ball in WHITE at the old location, draw it in BLACK at the new location and set lastX and lastY to the new values.

You may wish to look at the "constrain" function.

Overall the aim is to achieve a snake of a certain length that moves with the analogue stick and almost wiggles and curves with the movement of the stick if you can imagine that. Sorry probably should've mentioned that in the first place.

So when I run my code at the moment it seems to just draw circles randomly in relation to the analog stick movement. Again Im not sure why its doing this but maybe something to do with the frame rate? it seems to me I want it to be drawing the circles (or snake) constantly so it has a smooth movement.

Also in regards to the mapping limits, yes I need to do this as the circles are only appearing on the edges and certain places on the LCd screen most likely because the values from the analogue stick are not mapped to that of the screens dimensions. The screen is 84x84 pixels and the analogue stick produces a value from 0 - 660 roughly in both the X and the Y directions.

Here is my code again, slightly modafied:

#include <SPI.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(3,4,5,6,7); //Initialise display object

// analog
int pin_x = 0;
int pin_y = 1;

int initialLength = 5;

void setup(){

Serial.begin(9600); //Begin Serial Communication
display.begin();
display.setContrast(25);
display.clearDisplay();
display.setTextSize(1); //Initial Display
display.setTextColor(BLACK);

//start up screen
/*
Serial.begin(9600); //Begin Serial Communication
display.begin();
display.setContrast(25);
display.clearDisplay();
display.setTextSize(1); //Initial Display
display.setTextColor(BLACK);
display.setCursor(10,15);
display.print("snake movement");
display.display();
delay(4000);
display.begin();
display.clearDisplay();
*/
}

void loop(){

int Ax = analogRead(pin_x);
int Ay = analogRead(pin_y); // read in analogue stick values

//Draw the snake
for(int i=0;i<initialLength;i++)
{
display.drawCircle(Ax,Ay,1,BLACK);
}
display.display();

}

Okay so I managed to Map it, producing lots of balls on the screen moving nicely with the movement of the analogue stick.

Can anybody help me with the frame rate problem on how to get it to daw the objects so that it appears as a smooth movement? and possibly to introduce the idea of a snake of length that wiggles with movement. so for example if you were to move the analogue stick from left to right it would make the snake move like a wave
///\ basically but a smooth curve.

updated code:

#include <SPI.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(3,4,5,6,7); //Initialise display object

// analog
int pin_x = 0;
int pin_y = 1;

int initialLength = 5;

void setup(){

Serial.begin(9600); //Begin Serial Communication
display.begin();
display.setContrast(25);
display.clearDisplay();
display.setTextSize(1); //Initial Display
display.setTextColor(BLACK);

//start up screen
/*
Serial.begin(9600); //Begin Serial Communication
display.begin();
display.setContrast(25);
display.clearDisplay();
display.setTextSize(1); //Initial Display
display.setTextColor(BLACK);
display.setCursor(10,15);
display.print("snake movement");
display.display();
delay(4000);
display.begin();
display.clearDisplay();
*/
}

void loop(){

// read in analogue stick values
int Ax = analogRead(pin_x);
int Ay = analogRead(pin_y);
// map the analogue values to mmatch that of the pixels in the LCD screen
Ax = map(Ax, 0, 660, 0, 84);
Ay = map(Ay, 0, 660, 0, 84);

// delay(2000);
//Draw the snake
for(int i=0;i<initialLength;i++)
{
display.drawCircle(Ax,Ay,1,BLACK);
}
display.display();

}

To do a snake, decide how many dots you want in your snake. Create arrays to remember where those dots were drawn. Each time you pick a new position for the 'head' of the snake, erase the oldest dot, re-draw all the newer dots, and add the newest dot.

If your dot position is jumping around even when you move the control slowly you might have dirt in the controls causing electrical noise.