I first read how Johanna manage with her boards, but she was using Attiny84.
So I searched a bit and find Mark Ng who's made a very simple and straightforward doc.
First thing I check if the board worked using a couple of code:
For the Master, to check serial communication:
#include // Declare SoftwareSerial library for serial com with Attiny.
#define rxPin 4 //I don't use it actualy so it's wired to an unused pin.
#define txPin 1 //It's MISO pin.
SoftwareSerial serial(rxPin, txPin);
void setup() {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
serial.begin(9600);
}
void loop(){
serial.println("Ok, it's working !");
delay(1000);
}
It worked.
For the Slave, to check if the LED's worked (basic Blink script):
int LED = 4;
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
} It worked.
I2C communication code
First thing to do, download and install the librairies. There's 2 of it, TinyWireM for the Master and TinyWireS for the Slave.
I first tested with Mark's code:
Master:
#include //Declare TinyWireM fontlibrary
#define device (1) //Define device number
void setup() {
TinyWireM.begin(); //Starting the library
}
void loop() {
TinyWireM.beginTransmission(device); //Starting transmission
TinyWireM.send(1); //Send number 1
TinyWireM.endTransmission(); //Ending transmission
delay(2000);
TinyWireM.beginTransmission(device);
TinyWireM.send(0);
TinyWireM.endTransmission();
delay(2000);
}
Slave:
#include //Declare TinyWireS library
#define output (4) //Define the LED pin
#define I2C_SLAVE_ADDR (1) //Define the slave's adresse
void setup() {
// put your setup code here, to run once:
TinyWireS.begin(I2C_SLAVE_ADDR); //Starting to listen from the Master.
pinMode(output, OUTPUT); //Define LED's pin as an output.
}
volatile byte msg = 0; //Declare variable for the message from the Master.
void loop() {
/* If the slave is available, the msg variable will be the content of what the Master is sending.
If msg is equal to 1, the LED will lights. If it's 0, it will be off.*/
if (TinyWireS.available())
msg = TinyWireS.receive();
if (msg == 1)
digitalWrite(output, HIGH);
else if (msg == 0)
digitalWrite(output, LOW);
else
msg = 0;
}
It worked !
But I wanted to have a serial communication. So I mix the code above with Johanna's one:
MASTER:
#include
#define device (1)
#define SLAVE_ADDR 0x6 //define the address for the slave
#include //add library to be able to use the serial monitor
int rxPin = 4; //the receiving pin
int txPin = 1; //the transmitting pin
SoftwareSerial serial(rxPin, txPin); // to set up the serial object
void setup() {
TinyWireM.begin();
pinMode(rxPin, INPUT); //the rx pin is the input of the communication
pinMode(txPin, OUTPUT); //the tx pin is the output of the communication
serial.begin(9600); //begin communication with computer
}
void loop() {
TinyWireM.beginTransmission(SLAVE_ADDR);
TinyWireM.send(1);
TinyWireM.endTransmission();
msg();//receive message
delay(2000);
TinyWireM.beginTransmission(SLAVE_ADDR);
TinyWireM.send(0);
TinyWireM.endTransmission();
msg();//receive message
delay(2000);
}
void msg()
{
volatile byte msg =0; //treat as variable
TinyWireM.requestFrom(SLAVE_ADDR,1);//request from slave address
if (TinyWireM.available()){ //to make sure the master is available
msg = TinyWireM.receive();//receive message
if (msg ==4){//if this message
serial.println("It's day!");}//print this in serial monitor
else if (msg ==5){//if this message
serial.println("It's night!");}//print this in serial monitor
}
}
SLAVE
#include
#define output (4)
#define I2C_SLAVE_ADDR 0x6
void setup() {
// put your setup code here, to run once:
TinyWireS.begin(I2C_SLAVE_ADDR);
pinMode(output, OUTPUT);
}
volatile byte msg = 0;
void loop() {
if (TinyWireS.available()){
msg = TinyWireS.receive();
if (msg == 1){
digitalWrite(output, HIGH);
TinyWireS.send(5); //send message
}
else if (msg == 0){
digitalWrite(output, LOW);
TinyWireS.send(4);//send message
}
}
}
It didn't work in the first time I tried because of this:
#include //add library to be able to use the serial monitor
int rxPin = 4; //the receiving pin
int txPin = 1; //the transmitting pin
The receving pin (which I didn't use anyway) was set on 0. But 0 was also pin 5 on the Attiny which was SDA pin. I change it for pin 4 because it was unused.
AND AFTER SEVERAL WEEKS, I MANAGED TO FINISHED THIS DOOMED WEEK, THE NETWORKING & COMMUNICATIONS WEEK:
But I'm quite sad for my radio board, I was very proud of it.