Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ps2.h>
- #include <LiquidCrystal.h>
- // OLED stuff
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define OLED_RESET 4
- Adafruit_SSD1306 display(OLED_RESET);
- // initialize the library with the numbers of the interface pins
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- int number;
- int max = 15;
- int score1 = 0;
- const int buttonPin_left = 13;
- const int buttonPin_up = 8;
- const int buttonPin_right = 9;
- int buttonState_up = 0;
- int buttonState_left = 0;
- int buttonState_right = 0;
- /*
- * an arduino sketch to interface with a ps/2 mouse.
- * Also uses serial protocol to talk back to the host
- * and report what it finds.
- */
- /*
- * Pin 7 is the mouse data pin, pin 6 is the clock pin
- * Feel free to use whatever pins are convenient.
- */
- PS2 mouse(6, 7);
- /*
- * initialize the mouse. Reset it, and place it into remote
- * mode, so we can get the encoder data on demand.
- */
- void mouse_init() {
- mouse.write(0xff); // reset
- mouse.read(); // ack byte
- mouse.read(); // blank */
- mouse.read(); // blank */
- mouse.write(0xf0); // remote mode
- mouse.read(); // ack
- delayMicroseconds(100);
- }
- void oled_write(int line, int pos, int size, String text) {
- display.clearDisplay(); // clear oled display
- display.setTextSize(size);
- display.setTextColor(WHITE);
- display.setCursor(line, pos);
- display.println(text);
- display.display();
- }
- // vars for our scene
- int plank1_pos = 20;
- int plank1_line = 0;
- int plank_buttom_pos = 40;
- int plank_buttom_line = 0;
- String plank1 = "___";
- String plank_buttom = "_____________________";
- void render() { // renders our scene
- display.setCursor(plank1_line, plank1_pos);
- display.println(plank1);
- display.setCursor(plank_buttom_line, plank_buttom_pos);
- display.println(plank_buttom);
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0, 55);
- display.println("Made by Dan Jones");
- display.setCursor(100, 1);
- display.println(score1);
- display.display();
- }
- // vars for character below
- int char_line = 0;
- int char_pos = 10;
- int length = 7;
- String chara = "*";
- void character(String direction) { // move a character around
- score(1);
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.clearDisplay(); // clear oled display
- // now lets draw our new position
- if (direction == "up") {
- char_line = char_line - length;
- display.setCursor(char_pos, char_line);
- display.println(chara);
- render(); // re-render our scene
- }
- if (direction == "left") {
- char_pos = char_pos - length;
- display.setCursor(char_pos, char_line);
- display.println(chara);
- render(); // re-render our scene
- }
- if (direction == "right") {
- char_pos = char_pos + length;
- display.setCursor(char_pos, char_line);
- display.println(chara);
- render(); // re-render our scene
- }
- render(); // re-render our scene
- }
- void score(int value) { // simple score for HUD
- score1 = score1 + value;
- render(); // re-render our scene
- }
- // vars for gravity
- int speed = 5;
- void gravity(int jump) { // lets try add gravity to our script....
- // is the user jumping?
- if (jump == 1) { // yes, lets jump
- display.clearDisplay();
- char_line = char_line - speed * 2;
- display.setCursor(char_pos, char_line);
- display.println(chara);
- render();
- }
- // collision detection is hard
- if (!(char_line >= plank1_pos)) {
- display.clearDisplay();
- char_line = char_line + speed;
- display.setCursor(char_pos, char_line);
- display.println(chara);
- render();
- }
- if (!(char_line >= plank_buttom_pos) && (char_pos >= plank1_pos)) {
- display.clearDisplay();
- char_line = char_line + speed;
- display.setCursor(char_pos, char_line);
- display.println(chara);
- render();
- }
- // lets check if the character is out of bounds
- if (char_line > plank_buttom_pos) { // reset
- char_line = 5;
- char_pos = 10;
- display.setCursor(char_pos, char_line);
- display.println(chara);
- render();
- }
- }
- void setup() {
- render(); // setup our scene
- lcd.begin(16, 2);
- Serial.begin(9600);
- mouse_init();
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // OLED display
- //oled_write(1, 0, 1, "Arduino TrackPad");
- }
- /*
- * get a reading from the mouse and report it back to the
- * host via the serial line.
- */
- void loop() {
- buttonState_up = digitalRead(buttonPin_up);
- buttonState_left = digitalRead(buttonPin_left);
- buttonState_right = digitalRead(buttonPin_right);
- char mstat;
- char mx;
- char my;
- /* get a reading from the mouse */
- mouse.write(0xeb); // give me data!
- mouse.read(); // ignore ack
- mstat = mouse.read();
- mx = mouse.read();
- my = mouse.read();
- /* send the data back up */
- lcd.setCursor(0, 0);
- lcd.print(" X = ");
- lcd.print(mx, DEC);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print(" Y = ");
- lcd.print(my, DEC);
- lcd.print(" ");
- if (my > max) {
- my = max;
- }
- if (mx > max) {
- mx = max;
- }
- while (my > 10) {
- number = number + 1;
- //Serial.print(number);
- //Serial.print("\n");
- lcd.setCursor(10, 0);
- //lcd.print("up");
- //oled_write(4, 0, 2, "Up");
- Serial.print("1");
- Serial.print("\n");
- //character("up");
- gravity(1);
- my = 0;
- //my = my - 1;
- }
- while (my < -10) {
- number = number - 1;
- //Serial.print(number);
- //Serial.print("\n");
- lcd.setCursor(10, 0);
- //lcd.print("down");
- //oled_write(4, 0, 2, "Down");
- Serial.print("2");
- Serial.print("\n");
- //character("down");
- my = 0;
- //my = my + 1;
- }
- while (mx < -10) {
- lcd.setCursor(10, 1);
- //lcd.print("left");
- //oled_write(4, 0, 2, "Left");
- Serial.print("4");
- Serial.print("\n");
- character("left");
- mx = 0;
- }
- while (mx > 10) {
- lcd.setCursor(10, 1);
- //lcd.print("right");
- //oled_write(4, 0, 2, "Right");
- Serial.print("3");
- Serial.print("\n");
- character("right");
- mx = 0;
- }
- // the touch pad controls suck, lets add buttons
- if (buttonState_up == HIGH) {
- gravity(1);
- }
- if (buttonState_left == HIGH) {
- character("left");
- }
- if (buttonState_right == HIGH) {
- character("right");
- }
- gravity(0); // run our gravity script
- //delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement