Wait for input...

Ok, I have sorted out the code now...

I sorted it out after reading the reference files in more detail (eventually)...

/* RGB Keyboard no serial v3

 *

 * Board may need resetting after turning on, not sure why but all 3 LEDs occasionally light up full when a key is pressed after turn on.

 *

 * Robin Whitfield

 * 25th June 2009

 *

 * Approx 2150 bytes

 */

 

#include "binary.h"

typedef uint8_t boolean;

typedef uint8_t byte;

#include <PS2Keyboard.h>



#define DATA_PIN 4 // Define keyboard data pin

PS2Keyboard keyboard;

int LEDR = 11; // Assign Red LED pin

int LEDG = 10; // Assign Green LED pin

int LEDB = 9; // Assign Blue LED pin

int r = 0; // Zero Red value

int g = 0; // Zero Green value

int b = 0; // Zero Blue value



void setup() {

  pinMode(LEDR, OUTPUT);  // Set Red LED pin as output

  pinMode(LEDG, OUTPUT);  // Set Green LED pin as output

  pinMode(LEDB, OUTPUT);  // Set Blue LED pin as output

  

  keyboard.begin(DATA_PIN); // Set up keyboard

}



int keyIn(){

  int key1 = 0; // Zero value for key1

  int key2 = 0; // Zero value for key2

  int key3 = 0; // Zero value for key3

  

  while(!keyboard.available()) {} // When keyboard is available:

  key1 = keyboard.read() -'0'; // Read data and assign to key1

  while(!keyboard.available()) {} // When keyboard is available

  key2 = keyboard.read() -'0'; // Read data and assign to key2

  while(!keyboard.available()) {} // When keyboard is available

  key3 = keyboard.read() -'0'; // Read data and assign to key3

  

  return ((key1*100)+(key2*10)+key3); // Turn key1, key2 and key3 into intended 3 digit number

  

}



void loop() {



  r = keyIn(); // run 'keyIn' to get Red value

  g = keyIn(); // run 'keyIn' to get Green value

  b = keyIn(); // run 'keyIn' to get Blue value

 

  r=min(r, 255); // Assign value to 255 if greater

  g=min(g, 255); // Assign value to 255 if greater

  b=min(b, 255); // Assign value to 255 if greater

  

  analogWrite(LEDR, r); // Write value to Red LED

  analogWrite(LEDG, g); // Write value to Blue LED

  analogWrite(LEDB, b); // Write value to Green LED

}

Mowcius