//////////////////////////////////////////////////////////////////////////////////////////                                                                                      //
//  Output Expander Module for eGizmo Universal MCU Trainer                             //
//                                                                                      //
// This program converts a serial into a parallel output with 8 output ports per board. //
// Each output ports has LED state indicators.                                          //
// +3.3V or +5V of the microcontroller may be used.                                     //
// This module is usually used with an input expander module.                           //
//                                                                                      //
// Pin assignments:                                                                     //
// Din on digital pin 11                                                                //
// Clk on digital pin 12                                                                //
// Str on digital pin 13                                                                //
//                                                                                      //
// Connect the outputs of the MCU trainer to the headers of the LED Monitor.            //
// 0 on D0... respectively.                                                             //
//                                                                                      //
// Codes by:                                                                            //
// eGizmo Mechatronix Central                                                           //
// Manila, Philippines                                                                  //
// April 3, 2013                                                                        //
//                                                                                      //
//                                                                                      //
//////////////////////////////////////////////////////////////////////////////////////////
 
// Constants
const int StrIN = 13;
const int ClkIN = 12;
const int DataIN =  11;
 
 
void setup()
{
  pinMode(DataIN, OUTPUT); // Pin 8 as output
  pinMode(ClkIN, OUTPUT); // Pin 9 as output
  pinMode(StrIN, OUTPUT); // Pin 10 as output
}
 
 
void loop()
{
  for(int i = 0; i<=256; i++)
  {
    shiftOut(i);
    // Set cycle for strobe in
    digitalWrite(StrIN, HIGH);
    digitalWrite(StrIN,LOW);
    delay(500);
  }
}
 
void shiftOut(byte DataOUT) // Sets a byte called DataOUT
{
  boolean state;
  
  // Sets the cycle for data in and clock in
  digitalWrite(DataIN, LOW);
  digitalWrite(ClkIN, LOW);
 
  for (int i=0; i<=7; i++) 
  // This is set for 8bit binary.
  {
    digitalWrite(ClkIN, LOW);
    if (DataOUT&(1<<i)) 
    {
      state = HIGH;
    }
    else 
    {
      state = LOW;
    }
 
    digitalWrite(DataIN, state);
    digitalWrite(ClkIN, HIGH);
  }
 
  digitalWrite(ClkIN,LOW);
  }
  
