Skip to content

Instantly share code, notes, and snippets.

@RichardB01
Created February 18, 2015 01:37
Show Gist options
  • Save RichardB01/150dbf72d4e9400f8c4d to your computer and use it in GitHub Desktop.
Save RichardB01/150dbf72d4e9400f8c4d to your computer and use it in GitHub Desktop.
/////////////////////////////////////////////////////////////////////////
// RICHARD BAMFORD - 2015 - University Of Hull - Embedded Systems Club //
/////////////////////////////////////////////////////////////////////////
#include <LiquidCrystal.h>
// Accelerometer pins
#define XPIN 1
#define YPIN 2
// This is the average number which the accelerometer rests at.
#define NORM 344
// Init the LCD screen library with the connected pins.
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// Represent where in the LCD's memory the data for our images are held.
byte BlockDataLocation = (byte)0;
byte PlayerDataLocation = (byte)1;
// Global struct to hold data for all things we want to draw (Blocks and Player);
struct Entity {
double x;
double y;
};
// Array to hold all the entities currently on the screen.
// 32 is the number of cells we can acces on the screen.
Entity Entities[32];
// This will alow us to 'smooth' the readings from the accelerometer.
struct Smoother {
int Readings[10];
int Total = 0;
int Average = 0;
};
// Which index in the smoothers we are reading from.
int index;
Smoother x;
Smoother y;
// we need the previous to calculate the DT of movement.
int PreviousXMovement;
int PreviousYMovement;
int XTilt;
int YTilt;
void UpdateAccelerometerValues()
{
x.Total = x.Total - x.Readings[index];
x.Readings[index] = analogRead(XPIN);
x.Total = x.Total + x.Readings[index];
y.Total = y.Total - y.Readings[index];
y.Readings[index] = analogRead(YPIN);
y.Total = y.Total + y.Readings[index];
index = index + 1;
if (index == 10) index = 0;
x.Average = x.Total / 10;
y.Average = y.Total / 10;
int XMovement = x.Average - NORM;
int YMovement = NORM - y.Average;
// DT
XTilt = PreviousXMovement - XMovement;
YTilt = PreviousYMovement - YMovement;
PreviousXMovement = XMovement;
PreviousYMovement = YMovement;
}
void setup()
{
Serial.begin(9600);
// The actual data where we want to send to the LCD.
// Not declaring these global to save some scrappy memory.
byte blockData[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte playerData[8] = {
B01110,
B10001,
B10101,
B11001,
B11001,
B10101,
B10001,
B01110,
};
// Attach the data specified above to the locations (1st arg)
lcd.createChar(BlockDataLocation, blockData);
lcd.createChar(PlayerDataLocation, playerData);
lcd.begin(16, 2);
// Add our player to the entities list, always the first index.
Entity player = { 0, 0 };
Entities[0] = player;
for (int i = 0; i < 10; i++)
{
x.Readings[i] = NORM;
y.Readings[i] = NORM;
}
}
void loop()
{
// Clear the sceen and set cursor to upper-left.
lcd.clear();
UpdateAccelerometerValues();
// Move the player according to the new accelerometer values capped.
Entity ply = Entities[0];
ply.x += XTilt;
ply.y += YTilt;
Serial.print("X: "); Serial.print(ply.x);
Serial.print("Y: "); Serial.print(ply.y);
Serial.print("\n");
ply.x = map(ply.x, 0, 1, 0, 1);
ply.y = map(ply.x, 0, 16, 0, 16);
// total size of array / the size of a single entity gives us the number of elements we have in it.
for (int i = 0; i < sizeof(Entities)/sizeof(Entity); i++)
{
Entity currentEnt = Entities[i];
lcd.setCursor(currentEnt.x, currentEnt.y);
// If it's the first element (always player)
if (i == 0)
{
lcd.write(PlayerDataLocation);
}
else
{
lcd.write(BlockDataLocation);
}
}
lcd.setCursor(0, 0);
lcd.write(PlayerDataLocation);
// 1 Frame every 350ms
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment