Created
January 5, 2019 11:34
-
-
Save sergiomtzlosa/b0c639606dfe3e17d6ca6ac323177b02 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
/** | |
* ATtiny85 as an I2C Slave | |
* ATtiny I2C slave receiving and sending data to a RaspberryPi master. | |
* | |
* ATTiny Pin 1 = (RESET) N/U ATTiny Pin 2 = (D3) BUZZER) | |
* ATTiny Pin 3 = (D4) to LED1 ATTiny Pin 4 = GND | |
* ATTiny Pin 5 = I2C SDA ATTiny Pin 6 = (D1) to LED2 | |
* ATTiny Pin 7 = I2C SCL ATTiny Pin 8 = VCC (2.7-5.5V) | |
* | |
* Use pullups on the SDA & SCL lines! (4k7) | |
* | |
* Install ATTiny85 cards --> https://forum.arduino.cc/index.php?topic=437386.0 | |
* | |
* File -> Preferences, enter the above URL in "Additional Board Manager URLs: | |
* | |
* http://drazzy.com/package_drazzy.com_index.json | |
* | |
* Install Card: "ATTiny Core by Spence Konde" | |
* | |
* TinyWireS library: http://playground.arduino.cc/uploads/Code/TinyWireS.zip | |
* | |
* More Info: https://www.raspberrypi.org/forums/viewtopic.php?t=102243 | |
**/ | |
#include "TinyWireS.h" | |
#define I2C_SLAVE_ADDR 0x2d | |
#define LED1_PIN 4 | |
#define LED2_PIN 1 | |
#define BUZZER_PIN 3 | |
// 0x00 --> Nothing happens | |
// 0x01 --> Buzzer ON, LEDs OFF | |
// 0x10 --> Buzzer OFF, LEDs ON | |
// 0x11 --> Buzzer ON, LEDs ON | |
#define BUZZER_ON_LEDS_OFF 0x01 | |
#define BUZZER_OFF_LEDS_ON 0x10 | |
#define BUZZER_ON_LEDS_ON 0x11 | |
bool tinyLatch = false; | |
void setup() { | |
pinMode(BUZZER_PIN, OUTPUT); | |
pinMode(LED1_PIN, OUTPUT); | |
pinMode(LED2_PIN, OUTPUT); | |
blinkLed(LED1_PIN, 2); | |
TinyWireS.begin(I2C_SLAVE_ADDR); | |
} | |
void loop() { | |
if (tinyLatch == false) { | |
byte bytesReceived = 0; | |
if (TinyWireS.available()) { | |
tinyLatch = true; | |
bytesReceived = TinyWireS.receive(); | |
if (bytesReceived) { | |
performOption(bytesReceived); | |
} | |
bytesReceived = 0; | |
tinyLatch = false; | |
} | |
} | |
} | |
bool isOptionValid(byte checkOption) { | |
return (checkOption == BUZZER_OFF_LEDS_ON || checkOption == BUZZER_ON_LEDS_ON || checkOption == BUZZER_ON_LEDS_OFF); | |
} | |
void performOption(byte option) { | |
if (!isOptionValid(option)) { | |
TinyWireS.send(42); | |
} else { | |
// return the option sent plus 10 | |
int inputValue = (int)option; | |
int response = inputValue + 10; | |
TinyWireS.send(response); | |
int high = (int)(inputValue >> 4); | |
int low = (int)(inputValue & 0xF); | |
if (low == 1) { | |
tone(BUZZER_PIN, 300, 250); | |
} | |
if (high == 1) { | |
blinkLed(LED1_PIN, 5); | |
} | |
if (low == 1) { | |
tone(BUZZER_PIN, 500, 250); | |
} | |
if (high == 1) { | |
blinkLed(LED2_PIN, 1); | |
} | |
} | |
} | |
void blinkLed(byte led, byte times) { | |
for (byte i = 0; i < times; i++) { | |
digitalWrite(led, HIGH); | |
delay(250); | |
digitalWrite(led, LOW); | |
delay(175); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment