Created
March 16, 2018 21:13
-
-
Save microcontrollershub/6d934ceba479634863980a1e13afb00d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arduino as IO - Extender (Slave_Code) | |
// by Varad Kulkarni <http://www.microcontrollershub.com> | |
// Created on 16 March 2018 | |
#include <Wire.h> | |
void setup() | |
{ | |
Wire.begin(0x40); // Initialize Arduino as slave with addres 0x40 | |
Wire.onReceive(receiveEvent); // Register event | |
Serial.begin(9600); // Start serial for debug | |
pinMode(8,OUTPUT); // Use LED_1 connected tompin 8 as indicator | |
digitalWrite(8,LOW); // Initialize LED_1 in OFF state | |
pinMode(9,OUTPUT); // Use LED_2 connected tompin 9 as indicator | |
digitalWrite(9,LOW); // Initialize LED_2 in OFF state | |
} | |
void loop() | |
{ | |
} | |
// function that executes whenever data is received from master | |
void receiveEvent(int howMany) | |
{ | |
while (0 < Wire.available()) // loop through all but the last | |
{ | |
Serial.println("data received"); | |
char cReceivedData = Wire.read(); // receive byte as an char | |
Serial.println(cReceivedData); // print the character | |
if('1' == cReceivedData) // If '1' is received, turn LED_1 OFF | |
{ | |
digitalWrite(8,HIGH); | |
} | |
else if('2' == cReceivedData) // If '2' is received, turn LED_1 ON | |
{ | |
digitalWrite(8,LOW); | |
} | |
else if('3' == cReceivedData) // If '3' is received, turn LED_2 ON | |
{ | |
digitalWrite(9,HIGH); | |
} | |
else if('4' == cReceivedData) // If '4' is received, turn LED_2 ON | |
{ | |
digitalWrite(9,LOW); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment