Advertisement
igendel

Untitled

Mar 6th, 2015
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. // Sample code for ICStation.com's PS2 Keyboard Interface Module
  2. // For the Arduino Uno, using the UART (serial)
  3. // by Ido Gendel, 2015
  4. // Share and enjoy!
  5.  
  6. // This is adapted to a common-anode RGB LED, so everything is in negative logic.
  7. // The Red, Green and Blue legs of the LED are connected to pin 9, 10 & 11
  8.  
  9. // Make sure to disconnect the module from the Arduino while uploading this sketch!
  10.  
  11. byte color[3] = {255, 255, 255};
  12. byte *ref = NULL;
  13.  
  14. void red() { ref = &color[0]; }
  15.  
  16. void green() { ref = &color[1]; }
  17.  
  18. void blue() { ref = &color[2]; }
  19.  
  20. void update() {
  21.   for (byte j = 0; j < 3; j++)  analogWrite(j + 9, color[j]);
  22. }
  23.  
  24. // Common Anode - Negative logic!
  25. void less() {
  26.   if (ref && (*ref < 255)) (*ref)++;
  27.   update();
  28. }
  29.  
  30. // Common Anode - Negative logic!
  31. void more() {
  32.   if (ref && (*ref > 0)) (*ref)--;
  33.   update();  
  34. }
  35.  
  36. void keyState() {
  37.   digitalWrite(13, digitalRead(2));
  38. }
  39.  
  40. void setup() {
  41.  
  42.   for (byte j = 9; j <= 13; j++)  pinMode(j, OUTPUT);
  43.   pinMode(2, INPUT);
  44.   attachInterrupt(0, keyState, CHANGE);
  45.   digitalWrite(13, LOW);
  46.   update();
  47.   Serial.begin(19200);
  48.  
  49. }
  50.  
  51. void loop() {
  52.  
  53.   while (!Serial.available());
  54.   char c = Serial.read();
  55.   switch (c) {
  56.     case 45: red(); break;
  57.     case 52: green(); break;    
  58.     case 50: blue(); break;
  59.     case 121: more(); break;
  60.     case 123: less(); break;
  61.   }
  62.   Serial.println(c, DEC);
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement