Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <MCP4922.h> //DAC library from https://github.com/helgenodland/MCP4922-Arduino-SPI-Library
  2. #include <SPI.h> //Serial Peripheral Interface library used by the DAC library
  3.  
  4. MCP4922 DAC(51, 52, 53, 5); // (MOSI,SCK,CS,LDAC) define Connections for MEGA_board,
  5. //MCP4922 DAC(11,13,10,5); // (MOSI,SCK,CS,LDAC) define Connections for UNO_board,
  6.  
  7. int sampling_points = 50; //Define how many sampling points are going to draw the circle.
  8.  
  9. boolean new_data; //trigger boolean
  10. int life = 0; //decay of the circle
  11.  
  12. int r = 4, g = 3, b = 2; //Define color pins
  13. int r_v = 1, g_v = 0, b_v = 0; //Define color values
  14.  
  15. void setup()
  16. {
  17. Serial.begin(19200); //Begin serial to connect with Processing
  18. SPI.begin();
  19.  
  20. //Set color pins as outputs
  21. pinMode(r, OUTPUT);
  22. pinMode(g, OUTPUT);
  23. pinMode(b, OUTPUT);
  24.  
  25. }
  26.  
  27. void loop()
  28. {
  29. setColors(); //Set colors
  30. recvOneChar(); //Receive trigger
  31.  
  32. for (int i = 0; i < sampling_points; i++) {
  33. int x = map(int(sin(i * TWO_PI / sampling_points) * (500 + 5 * life)), -1000, 1000, 0, 4095); //Map sine to DAC code values.
  34. int y = map(int(cos(i * TWO_PI / sampling_points) * (500 + 5 * life)), -1000, 1000, 0, 4095); //Map sine to DAC code values.
  35. DAC.Set(x, y); // Send to DAC
  36. }
  37. if (new_data) life -= 5; //Decay
  38. if (life <= 0) new_data = false; //Remove trigger when decay is over.
  39.  
  40. }
  41.  
  42. void recvOneChar() {
  43. if (Serial.available() > 0) { //If there's a trigger
  44. char receivedChar = Serial.read(); //Flush the buffer by reading the char.
  45. new_data = true; //Set trigger on
  46. life = 100; //Set life to maximum
  47. }
  48. }
  49.  
  50. void setColors() {
  51. digitalWrite(r, r_v);
  52. digitalWrite(g, g_v);
  53. digitalWrite(b, b_v);
  54. }
  55.  
  56. void resetColors() {
  57. digitalWrite(r, LOW);
  58. digitalWrite(g, LOW);
  59. digitalWrite(b, LOW);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement