Use an Attiny45 as slave and an Arduino MEGA 2560 as Master I2C-Bus

Hello,

if got some problems with my Arduino-System.

I want to send some information from my Arduino-Board to my Attiny45. I know it is possible to communicate in the other way. For example the Attiny send some temperature values to the master.

But that's not my problem. I want to send commands from my Arduino to the slaves and then they change the output ports LOW and HIGH.

Here is my master code:


#include <TinyWireM.h>
#include <USI_TWI_Master.h>

void setup() 
{
  // put your setup code here, to run once:
  TinyWireM.begin();
  TinyWireM.requestFrom(requestEvent);
}

void loop() 
{

}

void requestEvent(uint8_t slaveAddr)
{
  TinyWireM.beginTransmission(2); //start of transmission
  TinyWireM.send('F');                   //transfers a char to the Slave
  TinyWireM.endTransmission();     //end of transmission  
  delay(500);
  
  TinyWireM.beginTransmission(2);  //start of transmission
  TinyWireM.send('d');                    //transfers a char to the Slave
  TinyWireM.endTransmission();      //end of transmission 
  delay(500);
}

Here the slave code:


//i2c Slave
#include <TinyWireS.h>
#include <Wire.h>

void setup()
{
  TinyWireS.begin(2);                // join i2c bus with address #2
  TinyWireS.onReceive(receiveEvent); // register event
  
  pinMode(1 ,OUTPUT);
}

void loop()
{
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()

void receiveEvent(int howMany)
{
  while(TinyWireS.available())     
  {
    char c = TinyWireS.receive();     // receive byte as a character

    switch (c)
    {
      case 'd':
        digitalWrite(1 ,LOW);
        break;
      case 'F':
        digitalWrite(1 ,HIGH);
        break;
    }
   }
  }

The LED on port 1 should blink... But I just get error messages.

Here are the Keywords from the libraries

#######################################

Syntax Coloring Map For TinyWireS

#######################################

#######################################

Datatypes (KEYWORD1)

#######################################

#######################################

Methods and Functions (KEYWORD2)

#######################################

begin KEYWORD2
send KEYWORD2
available KEYWORD2
receive KEYWORD2

#######################################

Instances (KEYWORD2)

#######################################

TinyWireS KEYWORD2

#######################################

Constants (LITERAL1)

#######################################


#######################################

Syntax Coloring Map For TinyWireM

#######################################

#######################################

Datatypes (KEYWORD1)

#######################################

#######################################

Methods and Functions (KEYWORD2)

#######################################

begin KEYWORD2
beginTransmission KEYWORD2
endTransmission KEYWORD2
requestFrom KEYWORD2
send KEYWORD2
receive KEYWORD2

#######################################

Instances (KEYWORD2)

#######################################

TinyWireM KEYWORD2

#######################################

Constants (LITERAL1)

#######################################


Thank you very much in advance.

The LED on port 1 should blink... But I just get error messages.

So, I'm going to paste a copy of some useless file, instead of the error messages. Help, anyway.

Really?

Hi,

thanks for the fast answer. (excuse my englisch.. I'm from Germany :wink: ).

I made some changes in the Master Code..

So here is the new code.


#include <Wire.h>

//#define slaveAddr 2
void setup() {
  // put your setup code here, to run once:
  Wire.begin();
}

void loop() 
{
  // put your main code here, to run repeatedly:
  Wire.beginTransmission(2);
  Wire.write('F');
  Wire.endTransmission();
  delay(500);
  
  Wire.beginTransmission(2);
  Wire.write('d');
  Wire.endTransmission();
  delay(500);
}

This code works on the Arduino Mega 2560.


But the slave Code will not run... :cry:

And here are the error messages

Arduino: 1.6.5 (Windows 7), Platine: "ATtiny, ATtiny45, 1 MHz (internal)"

Build-Optionen wurden ver‰ndert, alles wird neu gebaut

Verwende die Bibliothek TinyWireS im Ordner: C:\Users\student\Documents\Arduino\libraries\TinyWireS (legacy)

Verwende die Bibliothek Wire im Ordner: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire

C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=attiny45 -DF_CPU=1000000L -DARDUINO=10605 -DARDUINO_attiny -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\attiny\avr\variants\tiny8 -IC:\Users\student\Documents\Arduino\libraries\TinyWireS -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire C:\Users\student\AppData\Local\Temp\build6108703560398728676.tmp\Attiny45_Slave_ausf_hren.cpp -o C:\Users\student\AppData\Local\Temp\build6108703560398728676.tmp\Attiny45_Slave_ausf_hren.cpp.o

Attiny45_Slave_ausf_hren.ino: In function 'void setup()':
Attiny45_Slave_ausf_hren.ino:17:35: error: no matching function for call to 'USI_TWI_S::receive(void (&)(int))'
Attiny45_Slave_ausf_hren.ino:17:35: note: candidate is:
In file included from Attiny45_Slave_ausf_hren.ino:6:0:
C:\Users\student\Documents\Arduino\libraries\TinyWireS/TinyWireS.h:52:13: note: uint8_t USI_TWI_S::receive()
uint8_t receive();
^
C:\Users\student\Documents\Arduino\libraries\TinyWireS/TinyWireS.h:52:13: note: candidate expects 0 arguments, 1 provided
no matching function for call to 'USI_TWI_S::receive(void (&)(int))'


Just from the slave code... Thanks a lot..

I don't see where your slave code is calling receive() with arguments. Hopefully, someone with an ATtiny45 can test your code.

char c = TinyWireS.receive();

thats my receive call. I hope everything is possible in my code.. Thanks a lot...

Christophhericks:
Hello,

if got some problems with my Arduino-System.

I want to send some information from my Arduino-Board to my Attiny45. I know it is possible to communicate in the other way. For example the Attiny send some temperature values to the master.

But that's not my problem. I want to send commands from my Arduino to the slaves and then they change the output ports LOW and HIGH.

Here the slave code:


void receiveEvent(int howMany)

{
 while(TinyWireS.available())    
 {
   char c = TinyWireS.receive();     // receive byte as a character

insert a type cast. The TinyWireS.receive() is declared as:

uint8_t TinyWireS.receive();

// to receive into a char, tell the compiler it is okay to put a uint8_t into a char.

char c = (char)TinyWireS.receive();

Chuck.

Thanks for the quick answer. I'll try it at home in the evening. Thanks a lot..

Hi chucktodd,

the hint with the char before the command was good, but now I get an other error-massage.


Arduino: 1.6.5 (Windows 7), Platine: "ATtiny, ATtiny45, 1 MHz (internal)"

Verwende die Bibliothek TinyWireS im Ordner: C:\Users\student\Documents\Arduino\libraries\TinyWireS (legacy)

C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=attiny45 -DF_CPU=1000000L -DARDUINO=10605 -DARDUINO_attiny -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\attiny\avr\variants\tiny8 -IC:\Users\student\Documents\Arduino\libraries\TinyWireS C:\Users\student\AppData\Local\Temp\build4825846298146883305.tmp\Attiny45_Slave_ausf_hren.cpp -o C:\Users\student\AppData\Local\Temp\build4825846298146883305.tmp\Attiny45_Slave_ausf_hren.cpp.o

Attiny45_Slave_ausf_hren.ino: In function 'void setup()':
Attiny45_Slave_ausf_hren.ino:18:21: error: expected initializer before '.' token
expected initializer before '.' token


The error is in the line "uint8_t TinyWireS.receive();"

How can I fix this problem?

Thanks...

The error is in the line "uint8_t TinyWireS.receive();"

How can I fix this problem?

Delete that line. Chuck showed that line, to show what the function signature looked like, not for you to include it in your code.