Advertisement
Guest User

Arduboy I2C

a guest
Jan 19th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include "Arduboy.h"
  2. #include <Wire.h>
  3.  
  4. // Make an instance of arduboy used for many functions
  5. Arduboy arduboy;
  6.  
  7. // Variables for your game go here.
  8. char text[] = "Press Buttons!";
  9. byte x;
  10. byte y;
  11.  
  12. #define CHAR_WIDTH 6
  13. #define CHAR_HEIGHT 8
  14. #define NUM_CHARS (sizeof(text) - 1)
  15. #define X_MAX (WIDTH - (NUM_CHARS * CHAR_WIDTH) + 1)
  16. #define Y_MAX (HEIGHT - CHAR_HEIGHT)
  17.  
  18. void setup() {
  19.   //initiate arduboy instance
  20.   arduboy.begin();
  21.   arduboy.setFrameRate(60);
  22.  
  23.   // set x and y to the middle of the screen
  24.   x = (WIDTH / 2) - (NUM_CHARS * CHAR_WIDTH / 2);
  25.   y = (HEIGHT / 2) - (CHAR_HEIGHT / 2);
  26.  
  27.   if (!checkI2C()){
  28.       arduboy.println("\nCheck your circuit!");
  29.   }
  30.  
  31. }
  32.  
  33. void loop() {
  34.   // pause render until it's time for the next frame
  35.   if (!(arduboy.nextFrame()))
  36.     return;
  37.  
  38.   if(arduboy.pressed(RIGHT_BUTTON) && (x < X_MAX)) {
  39.     x++;
  40.   }
  41.  
  42.   if(arduboy.pressed(LEFT_BUTTON) && (x > 0)) {
  43.     x--;
  44.   }
  45.  
  46.   if((arduboy.pressed(UP_BUTTON) || arduboy.pressed(B_BUTTON)) && (y > 0)) {
  47.     y--;
  48.   }
  49.  
  50.   if((arduboy.pressed(DOWN_BUTTON) || arduboy.pressed(A_BUTTON)) && (y < Y_MAX)) {
  51.     y++;
  52.   }
  53.  
  54.   arduboy.clear();
  55.   arduboy.setCursor(x, y);
  56.   arduboy.print(text);
  57.   arduboy.display();
  58. }
  59.  
  60. bool checkI2C() {
  61.   arduboy.clear();
  62.   arduboy.println("Check i2c"); arduboy.display();
  63.   arduboy.println("Wire.begin();"); arduboy.display();
  64.   Wire.begin();
  65.   byte error, address;
  66.   int nDevices;
  67.   arduboy.println("I2C bus Scanning..."); arduboy.display();
  68.   nDevices = 0;
  69.   char tempText[20];
  70.   for(address = 8; address < 127; address++ ) {
  71.     sprintf(tempText,"Address:%d",address);
  72.     arduboy.setCursor(0, 24);
  73.     arduboy.println(tempText); arduboy.display();
  74.    
  75.     Wire.beginTransmission(address);
  76.     arduboy.println("begin.Transmission"); arduboy.display();
  77.     error = Wire.endTransmission();
  78.     arduboy.println("end.Transmission"); arduboy.display();
  79.     if (error == 0) {
  80.       arduboy.print("\nI2C device found at address 0x"); arduboy.display();
  81.       if (address<16) {
  82.         arduboy.print("0"); arduboy.display();
  83.       }
  84.       arduboy.println(address,HEX); arduboy.display();
  85.       nDevices++;
  86.     }
  87.     else if (error==4) {
  88.       arduboy.print("\nUnknow error at address 0x"); arduboy.display();
  89.       if (address<16) {
  90.         arduboy.print("0"); arduboy.display();
  91.       }
  92.       arduboy.println(address,HEX); arduboy.display();
  93.     }    
  94.   }
  95.   if (nDevices == 0) {
  96.     arduboy.println("No I2C devices found\n"); arduboy.display();
  97.     return false;
  98.   }
  99.   else {
  100.     arduboy.println("done\n"); arduboy.display();
  101.     return true;
  102.   }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement