For communicating between projects, I’ve paired with Josep Marti and Diar Amin. We decided to use the ribbon cable already assembled to create an i2C hub and talk with our hello boards.
For I2C to work we need two PullUp resistors, like this:
Since we’ll be using Attiny44s and it doesn’t support the Arduino wire library. The first step is to add the AttinyCore from this link to arduino, then install it on the boards manager.
By adapting Josep’s code, we managed to get all boards talking and changing the master’s color when pressing the buttons.
This is the master’s code:
#include
int State[] = {0, 0, 0};
int but=8;
int R=7;
int G=2;
int B=3;
int Gustavo = 1;
int Diar = 2;
void setup() {
Wire.begin();
delay(5000);
}
void loop() {
if (digitalRead(but)== HIGH) {
analogWrite(R,255);
analogWrite(G,0);
analogWrite(B,255);
State[0] = 1;
}
else {
analogWrite(R,255);
analogWrite(G,255);
analogWrite(B,0);
State[0] = 0;
}
readU();
writeU();
delay(10);
}
void readU() {
Wire.requestFrom(Gustavo, 1);
State[1] = Wire.read();
Wire.requestFrom(Diar, 1);
State[2] = Wire.read();
}
void writeU() {
Wire.beginTransmission(Gustavo);
Wire.write(State[2]);
Wire.endTransmission();
Wire.beginTransmission(Diar);
Wire.write(State[0]);
Wire.endTransmission();
}
This is the slaves:
#include
int LED = 1; //Change it for your LED pin
int But = 2; //Change it for your Button pin
int Gustavo = 1;
int Diar = 2;
int LEDState = 0;
int ButState = 0;
void setup() {
Wire.begin(Diar); //Change the name
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
}
void loop() {
if (digitalRead(But)==HIGH) {
ButState = 1;
}
else{
ButState = 0;
}
if (LEDState == 1) {
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}
}
void receiveEvent() {
LEDState=Wire.read();
}
void requestEvent() {
Wire.write(ButState);
}
video demonstration of our group assignment
Individual assignment:
After I started to explore how I can make it happend with my board. First Test - so one Board (I borrowed Josephs) as a master and was adjusting everything to make work with my board for final project with ATtiny84 as a slave.
copy this link to my arduino preferences.
video demonstration
Than, next exploration with I2C communication - Instead of Josephs board, I plugged my own HelloBoard from electronics design week with ATtiny44 using it as a slave with a button for a color change communication when the button is pressed and uploaded this code to it:
Arduino code for Slave: This code is really simple, it just reads the state of the button connected to pin 3 of the ATtiny. When the lecture is HIGH, it saves the global variable ButState as a 1 and lights up the LED connected to pin 7. Otherwise, if the button pin is LOW, it sets the ButSTate variable to 0 and turn off the LED. Then, when the master boards makes a request to the board using the address 1, the program runs the void requetEvent() where it sends the ButState variable throught the Wire.h library to the master, sending the state of the button:
For Master I used my final board with Neopixels LED Strip with ATtiny84.
Arduino code for Master: The master board is programmed to light up the NeoPixels strip deppending on the valu of the state variable. This state variable is defined in the ReadSlave void that is executed during every loop of the program. This loop requests one byte to the slave with an addredd equals to 1, and reads what the slave is sending. In our case, it reads the state of the button, so, when the button is pressed it shines in one colour, when it's unpressed, it shines to a different one:
#include
#ifdef __AVR__
#include
#endif
#define PIN 8
Adafruit_NeoPixel strip = Adafruit_NeoPixel(13, PIN, NEO_GRB + NEO_KHZ800);
#include
int state = 0;
void setup() {
Wire.begin();
delay(5000);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
if (state == 0){
for(uint16_t i=0; i
Video demonstration of light change when the button is pressed.
I have also experimented with board including Wifi extention to check the networking and communication possibilities:
As a test I tried sending messages via MQTT using board NodeMCU V3 which has already embedded wifi component.