Advertisement
BeamNG_IRC

Untitled

Nov 23rd, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. // some required libraries
  2. #include <LiquidCrystal.h> // lcd display
  3. #include <SPI.h> // communications
  4. #include <Wire.h> // i have no idea, but needed
  5. #include <Adafruit_GFX.h> // gfx library for oled
  6. #include <Adafruit_SSD1306.h> // oled display driver
  7. #define OLED_RESET 4 // oled reset code
  8. Adafruit_SSD1306 display(OLED_RESET); // oled reset
  9. LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // lcd setup
  10. // variables used for buttons
  11. const int buttonPin_left = 7; // left button pin
  12. const int buttonPin_up = 8; // up button pin
  13. const int buttonPin_right = 9; // right button pin
  14. int buttonState_up = 0; // up button state
  15. int buttonState_left = 0; // left button state
  16. int buttonState_right = 0; // right button state
  17.  
  18. // lets create some functions
  19. void LcdWrite(int x, int y, String text) { // this function will write to the lcd screen, this is not handled by RenderScene()
  20.     lcd.setCursor(x, y); // set our lcd cursor position
  21.     lcd.print(text); // write to the lcd screen
  22. }
  23.  
  24. void OledWrite(int x, int y, int size, String text) { // this function will write to our oled
  25.     display.setTextSize(size); // set the font size
  26.     display.setTextColor(WHITE); // set the font color
  27.     display.setCursor(x, y); // set the cursor position
  28.     display.println(text); // add our text to the oled buffer
  29. }
  30.  
  31. void RenderScene() { // this function will render our scene on the oled display
  32.     display.display(); // update our oled (print buffer)
  33. }
  34.  
  35. String fill = "-"; // the character that will fill our bar
  36. String fill_draw; // the variable that holds our bar string
  37. String blank = " "; // blank space
  38. int bars_total = 0; // this var will hold the number of bars on screen
  39. const int bars = 10; // this variable will store the maximum number of bars
  40.  
  41. void PowerBar(int button) { // the bar used to display the force of the cannon
  42.     OledWrite(0, 55, 1, "[                   ]"); // our power bar, 19 bars
  43.     if (button == 2) { // if the right button is pressed
  44.         fill_draw += fill; // add a character to the fill_draw variable
  45.         bars_total++; // add one to our total bar count    
  46.     }
  47.     if (button == 1) { // if the left button is pressed
  48.    
  49.     }
  50.     OledWrite(1, 55, 1, fill_draw);
  51. }
  52.  
  53. void Controls() { // this functions checks for input from the controller
  54.     buttonState_up = digitalRead(buttonPin_up); // up button state
  55.     buttonState_left = digitalRead(buttonPin_left); // left button state
  56.     buttonState_right = digitalRead(buttonPin_right); // right button state
  57.     if (buttonState_up == HIGH) { // up button detection  
  58.         PowerBar(0); // tell the power bar function the up button was pressed
  59.     }
  60.     if (buttonState_left == HIGH) { // left button detection      
  61.         PowerBar(1); // tell the power bar function the left button was pressed
  62.     }
  63.     if (buttonState_right == HIGH) { // right button detection      
  64.         PowerBar(2); // tell the power bar function the right button was pressed
  65.     }
  66. }
  67. void setup() { // our function that is loaded first
  68.     display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // lets begin our oled display/connection
  69.     display.clearDisplay(); // clear oled display from anything left over from last session
  70.     lcd.begin(16, 2); // define our lcd properties
  71.     lcd.clear(); // clear our lcd screen from any residue
  72.     LcdWrite(0, 0, "Arduino Cannon"); // display program name to lcd
  73.     LcdWrite(0, 1, "By Dan Jones"); // display creator to lcd
  74. }
  75.  
  76. void loop() { // our looping function
  77.     OledWrite(0, 10, 2, "Test"); // test text
  78.     PowerBar(9); // put our power bar graphics into the oled buffer
  79.     Controls(); // check for input
  80.     RenderScene(); // render our scene to the oled display
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement