Guest User

Untitled

a guest
Jul 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "Arduboy2.h"
  3.  
  4. uint8_t AcXH, AcXL;
  5.  
  6. constexpr float Tau = 6.28318530718;
  7.  
  8. constexpr float mapRange(float input, float inputMin, float inputMax, float outputMin, float outputMax)
  9. {
  10. return outputMin + (input - inputMin) * ((outputMax - outputMin) / (inputMax - inputMin));
  11. }
  12.  
  13. constexpr uint8_t screenWidth = WIDTH;
  14. constexpr uint8_t screenHeight = HEIGHT;
  15. constexpr uint8_t halfScreenWidth = WIDTH / 2;
  16. constexpr uint8_t halfScreenHeight = HEIGHT / 2;
  17.  
  18. Arduboy2 arduboy;
  19.  
  20. // amplitude is the sine wave's
  21. // distance from the vertical baseline in pixels.
  22. // i.e. the height of the wave is 2 * amplitude
  23. uint8_t amplitude = 20;
  24.  
  25. // frequency is the number of pixels a full wave spans
  26. // on the horizontal axis.
  27. uint8_t frequency = 16;
  28.  
  29. // sampleOffset is used to animate the sine wave
  30. uint8_t sampleOffset = 0;
  31.  
  32. void setup()
  33. {
  34. arduboy.boot();
  35.  
  36. TCCR1B = (bit(WGM32) | bit(CS31)); // CTC mode. Divide by 8 clock prescale
  37.  
  38. // Initialize MPU-6050
  39. Wire.begin();
  40. Wire.beginTransmission(0x69);
  41. Wire.write(0x6B); // PWR_MGMT_1 register
  42. Wire.write(0); // set to zero (wakes up the MPU-6050)
  43. Wire.endTransmission(true);
  44. }
  45.  
  46. // previousX and previousY are used for drawing
  47. // the lines that make up the sine waves.
  48. // They represent the endpoint of the previous line.
  49. uint8_t previousX = 0;
  50. uint8_t previousY = halfScreenHeight;
  51.  
  52. void loop()
  53. {
  54. Wire.beginTransmission(0x69); // I2C address of the MPU-6050
  55. Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  56. Wire.endTransmission(true);
  57. Wire.requestFrom(0x69,2,true);
  58. AcXH = Wire.read(); // 0x3B (ACCEL_XOUT_H)
  59. AcXL = Wire.read(); // 0x3C (ACCEL_XOUT_L)
  60.  
  61. OCR1A = AcXH<<8|AcXL; // load the count (16 bits), which determines the frequency
  62. frequency = (127 & AcXH);
  63.  
  64. arduboy.pollButtons();
  65.  
  66. if (arduboy.justPressed(UP_BUTTON))
  67. TCCR1A = bit(COM1A0); // set toggle on compare mode (which connects the pin)
  68.  
  69. if (arduboy.justPressed(DOWN_BUTTON))
  70. TCCR1A = 0; // set normal mode (which disconnects the pin)
  71.  
  72. // x represents the x coordinate of the current screen pixel
  73. // This is used as an offset for sampling sine
  74. for(uint8_t x = 0; x < screenWidth; ++x)
  75. {
  76. // theta is derived by mapping the sampleOffset
  77. // from a (0, frequency) range to a (0, Tau) range.
  78. float theta = mapRange((sampleOffset + x) % frequency, 0, frequency, 0, Tau);
  79.  
  80. double y = halfScreenHeight + (-amplitude * sin(theta));
  81.  
  82. if (x >= previousX)
  83. arduboy.drawLine(previousX, previousY, x, y, WHITE);
  84.  
  85. previousX = x;
  86. previousY = y;
  87. }
  88.  
  89. ++sampleOffset;
  90. sampleOffset %= frequency;
  91.  
  92. arduboy.display(CLEAR_BUFFER);
  93. }
Add Comment
Please, Sign In to add comment