Advertisement
gabbyshimoni

controlRGB via joystick

Feb 3rd, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. /*
  2.    03.02.2019
  3.    Written by: Gabby Shimoni
  4.    Description:
  5.    This program makes and RGB led shine based on input from joystick as follows:
  6.    X value control the RED led
  7.    Y axis controls the GREEN led
  8.    pb turns the BLUE led ON
  9. */
  10.  
  11. #define redLedPin 9
  12. #define greenLedPin 11
  13. #define blueLedPin 10
  14.  
  15. #define pbPin 2
  16. int pbState = 0;
  17. #define potXpin A0
  18. #define potYpin A1
  19. int potXval = 0, potYval = 0;
  20.  
  21. void setup() {
  22.   Serial.begin(9600);
  23.   pinMode(redLedPin, OUTPUT);
  24.   pinMode(greenLedPin, OUTPUT);
  25.   pinMode(blueLedPin, OUTPUT);
  26.   pinMode(pbPin, INPUT);
  27. }
  28.  
  29. void loop() {
  30.   potXval = analogRead(potXpin);
  31.   potYval = analogRead(potYpin);
  32.   pbState = digitalRead(pbPin);
  33.  
  34.   potXval = map(potXval, 0, 1023, 0, 255);
  35.   potYval = map(potYval, 0, 1023, 0, 255);
  36.  
  37.   analogWrite(redLedPin, potXval);
  38.   analogWrite(greenLedPin, potYval);
  39.   if (pbState==HIGH) {
  40.     digitalWrite(blueLedPin, HIGH);
  41.   }
  42.   else {
  43.     digitalWrite(blueLedPin, LOW);
  44.   }
  45.   delay(200);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement