igendel icon

Untitled

igendel | PRO | 03/07/15 01:08:47 AM UTC | 0 ⭐ | 1329 👁️ | Never ⏰ | []
C++ |

1.39 KB

|

None

|

0 👍

/

0 👎

// Sample code for ICStation.com's PS2 Keyboard Interface Module 
// For the Arduino Uno, using the UART (serial)
// by Ido Gendel, 2015
// Share and enjoy!
 
// This is adapted to a common-anode RGB LED, so everything is in negative logic.
// The Red, Green and Blue legs of the LED are connected to pin 9, 10 & 11
 
// Make sure to disconnect the module from the Arduino while uploading this sketch!
 
byte color[3] = {255, 255, 255};
byte *ref = NULL;
 
void red() { ref = &color[0]; }
 
void green() { ref = &color[1]; }
 
void blue() { ref = &color[2]; }
 
void update() {
  for (byte j = 0; j < 3; j++)  analogWrite(j + 9, color[j]);
}
 
// Common Anode - Negative logic!
void less() {
  if (ref && (*ref < 255)) (*ref)++;
  update();
}
 
// Common Anode - Negative logic!
void more() {
  if (ref && (*ref > 0)) (*ref)--;
  update();  
}
 
void keyState() {
  digitalWrite(13, digitalRead(2));
}
 
void setup() {
 
  for (byte j = 9; j <= 13; j++)  pinMode(j, OUTPUT);
  pinMode(2, INPUT);
  attachInterrupt(0, keyState, CHANGE);
  digitalWrite(13, LOW);
  update();
  Serial.begin(19200);
  
}
 
void loop() {
 
  while (!Serial.available());
  char c = Serial.read();
  switch (c) {
    case 45: red(); break;
    case 52: green(); break;    
    case 50: blue(); break;
    case 121: more(); break;
    case 123: less(); break;
  }
  Serial.println(c, DEC);
  
}

Comments