Advertisement
Purkolator

Feelings PS2

Jan 26th, 2024 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 6.57 KB | Source Code | 0 0
  1. #include <ps2.h>
  2. #include <FastLED.h>
  3. /*
  4.  * an arduino sketch to interface with a ps/2 mouse.
  5.  * Also uses serial protocol to talk back to the host
  6.  * and report what it finds.\
  7.  * Vector is cacluated based on running mouse coordinates. Vector angle is mapped to RGB colors to light up small ring
  8.  */
  9.  
  10. /*
  11.  * Pin 5 is the mouse data pin, pin 6 is the clock pin
  12.  * Feel free to use whatever pins are convenient.
  13.  */
  14. //setup ports for trackball
  15. PS2 mouse(6, 5);
  16.  
  17. //define pins and defaults for LEDs
  18. #define DATA_PIN_RING   11
  19. #define DATA_PIN_BALL   12
  20. #define DATA_PIN_DESK   7
  21. #define DATA_PIN_ART1   8
  22. #define DATA_PIN_ART2   9
  23. #define DATA_PIN_ART3   10
  24. //#define CLK_PIN   4
  25. #define LED_TYPE WS2813
  26. #define LED2_TYPE WS2811
  27. #define COLOR_ORDER GRB
  28. #define NUM_LEDS_RING 48
  29. #define NUM_LEDS_BALL 8
  30. #define NUM_LEDS_DESK 497
  31. #define NUM_LEDS_ART1 497
  32. #define NUM_LEDS_ART2 955
  33. #define NUM_LEDS_ART3 535
  34. #define BRIGHTNESS         200
  35. #define FRAMES_PER_SECOND   20
  36.  
  37. //name each led set
  38. CRGB ring[NUM_LEDS_RING];
  39. CRGB ball[NUM_LEDS_BALL];
  40. CRGB desk[NUM_LEDS_DESK];
  41. CRGB art1[NUM_LEDS_ART1];
  42. CRGB art2[NUM_LEDS_ART2];
  43. CRGB art3[NUM_LEDS_ART3];
  44.  
  45. //define button constants
  46. const int button1 = 13;  // define button 1
  47. const int button2 = 14;  // define button 2
  48. const int button3 = 15;  // define button 3
  49.  
  50. //define buttonstate variables
  51. int buttonState1 = 0;  // variable for reading the pushbutton status
  52. int buttonState2 = 0;  // variable for reading the pushbutton status
  53. int buttonState3 = 0;  // variable for reading the pushbutton status
  54.  
  55.  
  56. //setup variables
  57. int X = 0;
  58. int Y = 0;
  59. double ang = 0;
  60. int dAng = 0;
  61. char mstat;
  62. char mx;
  63. char my;
  64. int Xabs;
  65. int Yabs;
  66. int sat = 255;
  67. int satold;
  68.  
  69. /*
  70.  * initialize the mouse. Reset it, and place it into remote
  71.  * mode, so we can get the encoder data on demand.
  72.  */
  73. void mouse_init()
  74. {
  75.   mouse.write(0xff);  // reset
  76.   mouse.read();  // ack byte
  77.   mouse.read();  // blank */
  78.   mouse.read();  // blank */
  79.   mouse.write(0xf0);  // remote mode
  80.   mouse.read();  // ack
  81.   delayMicroseconds(100);
  82. }
  83.  
  84. void setup()
  85. {
  86.   Serial.begin(9600);
  87.   Serial.println("begin");
  88.   mouse_init();
  89.  
  90.   //Setup ports
  91.   pinMode(5,OUTPUT);
  92.   pinMode(6,OUTPUT);
  93.   pinMode(7,OUTPUT);
  94.   pinMode(8,OUTPUT);
  95.   pinMode(9,OUTPUT);
  96.   pinMode(10,OUTPUT);
  97.   pinMode(11,OUTPUT);
  98.   pinMode(12,OUTPUT);
  99.   pinMode(13,OUTPUT);
  100.   //setup for LEDs
  101.   delay(1000); // 2 second delay for recovery
  102.  
  103.   // tell FastLED about the LED strip configuration
  104.   FastLED.addLeds<LED2_TYPE,DATA_PIN_RING,COLOR_ORDER>(ring, NUM_LEDS_RING).setCorrection(TypicalLEDStrip); // define color ring around trackball
  105.   FastLED.addLeds<LED2_TYPE,DATA_PIN_BALL,COLOR_ORDER>(ball, NUM_LEDS_BALL).setCorrection(TypicalLEDStrip); // defines led ring illuminating trackball ball
  106.   FastLED.addLeds<LED_TYPE,DATA_PIN_DESK,COLOR_ORDER>(desk, NUM_LEDS_DESK).setCorrection(TypicalLEDStrip); // define LEDs illuminating desk panels (LEDs 16,17 and 18 are stand ins for LED arrays for artifact panels)
  107.   FastLED.addLeds<LED_TYPE,DATA_PIN_ART1,COLOR_ORDER>(art1, NUM_LEDS_ART1).setCorrection(TypicalLEDStrip); // define LEDs illuminating desk panels (LEDs 16,17 and 18 are stand ins for LED arrays for artifact panels)
  108.   FastLED.addLeds<LED_TYPE,DATA_PIN_ART2,COLOR_ORDER>(art2, NUM_LEDS_ART2).setCorrection(TypicalLEDStrip); // define LEDs illuminating desk panels (LEDs 16,17 and 18 are stand ins for LED arrays for artifact panels)
  109.   FastLED.addLeds<LED_TYPE,DATA_PIN_ART3,COLOR_ORDER>(art3, NUM_LEDS_ART3).setCorrection(TypicalLEDStrip); // define LEDs illuminating desk panels (LEDs 16,17 and 18 are stand ins for LED arrays for artifact panels)
  110.  
  111.   // set master brightness control
  112.   FastLED.setBrightness(BRIGHTNESS);
  113.   fill_solid(desk, NUM_LEDS_DESK, CRGB::Black);
  114.   fill_solid(art1, NUM_LEDS_ART1, CRGB::Black);
  115.   fill_solid(art2, NUM_LEDS_ART2, CRGB::Black);
  116.   fill_solid(art3, NUM_LEDS_ART3, CRGB::Black);
  117.   rainbow();  // set ring to rainbow
  118.    Serial.println("    INIT    ");
  119. }
  120.  
  121. void loop()
  122. {
  123.  
  124. // read the state of the pushbutton value:
  125. buttonState1 = digitalRead(button1);
  126. buttonState2 = digitalRead(button2);
  127. buttonState3 = digitalRead(button3);
  128.  
  129. // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  130. if (buttonState1 == HIGH) {
  131.   // turn LED on:
  132.  fill_solid(art1, NUM_LEDS_ART1, CHSV(dAng,sat,200));
  133.  //FastLED.show();
  134.    Serial.println("BTN 1 Pressed");
  135. }
  136.  
  137. if (buttonState2 == HIGH) {
  138.   // turn LED on:
  139.  fill_solid(art2, NUM_LEDS_ART2, CHSV(dAng,sat,200));
  140.  //FastLED.show();
  141.   Serial.println("BTN 2 Pressed");
  142. }
  143.  
  144. if (buttonState3 == HIGH) {
  145.   // turn LED on:
  146.  fill_solid(art3, NUM_LEDS_ART3, CHSV(dAng,sat,200));
  147.  //FastLED.show();
  148.    Serial.println("BTN 3 Pressed");
  149. }
  150.  
  151.  
  152.  
  153. // /* get a reading from the mouse */
  154. mouse.write(0xeb);  // give me data!
  155. mouse.read();      // ignore ack
  156. mstat = mouse.read();
  157. mx = mouse.read();
  158. my = mouse.read();
  159. X = (X+mx); //add last reading from trackball to current X position
  160. Y = (Y+my); //add last reading from trackball to current Y position
  161. X = constrain(X,-255,255); //constrain so as not to get to far away from 0,0
  162. Y = constrain(Y,-255,255); //constrain so as not to get to far away from 0,0
  163.  
  164. // //calculate saturation
  165. Xabs = abs(X);
  166. Yabs = abs(Y);
  167. sat = max(Xabs,Yabs);
  168.  
  169. //   //Center saturated
  170. sat = map(sat,0,255,255,60); //limit saturation so that it never goes fully white
  171.  
  172. //   //Edge saturated
  173. // sat = map(sat,0,255,60,255); //limit saturation so that it never goes fully white
  174.  
  175. // //Calculate vector (-180 to 180 degrees) and map to hue (0-254)
  176.   ang = atan2(Y,X)* 57.2957795131;
  177.   dAng = map(ang,-180,180,254,0);
  178.  
  179. balldesk(); //update ball and desk LEDs with current color(dAng) and saturation(sat)
  180. FastLED.show();
  181. FastLED.delay(50); //procedural delay
  182. //  
  183. //
  184.  
  185. //  Debug trackball input
  186. //  Serial.print(mstat, BIN);
  187. //  Serial.print("\tmx=");
  188. //  Serial.print(mx, DEC);
  189. //  Serial.print("\tmy=");
  190. //  Serial.print(my, DEC);
  191. //  Serial.print("\tX=");
  192. //  Serial.print(X, DEC);
  193. //  Serial.print("\tY=");
  194. //  Serial.print(Y, DEC);
  195. //  Serial.print("\tDEG=");
  196. //  Serial.print(ang, DEC);
  197. //  Serial.print("\tCOLOR=");
  198. //  Serial.print(dAng, DEC);
  199. //  Serial.println();
  200.  
  201. //delay(20);  /* twiddle */
  202. }
  203.  
  204. void rainbow()
  205. {
  206.   // FastLED's built-in rainbow generator
  207.   fill_rainbow_circular(ring, NUM_LEDS_RING, 0, 0);
  208. }
  209.  
  210. void balldesk()
  211. {
  212. fill_solid(desk, NUM_LEDS_DESK, CHSV(dAng,sat,255)); //fills desk led array with color chosen by trackball
  213. fill_solid(ball, NUM_LEDS_BALL, CHSV(dAng,sat,255)); //fills leds under trackball with color chosen by trackball
  214. }
Tags: FastLED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement