home-automation/hardware/arduino/i2c_dip/slave_sender/slave_sender.ino

70 lines
1.8 KiB
C++

// need 0.1uf cap between vcc and ground for proper operation
// need 10k pull up resistor on scl and sda for i2c to work correctly
// Pin Mappings (Per TinyCore)
// ATTiny Pin -- Arduino Pin
// 01 17/PA2/RESET
// 02 0/PD0/RXD
// 03 1/PD1/TXD
// 04 2/PA1/XTAL2
// 05 3/PA0/XTAL1
// 06 4/PD2/INT0
// 07 5/PD3/INT1
// 08 6/PD4
// 09 7/PD5
// 10 GND
// 11 8/PD6
// 12 9/PB0/PCINT0
// 13 10/PB1/PCINT1
// 14 11/PB2/PCINT2/OC0A
// 15 12/PB3/PCINT3/OC1A
// 16 13/PB4/PCINT4/OC1B
// 17 14/PB5/PCINT5/MOSI/DI/SDA
// 18 15/PB6/PCINT6/MISO/DO
// 19 16/PB7/PCINT7/SCK/SCL
// 20 VCC
// i2c ATTiny Pins
// sda: 17
// scl: 19
// DIP Switch ATTiny Pins
// 2 - 9
#define I2C_ADDRESS 0x96
#include <Wire.h>
void setup() {
pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
Wire.begin(I2C_ADDRESS);
Wire.onRequest(requestEvent);
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
byte dip_switch = 0b00000000;
void requestEvent() {
bitWrite(dip_switch, 0, digitalRead(0));
bitWrite(dip_switch, 1, digitalRead(1));
bitWrite(dip_switch, 2, digitalRead(2));
bitWrite(dip_switch, 3, digitalRead(3));
bitWrite(dip_switch, 4, digitalRead(4));
bitWrite(dip_switch, 5, digitalRead(5));
bitWrite(dip_switch, 6, digitalRead(6));
bitWrite(dip_switch, 7, digitalRead(7));
Wire.write(dip_switch);
}