Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ps2.h>
- #include <FastLED.h>
- /*
- * an arduino sketch to interface with a ps/2 mouse.
- * Also uses serial protocol to talk back to the host
- * and report what it finds.\
- * Vector is cacluated based on running mouse coordinates. Vector angle is mapped to RGB colors to light up small ring
- */
- /*
- * Pin 5 is the mouse data pin, pin 6 is the clock pin
- * Feel free to use whatever pins are convenient.
- */
- //setup ports for trackball
- PS2 mouse(6, 5);
- //define pins and defaults for LEDs
- #define DATA_PIN_RING 11
- #define DATA_PIN_BALL 12
- #define DATA_PIN_DESK 7
- #define DATA_PIN_ART1 8
- #define DATA_PIN_ART2 9
- #define DATA_PIN_ART3 10
- //#define CLK_PIN 4
- #define LED_TYPE WS2813
- #define LED2_TYPE WS2811
- #define COLOR_ORDER GRB
- #define NUM_LEDS_RING 48
- #define NUM_LEDS_BALL 8
- #define NUM_LEDS_DESK 497
- #define NUM_LEDS_ART1 497
- #define NUM_LEDS_ART2 955
- #define NUM_LEDS_ART3 535
- #define BRIGHTNESS 200
- #define FRAMES_PER_SECOND 20
- //name each led set
- CRGB ring[NUM_LEDS_RING];
- CRGB ball[NUM_LEDS_BALL];
- CRGB desk[NUM_LEDS_DESK];
- CRGB art1[NUM_LEDS_ART1];
- CRGB art2[NUM_LEDS_ART2];
- CRGB art3[NUM_LEDS_ART3];
- //define button constants
- const int button1 = 13; // define button 1
- const int button2 = 14; // define button 2
- const int button3 = 15; // define button 3
- //define buttonstate variables
- int buttonState1 = 0; // variable for reading the pushbutton status
- int buttonState2 = 0; // variable for reading the pushbutton status
- int buttonState3 = 0; // variable for reading the pushbutton status
- //setup variables
- int X = 0;
- int Y = 0;
- double ang = 0;
- int dAng = 0;
- char mstat;
- char mx;
- char my;
- int Xabs;
- int Yabs;
- int sat = 255;
- int satold;
- /*
- * initialize the mouse. Reset it, and place it into remote
- * mode, so we can get the encoder data on demand.
- */
- void mouse_init()
- {
- mouse.write(0xff); // reset
- mouse.read(); // ack byte
- mouse.read(); // blank */
- mouse.read(); // blank */
- mouse.write(0xf0); // remote mode
- mouse.read(); // ack
- delayMicroseconds(100);
- }
- void setup()
- {
- Serial.begin(9600);
- Serial.println("begin");
- mouse_init();
- //Setup ports
- pinMode(5,OUTPUT);
- pinMode(6,OUTPUT);
- pinMode(7,OUTPUT);
- pinMode(8,OUTPUT);
- pinMode(9,OUTPUT);
- pinMode(10,OUTPUT);
- pinMode(11,OUTPUT);
- pinMode(12,OUTPUT);
- pinMode(13,OUTPUT);
- //setup for LEDs
- delay(1000); // 2 second delay for recovery
- // tell FastLED about the LED strip configuration
- FastLED.addLeds<LED2_TYPE,DATA_PIN_RING,COLOR_ORDER>(ring, NUM_LEDS_RING).setCorrection(TypicalLEDStrip); // define color ring around trackball
- FastLED.addLeds<LED2_TYPE,DATA_PIN_BALL,COLOR_ORDER>(ball, NUM_LEDS_BALL).setCorrection(TypicalLEDStrip); // defines led ring illuminating trackball ball
- 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)
- 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)
- 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)
- 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)
- // set master brightness control
- FastLED.setBrightness(BRIGHTNESS);
- fill_solid(desk, NUM_LEDS_DESK, CRGB::Black);
- fill_solid(art1, NUM_LEDS_ART1, CRGB::Black);
- fill_solid(art2, NUM_LEDS_ART2, CRGB::Black);
- fill_solid(art3, NUM_LEDS_ART3, CRGB::Black);
- rainbow(); // set ring to rainbow
- Serial.println(" INIT ");
- }
- void loop()
- {
- // read the state of the pushbutton value:
- buttonState1 = digitalRead(button1);
- buttonState2 = digitalRead(button2);
- buttonState3 = digitalRead(button3);
- // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
- if (buttonState1 == HIGH) {
- // turn LED on:
- fill_solid(art1, NUM_LEDS_ART1, CHSV(dAng,sat,200));
- //FastLED.show();
- Serial.println("BTN 1 Pressed");
- }
- if (buttonState2 == HIGH) {
- // turn LED on:
- fill_solid(art2, NUM_LEDS_ART2, CHSV(dAng,sat,200));
- //FastLED.show();
- Serial.println("BTN 2 Pressed");
- }
- if (buttonState3 == HIGH) {
- // turn LED on:
- fill_solid(art3, NUM_LEDS_ART3, CHSV(dAng,sat,200));
- //FastLED.show();
- Serial.println("BTN 3 Pressed");
- }
- // /* get a reading from the mouse */
- mouse.write(0xeb); // give me data!
- mouse.read(); // ignore ack
- mstat = mouse.read();
- mx = mouse.read();
- my = mouse.read();
- X = (X+mx); //add last reading from trackball to current X position
- Y = (Y+my); //add last reading from trackball to current Y position
- X = constrain(X,-255,255); //constrain so as not to get to far away from 0,0
- Y = constrain(Y,-255,255); //constrain so as not to get to far away from 0,0
- // //calculate saturation
- Xabs = abs(X);
- Yabs = abs(Y);
- sat = max(Xabs,Yabs);
- // //Center saturated
- sat = map(sat,0,255,255,60); //limit saturation so that it never goes fully white
- // //Edge saturated
- // sat = map(sat,0,255,60,255); //limit saturation so that it never goes fully white
- // //Calculate vector (-180 to 180 degrees) and map to hue (0-254)
- ang = atan2(Y,X)* 57.2957795131;
- dAng = map(ang,-180,180,254,0);
- balldesk(); //update ball and desk LEDs with current color(dAng) and saturation(sat)
- FastLED.show();
- FastLED.delay(50); //procedural delay
- //
- //
- // Debug trackball input
- // Serial.print(mstat, BIN);
- // Serial.print("\tmx=");
- // Serial.print(mx, DEC);
- // Serial.print("\tmy=");
- // Serial.print(my, DEC);
- // Serial.print("\tX=");
- // Serial.print(X, DEC);
- // Serial.print("\tY=");
- // Serial.print(Y, DEC);
- // Serial.print("\tDEG=");
- // Serial.print(ang, DEC);
- // Serial.print("\tCOLOR=");
- // Serial.print(dAng, DEC);
- // Serial.println();
- //delay(20); /* twiddle */
- }
- void rainbow()
- {
- // FastLED's built-in rainbow generator
- fill_rainbow_circular(ring, NUM_LEDS_RING, 0, 0);
- }
- void balldesk()
- {
- fill_solid(desk, NUM_LEDS_DESK, CHSV(dAng,sat,255)); //fills desk led array with color chosen by trackball
- fill_solid(ball, NUM_LEDS_BALL, CHSV(dAng,sat,255)); //fills leds under trackball with color chosen by trackball
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement