/////////////////////////////////////////////////////////////////////////
// Input Expander Module                                               //
// for MCU Trainer                                                     //
//                                                                     //
// This sample program makes the module a binary counter.              //
// A sample circuit is shown in this folder of how                     //
// the input expander may be tested independently without              //
// an output expander module.                                          //
//                                                                     //
// This test program checks the functionality of the input expander    //
// module using the software's serial monitor and LED indicators.      //
//                                                                     //
// JP2 pin assignments:                                                //
// Dout on digital pin 11                                              //
// Clk on digital pin 12                                               //
// Str on digital pin 13                                               //
//                                                                     //
// Codes by:                                                           //
// eGizmo Mechatronix Central                                          //
// Manila, Philippines                                                 //
// April 4, 2013                                                       //
//                                                                     //
/////////////////////////////////////////////////////////////////////////
 
// constants won't change. They're used here to 
// set pin numbers:
const int INCLK = 12;     // input expander CLK pin
const int DIN =  11;      // input expander DOUT pin
const int INSTR =  13;      // input expander STR pin
const int OUTCLK = A2;     // output expander CLK pin
const int DOUT =  A1;      // output expander DIN pin
const int OUTSTR =  A0;      // output expander STR pin
 
// Set delay, bytes, counter, and mask.
int d = 100;
byte byte1;
byte counter;
byte mask;
 
 
void setup() 
{          
  // Initialize serial monitor with 9600 baud.
  Serial.begin(9600);  
  // Set pins to output i stands for increment.
  pinMode(INCLK, OUTPUT);     // Set Clock in as output
  pinMode(DIN, INPUT);     // Set Data in as input
  pinMode(INSTR, OUTPUT);     // Set Strobe in as output
  pinMode(OUTCLK, OUTPUT);     // Set Clock out as output
  pinMode(DOUT, OUTPUT);     // Set Data out as output
  pinMode(OUTSTR, OUTPUT);     // Set Strobe out as output
}
 
 
// Set clock in cycle
void CLKIN(){
  digitalWrite(INCLK, HIGH);
  digitalWrite(INCLK, LOW);
}  
 
// Set clock out cycle
void CLKOUT(){
  digitalWrite(OUTCLK, HIGH);
  digitalWrite(OUTCLK, LOW);
}  
 
// Set strobe in cycle
void STRIN(){
  digitalWrite(INSTR, HIGH);
  digitalWrite(INSTR, LOW);
}  
 
// Set strobe out cycle
void STROUT(){
  digitalWrite(INSTR, HIGH);
  digitalWrite(INSTR, LOW);
}  
 
 
 
// Set LED outputs
void out_LEDs()
{
  for(mask=1; mask>0;)
  { 
    if(byte1 & mask)
      digitalWrite(DOUT, HIGH);
    else  
      digitalWrite(DOUT, LOW);
    CLKOUT();
    mask = mask << 1;
  }
  STROUT();
}
 
 
 
byte read_LEDs()
{
  byte byte1 = 0;
 
  digitalWrite(INSTR, LOW);
  CLKIN();
  digitalWrite(INSTR, HIGH);
  for(mask=1; mask>0;)
  { 
    if(digitalRead(DIN) == HIGH)
      byte1 |= mask; 
    CLKIN();
    mask = mask << 1;
  }
  return byte1; // Returns the value
}
 
 
 
void loop() 
{
byte reading = read_LEDs(); 
// Set reading as the variable for LED output
Serial.println(reading);
 
}
