# data[].attributes.headline
Keyboard input 7-segment timer interrupt

# data[].attributes.subject
Keyboard input 7-segment timer interrupt

# data[].attributes.body
bool wasd = true;
bool wasdserial = true;
char Byte1, Byte2, data;

#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define S4 6
#define S5 7
#define S6 8

#define D1 9
#define D2 10

void setup()
{
Serial.begin(9600);

pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(S4, OUTPUT);
pinMode(S5, OUTPUT);
pinMode(S6, OUTPUT);

pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);

noInterrupts();           // disable all interrupts

TCCR1A = 0;
TCCR1B = 0;
TCNT1  = 0;

// compare match register 16MHz/256 * t(s) - 1
OCR1A = 200;

TCCR1B = 0x0D;   // CTC mode

TIMSK1 = 0x22; // enable timer compare interrupt

interrupts();             // enable all interrupts

}

ISR(TIMER1_COMPA_vect) {

if (wasd) {
wasd = false;
SegConvert(0);
digitalWrite(D1, LOW);
digitalWrite(D2, HIGH);
SegConvert(Byte1);

}
else {
wasd = true;
SegConvert(0);
digitalWrite(D1, HIGH);
digitalWrite(D2, LOW);
SegConvert(Byte2);

}
}

void loop()
{
if (Serial.available() > 0) {
// read the incoming byte:
data = Serial.read();

if (wasdserial) {
wasdserial = false;
Byte1 = data;
}
else {
wasdserial = true;
Byte2 = data;
}
}

}

void SegConvert(char SegValue) {        // function 'SegConvert'

switch (SegValue) {                   //DP G F E D C B A
case 0x30 : digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
digitalWrite(S4, HIGH);
digitalWrite(S5, HIGH);
digitalWrite(S6, LOW);
break;      // 0 0 1 1 1 1 1 1 binary
case 0x31 : digitalWrite(S0, LOW);
digitalWrite(S1, HIGH);
digitalWrite(S2, HIGH);
digitalWrite(S3, LOW);
digitalWrite(S4, LOW);
digitalWrite(S5, LOW);
digitalWrite(S6, LOW);
break;      // 0 0 0 0 0 1 1 0 binary
case 0x32 : digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
digitalWrite(S4, HIGH);
digitalWrite(S5, LOW);
digitalWrite(S6, HIGH);
break;      // 0 1 0 1 1 0 1 1 binary
case 0x33 : digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
digitalWrite(S4, LOW);
digitalWrite(S5, LOW);
digitalWrite(S6, HIGH);
break;      // 0 1 0 0 1 1 1 1 binary
case 0x34 : digitalWrite(S0, LOW);
digitalWrite(S1, HIGH);
digitalWrite(S2, HIGH);
digitalWrite(S3, LOW);
digitalWrite(S4, LOW);
digitalWrite(S5, HIGH);
digitalWrite(S6, HIGH);
break;      // 0 1 1 0 0 1 1 0 binary
case 0x35 : digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
digitalWrite(S4, LOW);
digitalWrite(S5, HIGH);
digitalWrite(S6, HIGH);
break;      // 0 1 1 0 1 1 0 1 binary
case 0x36 : digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
digitalWrite(S4, HIGH);
digitalWrite(S5, HIGH);
digitalWrite(S6, HIGH);
break;      // 0 1 1 1 1 1 0 1 binary
case 0x37 :
digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
digitalWrite(S2, HIGH);
digitalWrite(S3, LOW);
digitalWrite(S4, LOW);
digitalWrite(S5, LOW);
digitalWrite(S6, LOW);
break;      // 0 0 0 0 0 1 1 1 binary
case 0x38 : digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
digitalWrite(S4, HIGH);
digitalWrite(S5, HIGH);
digitalWrite(S6, HIGH);
break;      // 0 1 1 1 1 1 1 1 binary
case 0x39 : digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
digitalWrite(S4, LOW);
digitalWrite(S5, HIGH);
digitalWrite(S6, HIGH);
break;      // 0 1 1 0 1 1 1 1 binary
case 0:
digitalWrite(S0, LOW);
digitalWrite(S1, LOW);
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
digitalWrite(S4, LOW);
digitalWrite(S5, LOW);
digitalWrite(S6, LOW);
}
}

# data[].attributes.body
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;

int digit1 = 10;
int digit2 = 9;

char Num[10] = {
0b1111110, // 0
0b0110000, // 1
0b1101101, // 2
0b1111001, // 3
0b0110011, // 4
0b1011011, // 5
0b1011111, // 6
0b1110000, // 7
0b1111111, // 8
0b1111011  // 9
};

void number(int n)
{
digitalWrite(a, n & 0b1000000);
digitalWrite(b, n & 0b0100000);
digitalWrite(c, n & 0b0010000);
digitalWrite(d, n & 0b0001000);
digitalWrite(e, n & 0b0000100);
digitalWrite(f, n & 0b0000010);
digitalWrite(g, n & 0b0000001);
}

void setup()
{
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(digit1, OUTPUT);
pinMode(digit2, OUTPUT);

digitalWrite(digit1, 0); // disable digit1
digitalWrite(digit2, 1); // disable digit2
}

int count = 84;
void loop()
{

int d1 = count / 10;
int d2 = count % 10;

digitalWrite(digit2, 1); // Off digit2
digitalWrite(digit1, 1);
number(Num[d1]);
digitalWrite(digit1, 0);
delay(10); // 1

digitalWrite(digit2, 1); // Off digit2
digitalWrite(digit1, 1); // Off digit1

number(Num[d2]);
digitalWrite(digit2, 0);
delay(10); // 2

}

# data[].attributes.headline
Using right shift >> then AND with 00000001

# data[].attributes.subject
Using right shift >> then AND with 00000001

# data[].attributes.body
void showNum(char number);
char getSegByte(char segValue);

void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}

void loop()
{
for(int i = 0; i<10; i++){
showNum(getSegByte(i));
delay(500);
}
}

char getSegByte(char segValue){
char segByte = 0x00;
switch(segValue){
case 0 : segByte = 0x3F;break;
case 1 : segByte = 0x06;break;
case 2 : segByte = 0x5B;break;
case 3 : segByte = 0x4F;break;
case 4 : segByte = 0x66;break;
case 5 : segByte = 0x6D;break;
case 6 : segByte = 0x7D;break;
case 7 : segByte = 0x07;break;
case 8 : segByte = 0x7F;break;
case 9 : segByte = 0x6F;break;
}
return segByte;
}

int firstPin = 0; //first pin connected to A
void showNum(char number){
for(int digit = 0; digit<7; digit++){
int bit = (number >> digit) & 1;
if(bit == 1){
digitalWrite(digit+firstPin, HIGH);
}
else
digitalWrite(digit+firstPin, LOW);
}

}

# data[].attributes.body
int led = 11;
int left = 12;
int right = 13;
int leftState = 0;
int rightState = 0;
int brightness = 0;
int interval = 12.75;

void setup()
{
pinMode(led, OUTPUT);
pinMode(left, INPUT);
pinMode(right, INPUT);
}

void loop()
{
analogWrite(led, brightness);
leftState = digitalRead (left);
rightState = digitalRead (right);
if(leftState == HIGH){
brightness = brightness - interval;
}
else if (rightState == HIGH){
brightness = brightness + interval;
}
if((brightness<0) || (brightness>255)){
brightness = 0;
}
delay(100);
}

# data[].attributes.body
// make an array to save Sev Seg pin configuration of numbers

int num_array[10][7] = {  { 1,1,1,1,1,1,0 },    // 0
{ 0,1,1,0,0,0,0 },    // 1
{ 1,1,0,1,1,0,1 },    // 2
{ 1,1,1,1,0,0,1 },    // 3
{ 0,1,1,0,0,1,1 },    // 4
{ 1,0,1,1,0,1,1 },    // 5
{ 1,0,1,1,1,1,1 },    // 6
{ 1,1,1,0,0,0,0 },    // 7
{ 1,1,1,1,1,1,1 },    // 8
{ 1,1,1,0,0,1,1 }};   // 9

//function header
void Num_Write(int);

void setup()
{
// set pin modes
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);

}

void loop()
{

//counter loop

delay(1000);
Num_Write(0);

delay(3000);
}

// this functions writes values to the sev seg pins
void Num_Write(int number)
{
int pin= 2;
for (int j=0; j < 7; j++) {
digitalWrite(pin, num_array[number][j]);
pin++;
}
}

# data[].attributes.body
#include <Wire.h>

int switch_ip1=8;
int switch_ip2=9;
int red_led=2;
int green_led=3;
int switch_word = 0;

void setup()
{
Wire.begin(4);
Wire.onReceive(receiveEvent); // register receive event
Wire.onRequest(requestEvent); // register request event

pinMode(switch_ip1,INPUT);
pinMode(switch_ip2,INPUT);
pinMode(red_led,OUTPUT);
pinMode(green_led,OUTPUT);
}

void loop()
{

}

void receiveEvent(int howMany)
{
int recd_val = Wire.read();
digitalWrite(red_led,0);
digitalWrite(green_led,0);
if (recd_val==1)
digitalWrite(red_led,1);
if (recd_val==2)
digitalWrite(green_led,1);
if (recd_val==3){
digitalWrite(red_led,1);
digitalWrite(green_led,1);
}
}

void requestEvent(){
switch_word=0;
if (digitalRead(switch_ip1)==HIGH)
switch_word=switch_word|0x01;        //OR in lsb
if (digitalRead(switch_ip2)==HIGH)
switch_word=switch_word|0x02;        //OR in next lsb

Wire.write(switch_word);
}

# data[].attributes.body
void setup()
{

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);

pinMode(0, OUTPUT);
pinMode(1, OUTPUT);

}

void loop()
{
digitalWrite(0,HIGH);
digitalWrite(1,LOW);

digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);

delay(10);
digitalWrite(0,LOW);
digitalWrite(1,HIGH);

digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);

delay(10);
}

# data[].attributes.body
#include "mbed.h"

DigitalOut led[] = {(LED1), (LED2), (LED3), (LED4)};

int main() {

while (1) {

for(int i=0;i<=3;i++){

led[i] = !led[i];

wait_ms(500);

led[i] = !led[i];

}

}

}

# data[].attributes.body
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void toggle_enable(void);
void LCD_init(void);
void display_to_LCD(char value);
void set_location(char location);

int RS = 12;
int E = 11;

int pins[] = {5, 4, 3, 2};

void setup() {
pinMode(RS,OUTPUT);
pinMode(E,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
LCD_init();
display_to_LCD(0x48);
display_to_LCD(0x45);
display_to_LCD(0x4C);
display_to_LCD(0x4C);
display_to_LCD(0x4F);

set_location(0x40);

for(char x=0x30;x<=0x39;x++){
display_to_LCD(x);              // display numbers 0-9
}
}

void loop() {
}

void LCD_init(void){
delay(20);
digitalWrite(RS,LOW);
digitalWrite(E,LOW);

//function mode
digitalWrite(5,LOW);
digitalWrite(4,HIGH);
digitalWrite(3,LOW);
digitalWrite(2,LOW); // data = 0x2
toggle_enable();
digitalWrite(5,LOW);
digitalWrite(4,LOW);
digitalWrite(3,LOW);
digitalWrite(2,HIGH); // data = 0x8
toggle_enable();

//display mode
digitalWrite(5,LOW);
digitalWrite(4,LOW);
digitalWrite(3,LOW);
digitalWrite(2,LOW); // data = 0x0
toggle_enable();
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
digitalWrite(3,HIGH);
digitalWrite(2,HIGH); // data = 0xF
toggle_enable();

//clear display
digitalWrite(5,LOW); // data = 0x0;
digitalWrite(4,LOW);
digitalWrite(3,LOW);
digitalWrite(2,LOW);
toggle_enable();
digitalWrite(5,HIGH);
digitalWrite(4,LOW);
digitalWrite(3,LOW);
digitalWrite(2,LOW); // data = 0x1;
toggle_enable();
}

void toggle_enable(){
digitalWrite(E,HIGH);
delay(1);
digitalWrite(E,LOW);
delay(1);
}

void display_to_LCD(char value){
digitalWrite(RS,HIGH);
char lvalue=value>>4;
for (int i=0; i<4; i++) {
int state = bitRead(lvalue, i);
digitalWrite(pins[i], state);
}
toggle_enable();
for (int i=0; i<4; i++) {
int state = bitRead(value, i);
digitalWrite(pins[i], state);
}
toggle_enable();
}

void set_location(char location){
digitalWrite(RS,LOW);
char llocation=(location|0x80)>>4;
for (int i=0; i<4; i++) {
int state = bitRead(llocation, i);
digitalWrite(pins[i], state);
}
toggle_enable();
for (int i=0; i<4; i++) {
int state = bitRead(location, i);
digitalWrite(pins[i], state);
}
toggle_enable();
}

# data[].attributes.body
#include "mbed.h"
AnalogIn Ain(p19);
BusOut LED(LED1,LED2,LED3,LED4);
int main() {
while(1) {
if(Ain<=0.25){
LED=1;
}
else if (Ainu>0.25 && Ain<=0.5){
LED=2;
}
else if (Ain>0.5 && Ain<=0.75){
LED=4;
}
else if (Ain>0.75 && Ain<=1){
LED=8;
}
}
}

# data[].attributes.body
int num_array[10][8] =  {{1,1,1,1,1,1,0,0},            {0,1,1,0,0,0,0,0},        {1,1,0,1,1,0,1,0},        {1,1,1,1,0,0,1,0},          {0,1,1,0,0,1,1,0},        {1,0,1,1,0,1,1,0},                      {1,0,1,1,1,1,1,0},    {1,1,1,0,0,0,0,0},    {1,1,1,1,1,1,1,0}, {1,1,1,1,0,1,1,0}};

void setup(){
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}

void loop() {
for (int i = 0; i < 10; i++){
delay(1000);
SegConvert(i);
}
}

void SegConvert(int SegValue) {
int pin = 1;
for (int j=0; j < 7; j++) {    digitalWrite(pin, num_array[SegValue][j]);
pin++;
}
}

# data[].attributes.body
unsigned const int A = 13;

unsigned const int B = 12;

unsigned const int C = 11;

unsigned const int D = 10;

unsigned const int E = 9;

unsigned const int F = 8;

unsigned const int G = 7;

unsigned const int H = 6;

void setup(void)

{

pinMode(A, OUTPUT);

pinMode(B, OUTPUT);

pinMode(C, OUTPUT);

pinMode(D, OUTPUT);

pinMode(E, OUTPUT);

pinMode(F, OUTPUT);

pinMode(G, OUTPUT);

pinMode(H, OUTPUT);

}

//My Functions

void zero(void) {

digitalWrite(A, HIGH);

digitalWrite(B, HIGH);

digitalWrite(C, LOW);

digitalWrite(D, HIGH);

digitalWrite(E, HIGH);

digitalWrite(F, LOW);

digitalWrite(G, HIGH);

digitalWrite(H, LOW);

}

void one(void) {

digitalWrite(A, HIGH);

digitalWrite(B, HIGH);

digitalWrite(C, HIGH);

digitalWrite(D, LOW);

digitalWrite(E, HIGH);

digitalWrite(F, HIGH);

digitalWrite(G, LOW);

digitalWrite(H, LOW);

}

void two(void) {

digitalWrite(A, LOW);

digitalWrite(B, HIGH);

digitalWrite(C, LOW);

digitalWrite(D, LOW);

digitalWrite(E, HIGH);

digitalWrite(F, HIGH);

digitalWrite(G, LOW);

digitalWrite(H, LOW);

}

void three(void) {

digitalWrite(A, HIGH);

digitalWrite(B, HIGH);

digitalWrite(C, HIGH);

digitalWrite(D, HIGH);

digitalWrite(E, HIGH);

digitalWrite(F, LOW);

digitalWrite(G, LOW);

digitalWrite(H, LOW);

}

// Start

void loop(void)

{

zero();

delay(1000);

one();

delay(1000);

two();

delay(1000);

three();

delay(1000);

}

# data[].attributes.body
#include <Servo.h>

Servo myservo;
int value;
double angle;

void setup()
{
Serial.begin(9600);
myservo.attach(9);
}

void loop()
{
value = analogRead(A0);
angle = map(value, 0, 1023, 0, 180);
Serial.println(angle);
myservo.write(angle);
delay(15);
}

# data[].attributes.body
void setup() {

for(int i=0;i<=7;i++)

pinMode(i, OUTPUT);

}

bool data[4][8]= {

{0,1,1,0,1,1,1,0},

{1,0,0,1,1,1,1,0},

{0,0,0,1,1,1,0,0},

{1,1,0,0,1,1,1,0}

};

void loop() {

for(int j=0;j<4;j++){

for(int k=0;k<=7;k++)

digitalWrite(k,LOW);

delay(500);

for(int k=0;k<=7;k++)

digitalWrite(k,data[j][k]);

delay(500);

}

}

# data[].attributes.body
int left = 0;
int right = 0;
float pwm = 0.00;

void setup()
{
pinMode(8, INPUT);
pinMode(7, INPUT);
pinMode(3, OUTPUT);
Serial.begin(9600);
}

void loop()
{
// range is 0.0 - 1.0
left = digitalRead(7);
right = digitalRead(8);

if(left == HIGH && pwm >= 0.05){
pwm -= 0.05;
}
if(right == HIGH && pwm < 1.00){
pwm += 0.05;
}
float result = pwm * 255;
int currentValue = int(result);
Serial.println(currentValue);
analogWrite(3, currentValue);
delay(500);
}

# data[].attributes.body
#include "mbed.h"

DigitalOut led(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

int i = 0;
int main() {

while (i<5) {
led = 1;
led2 = 0;
led3 = 0;
led4 = 0;
wait_ms(500);
led = 0;
led2 = 1;
led3 = 0;
led4 = 0;
wait_ms(500);
led = 0;
led2 = 0;
led3 = 1;
led4 = 0;
wait_ms(500);
led = 0;
led2 = 0;
led3 = 0;
led4 = 1;
wait_ms(500);
i++;
}
led4 = 0;
}

# data[].attributes.body
void setup() {
pinMode(7, OUTPUT);
pinMode(A0, INPUT);
}

void loop(){
while(analogRead(A0)<1023/2)
{

}
tone(7, 220, 100);
delay(200);
}

# data[].attributes.body
int num_array[10][7] = {  { 1,1,1,1,1,1,0 },    // 0
{ 0,1,1,0,0,0,0 },    // 1
{ 1,1,0,1,1,0,1 },    // 2
{ 1,1,1,1,0,0,1 },    // 3
{ 0,1,1,0,0,1,1 },    // 4
{ 1,0,1,1,0,1,1 },    // 5
{ 1,0,1,1,1,1,1 },    // 6
{ 1,1,1,0,0,0,0 },    // 7
{ 1,1,1,1,1,1,1 },    // 8
{ 1,1,1,0,0,1,1 }};   // 9
void Num_Write(int);
char number;
void setup()
{

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);

pinMode(0, OUTPUT);
pinMode(1, OUTPUT);

}

void loop()
{
digitalWrite(0,HIGH);
digitalWrite(1,LOW);

digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);

delay(10);
digitalWrite(0,LOW);
digitalWrite(1,HIGH);

digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);

delay(10);
}

void Num_Write(int number)
{
int pin= 2;
for (int j=0; j < 7; j++) {
digitalWrite(pin, num_array[number][j]);
pin++;
}
}

//int num_array[10][7] = { { 1,1,1,1,1,1,0 }, // 0
// { 0,1,1,0,0,0,0 }, // 1
// { 1,1,0,1,1,0,1 }, // 2
// { 1,1,1,1,0,0,1 }, // 3
// { 0,1,1,0,0,1,1 }, // 4
// { 1,0,1,1,0,1,1 }, // 5
// { 1,0,1,1,1,1,1 }, // 6
// { 1,1,1,0,0,0,0 }, // 7
// { 1,1,1,1,1,1,1 }, // 8
// { 1,1,1,0,0,1,1 }}; // 9
char SegConvert(char SegValue) {        // function 'SegConvert'
char SegByte=0x00;
switch (SegValue) {                   //DP G F E D C B A
case 0 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
break;      // 0 0 1 1 1 1 1 1 binary
case 1 : digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
break;      // 0 0 0 0 0 1 1 0 binary
case 2 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
break;      // 0 1 0 1 1 0 1 1 binary
case 3 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
break;      // 0 1 0 0 1 1 1 1 binary
case 4 : digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;      // 0 1 1 0 0 1 1 0 binary
case 5 : digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;      // 0 1 1 0 1 1 0 1 binary
case 6 : digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;      // 0 1 1 1 1 1 0 1 binary
case 7 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
break;      // 0 0 0 0 0 1 1 1 binary
case 8 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;      // 0 1 1 1 1 1 1 1 binary
case 9 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;      // 0 1 1 0 1 1 1 1 binary
}
return SegByte;
}

# data[].attributes.body
#include<SPI.h>

int switch_ip1=8;
int switch_ip2=9;
int red_led=2;
int green_led=3;

byte switch_word = 0;
byte recd_val = 0;

void setup()
{
Serial.begin(115200);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
digitalWrite(SS,HIGH);
pinMode(switch_ip1,INPUT);
pinMode(switch_ip2,INPUT);
pinMode(red_led,OUTPUT);
pinMode(green_led,OUTPUT);
}

void loop()
{
switch_word=0;
if (digitalRead(switch_ip1)==HIGH)
switch_word=switch_word|0x01;        //OR in lsb
if (digitalRead(switch_ip2)==HIGH)
switch_word=switch_word|0x02;        //OR in next lsb

digitalWrite(SS,LOW);
recd_val=SPI.transfer(switch_word);

digitalWrite(red_led,0);
digitalWrite(green_led,0);
if (recd_val==1)
digitalWrite(red_led,1);
if (recd_val==2)
digitalWrite(green_led,1);
if (recd_val==3){
digitalWrite(red_led,1);
digitalWrite(green_led,1);
}
delay(1000);
}

# data[].attributes.body
#include <SPI.h>

int switch_ip1=8;
int switch_ip2=9;
int red_led=2;
int green_led=3;
int switch_word = 0;
int recd_val = 0;

volatile boolean received;
volatile byte Slavereceived,Slavesend;

void setup()
{
Serial.begin(115200);
pinMode(MISO,OUTPUT);

pinMode(switch_ip1,INPUT);
pinMode(switch_ip2,INPUT);
pinMode(red_led,OUTPUT);
pinMode(green_led,OUTPUT);

SPCR |= _BV(SPE);
received = false;
SPI.attachInterrupt();
}

ISR (SPI_STC_vect)
{
recd_val = SPDR;
received = true;
}

void loop()
{
if(received){
digitalWrite(red_led,0);
digitalWrite(green_led,0);
if (recd_val==1)
digitalWrite(red_led,1);
if (recd_val==2)
digitalWrite(green_led,1);
if (recd_val==3){
digitalWrite(red_led,1);
digitalWrite(green_led,1);
}

switch_word=0;
if (digitalRead(switch_ip1)==HIGH)
switch_word=switch_word|0x01;        //OR in lsb
if (digitalRead(switch_ip2)==HIGH)
switch_word=switch_word|0x02;        //OR in next lsb
SPDR = switch_word;
delay(1000);
}
}

# data[].attributes.body
#include <Wire.h>

int switch_ip1=8;
int switch_ip2=9;
int red_led=2;
int green_led=3;

void setup()
{
Wire.begin();
pinMode(switch_ip1,INPUT);
pinMode(switch_ip2,INPUT);
pinMode(red_led,OUTPUT);
pinMode(green_led,OUTPUT);
}

int switch_word = 0;
int recd_val = 0;

void loop()
{
switch_word=0;
if (digitalRead(switch_ip1)==HIGH)
switch_word=switch_word|0x01;        //OR in lsb
if (digitalRead(switch_ip2)==HIGH)
switch_word=switch_word|0x02;        //OR in next lsb

Wire.beginTransmission(4);
Wire.write(switch_word);
Wire.endTransmission();
delay(500);

Wire.requestFrom(4, 1);    // request 1 bytes from slave device #4

while (Wire.available()) { // slave may send less than requested
recd_val = Wire.read(); // receive a byte as character
}
digitalWrite(red_led,0);
digitalWrite(green_led,0);
if (recd_val==1)
digitalWrite(red_led,1);
if (recd_val==2)
digitalWrite(green_led,1);
if (recd_val==3){
digitalWrite(red_led,1);
digitalWrite(green_led,1);
}
delay(500);
}

# data[].attributes.body
int num_array[10][7] = {  { 1,1,1,1,1,1,0 },    // 0
{ 0,1,1,0,0,0,0 },    // 1
{ 1,1,0,1,1,0,1 },    // 2
{ 1,1,1,1,0,0,1 },    // 3
{ 0,1,1,0,0,1,1 },    // 4
{ 1,0,1,1,0,1,1 },    // 5
{ 1,0,1,1,1,1,1 },    // 6
{ 1,1,1,0,0,0,0 },    // 7
{ 1,1,1,1,1,1,1 },    // 8
{ 1,1,1,0,0,1,1 }};   // 9
void Num_Write(int);
char number;
bool wasd = true;

void setup()
{

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);

pinMode(0, OUTPUT);
pinMode(1, OUTPUT);

noInterrupts();           // disable all interrupts

TCCR1A = 0;
TCCR1B = 0;
TCNT1  = 0;

// compare match register 16MHz/256 * t(s) - 1
OCR1A = 155;

TCCR1B = 0x0D;   // CTC mode

TIMSK1 =0x22;  // enable timer compare interrupt

interrupts();             // enable all interrupts

}

ISR(TIMER1_COMPA_vect) {

if(wasd){
wasd = false;
digitalWrite(0,HIGH);
digitalWrite(1,LOW);

digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}
else{
wasd = true;
digitalWrite(0,LOW);
digitalWrite(1,HIGH);

digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}
}

void loop()
{

}

void Num_Write(int number)
{
int pin= 2;
for (int j=0; j < 7; j++) {
digitalWrite(pin, num_array[number][j]);
pin++;
}
}

//int num_array[10][7] = { { 1,1,1,1,1,1,0 }, // 0
// { 0,1,1,0,0,0,0 }, // 1
// { 1,1,0,1,1,0,1 }, // 2
// { 1,1,1,1,0,0,1 }, // 3
// { 0,1,1,0,0,1,1 }, // 4
// { 1,0,1,1,0,1,1 }, // 5
// { 1,0,1,1,1,1,1 }, // 6
// { 1,1,1,0,0,0,0 }, // 7
// { 1,1,1,1,1,1,1 }, // 8
// { 1,1,1,0,0,1,1 }}; // 9
char SegConvert(char SegValue) {        // function 'SegConvert'
char SegByte=0x00;
switch (SegValue) {                   //DP G F E D C B A
case 0 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
break;      // 0 0 1 1 1 1 1 1 binary
case 1 : digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
break;      // 0 0 0 0 0 1 1 0 binary
case 2 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
break;      // 0 1 0 1 1 0 1 1 binary
case 3 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
break;      // 0 1 0 0 1 1 1 1 binary
case 4 : digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;      // 0 1 1 0 0 1 1 0 binary
case 5 : digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;      // 0 1 1 0 1 1 0 1 binary
case 6 : digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;      // 0 1 1 1 1 1 0 1 binary
case 7 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
break;      // 0 0 0 0 0 1 1 1 binary
case 8 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;      // 0 1 1 1 1 1 1 1 binary
case 9 : digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;      // 0 1 1 0 1 1 1 1 binary
}
return SegByte;
}

# data[].attributes.attachment_link.description
See all the wonderful things Kok Soon Tey has made