Guest User

Untitled

a guest
Jul 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. //Note that all the color value variables are used to dim the respective LED.
  2. //Meaning, the axis on the joystick associated with redValue will dim RED
  3. //instead of making that direction RED.
  4.  
  5.  
  6. // const int SW_pin = 2; //Switch currently unused
  7. const int X_pin = 0; // VRx connected to A0
  8. const int Y_pin = 1; // VRy connected to A1
  9. #define RED 11 // Red to Digital 11
  10. #define BLUE 10 // Blue to Digital 10
  11. #define GREEN 9 // Green to Digital 9
  12.  
  13.  
  14.  
  15. void setup() {
  16. // pinMode(SW_pin, INPUT); //Swith currently unused
  17. // digitalWrite(SW_pin, HIGH); //Swith currently unused
  18. // Serial.begin(9600); //Serial was used for debugging. Turn on to see values.
  19.  
  20. pinMode(RED, OUTPUT);
  21. pinMode(GREEN, OUTPUT);
  22. pinMode(BLUE, OUTPUT);
  23. }
  24.  
  25. void loop() {
  26. int x = 0;
  27. int y = 0;
  28. x = analogRead(X_pin); //Read VRx normally
  29. y = abs(1023-(analogRead(Y_pin))); //Invert VRy read, as the module is inverted
  30.  
  31. int redValue = 0;
  32. int greenValue = 0;
  33. int blueValue = 0;
  34. int xValue = 0; //xValue is later reffered to as "douse," as it's main role is to douse the red and and blue LEDs
  35.  
  36. xValue = (map(y, 512, 1023, 0, 255)); //Douse is the upper half of the Y axis
  37. xValue = constrain (xValue, 0, 255); //Set the upper half range to 0 thru 255
  38. greenValue = (map(y, 0, 512, 0, 255)); //Set the green control to the lower half of the Y axis
  39. redValue = (map(x, 0, 512, 0, 255))-xValue; //Set the red control to the left half of the X axis, then subtract the "Douse" value
  40. blueValue = (map(x, 512, 1023, 255, 0))-xValue; //Set the blue control to the right half of the X axis, then subtract the "Douse" value
  41.  
  42. blueValue = constrain(blueValue, 0, 255); //Make sure the range of power for each output is 0 to 255
  43. redValue = constrain(redValue, 0, 255);
  44. greenValue = constrain(greenValue, 0, 255);
  45.  
  46. analogWrite(RED, redValue); //Make the lights turn on.
  47. analogWrite(BLUE, blueValue); // :)
  48. analogWrite(GREEN, greenValue);
  49.  
  50. Serial.print("X-axis: "); //I'm letting this still execute, but nothing will display
  51. Serial.println((x)); //until the Serial.begin(baud) is turned back on.
  52. Serial.print("Y-axis: ");
  53. Serial.println((y));
  54. Serial.print("Red=");
  55. Serial.println(redValue);
  56. Serial.print("Blue=");
  57. Serial.println(blueValue);
  58. Serial.print("Green=");
  59. Serial.println(greenValue);
  60. Serial.print("Douse="); //Remember, "DOUSE" is the xValue and it's role is to turn off red and blue LEDs
  61. Serial.println(xValue);
  62. Serial.print("\n");
  63.  
  64. // delay(500); //Used in debugging so one can read the serial output. Keep off to keep LED control real-time
  65. }
Add Comment
Please, Sign In to add comment