Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // some required libraries
- #include <LiquidCrystal.h> // lcd display
- #include <SPI.h> // communications
- #include <Wire.h> // i have no idea, but needed
- #include <Adafruit_GFX.h> // gfx library for oled
- #include <Adafruit_SSD1306.h> // oled display driver
- #define OLED_RESET 4 // oled reset code
- Adafruit_SSD1306 display(OLED_RESET); // oled reset
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // lcd setup
- // variables used for buttons
- const int buttonPin_left = 7; // left button pin
- const int buttonPin_up = 8; // up button pin
- const int buttonPin_right = 9; // right button pin
- int buttonState_up = 0; // up button state
- int buttonState_left = 0; // left button state
- int buttonState_right = 0; // right button state
- // lets create some functions
- void LcdWrite(int x, int y, String text) { // this function will write to the lcd screen, this is not handled by RenderScene()
- lcd.setCursor(x, y); // set our lcd cursor position
- lcd.print(text); // write to the lcd screen
- }
- void OledWrite(int x, int y, int size, String text) { // this function will write to our oled
- display.setTextSize(size); // set the font size
- display.setTextColor(WHITE); // set the font color
- display.setCursor(x, y); // set the cursor position
- display.println(text); // add our text to the oled buffer
- }
- void RenderScene() { // this function will render our scene on the oled display
- display.display(); // update our oled (print buffer)
- }
- // our platform arrays
- int Platform1[] = {0, 63, 128, 63}; // x1, y1, x2, y2
- void DrawPlatforms() { // this function will add our platforms to the oled buffer
- /*
- Lets go into a little detail in this function:
- We use the drawLine() function to draw a line to the oled.
- the arguments are as follows:
- x1, y1, x2, y2, color
- Lets think of using a pen.
- x1 = the starting x position of the pens tip
- y1 = the starting y position of the pens tip
- x2 = the ending x position of the pens tip
- y2 = the ending y position of the pens tip
- so, between these coordinates, a line is drawn
- color = the color of the pen tip
- */
- display.drawLine(Platform1[0], Platform1[1], Platform1[2], Platform1[3], WHITE); // put our first platform into the buffer
- }
- // our characters arrays
- String character[] = {"*"}; // this array holds ourcharacters images
- int CharacterPos[] = {10, 10}; // our characters x and y positions
- void Controls() { // this functions checks for input from the controller
- buttonState_up = digitalRead(buttonPin_up); // up button state
- buttonState_left = digitalRead(buttonPin_left); // left button state
- buttonState_right = digitalRead(buttonPin_right); // right button state
- if (buttonState_up == HIGH) { // up button detection
- // This is our jump script
- CharacterPos[1] = CharacterPos[1] - 7; // change the y position by - 1
- }
- if (buttonState_left == HIGH) { // left button detection
- CharacterPos[0] = CharacterPos[0] - 1; // change the x position by - 1
- }
- if (buttonState_right == HIGH) { // right button detection
- CharacterPos[0] = CharacterPos[0] + 1; // change the x position by + 1
- }
- }
- void Gravity() { // this function will control the gravitational forces of our character
- // this is hard to do
- if (CharacterPos[1] <= Platform1[1] - 5) { // if the characters y pos is less than platform1's y pos, fall
- CharacterPos[1] = CharacterPos[1] + 1; // change the y position by + 1
- }
- }
- void DrawCharacter() { // this function will draw our character to the screen
- OledWrite(CharacterPos[0], CharacterPos[1], 1, character[0]); // draw the character to the buffers
- }
- void setup() { // our function that is loaded first
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // lets begin our oled display/connection
- display.clearDisplay(); // clear oled display from anything left over from last session
- lcd.begin(16, 2); // define our lcd properties
- lcd.clear(); // clear our lcd screen from any residue
- LcdWrite(0, 0, "Arduino Gravity"); // display program name to lcd
- //LcdWrite(0, 1, "By Dan Jones"); // display creator to lcd
- }
- void Debugging() { // this function display debugging info on the lcd display
- lcd.setCursor(0, 1); // set our lcd cursor position
- lcd.print("X: "); // debugging info
- lcd.print(CharacterPos[0]); // debugging info
- lcd.print(" Y: "); // debugging info
- lcd.print(CharacterPos[1]); // debugging info
- }
- void loop() { // our looping function
- display.clearDisplay(); // clear our oled display (removes ghosting)
- Controls(); // check for input
- DrawCharacter(); // draw our character to the buffer
- DrawPlatforms(); // put our platforms into the oled buffer
- Gravity(); // run our gravity script
- Debugging(); // display our debugging information
- RenderScene(); // render our scene to the oled display
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement