Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save microcontrollershub/906090a94584e4f01b715a7e991ae493 to your computer and use it in GitHub Desktop.
Save microcontrollershub/906090a94584e4f01b715a7e991ae493 to your computer and use it in GitHub Desktop.
// Arduino as IO - Expander (Master_Code)
// by Varad Kulkarni <http://www.microcontrollershub.com>
// Created 16 March 2018
#include <Wire.h>
//------------ Serial commands ------------
String LED_1_ON_Command = "LED 1 ON"; // I2C Command = 1
String LED_1_OFF_Command = "LED 1 OFF"; // I2C Command = 2
String LED_2_ON_Command = "LED 2 ON"; // I2C Command = 3
String LED_2_OFF_Command = "LED 2 OFF"; // I2C Command = 4
void setup()
{
Serial.begin(9600); // Initialize Serial communication at 9600 baud
Wire.begin(); // Initialize Arduino as Master
}
void loop()
{
if(Serial.available())
{
String ReceivedString = Serial.readString();
Serial.println("****** Serial Data recevied ******");
Serial.println(ReceivedString);
delay(200);
if(LED_1_ON_Command == ReceivedString)
{
Wire.beginTransmission(0x40); // Transmit to Slave device #0x40
Wire.write('1'); // Sends one byte as '1'
Wire.endTransmission(); // End transmition
delay(100);
}
else if(LED_1_OFF_Command == ReceivedString)
{
Wire.beginTransmission(0x40); // Transmit to Slave device #0x40
Wire.write('2'); // Sends one byte as '2'
Wire.endTransmission(); // End transmition
delay(100);
}
else if(LED_2_ON_Command == ReceivedString)
{
Wire.beginTransmission(0x40); // Transmit to Slave device #0x40
Wire.write('3'); // Sends one byte as '3'
Wire.endTransmission(); // End transmition
delay(100);
}
else if(LED_2_OFF_Command == ReceivedString)
{
Wire.beginTransmission(0x40); // Transmit to Slave device #0x40
Wire.write('4'); // Sends one byte as '4'
Wire.endTransmission(); // End transmition
delay(100);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment