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)
- }
- String fill = "-"; // the character that will fill our bar
- String fill_draw; // the variable that holds our bar string
- String blank = " "; // blank space
- int bars_total = 0; // this var will hold the number of bars on screen
- const int bars = 10; // this variable will store the maximum number of bars
- void PowerBar(int button) { // the bar used to display the force of the cannon
- OledWrite(0, 55, 1, "[ ]"); // our power bar, 19 bars
- if (button == 2) { // if the right button is pressed
- fill_draw += fill; // add a character to the fill_draw variable
- bars_total++; // add one to our total bar count
- }
- if (button == 1) { // if the left button is pressed
- }
- OledWrite(1, 55, 1, fill_draw);
- }
- 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
- PowerBar(0); // tell the power bar function the up button was pressed
- }
- if (buttonState_left == HIGH) { // left button detection
- PowerBar(1); // tell the power bar function the left button was pressed
- }
- if (buttonState_right == HIGH) { // right button detection
- PowerBar(2); // tell the power bar function the right button was pressed
- }
- }
- 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 Cannon"); // display program name to lcd
- LcdWrite(0, 1, "By Dan Jones"); // display creator to lcd
- }
- void loop() { // our looping function
- OledWrite(0, 10, 2, "Test"); // test text
- PowerBar(9); // put our power bar graphics into the oled buffer
- Controls(); // check for input
- RenderScene(); // render our scene to the oled display
- }
Advertisement
Add Comment
Please, Sign In to add comment