I2C between Trinkets and Mega not working

here is my dilemma...FIRST - what i am trying to do:

I have 2 trinkets and a mega 1280 (i know the schematic says 2560...but it doesn't have my model) talking via I2C...i have the master trinket 5V take input from a button...when pressed it is supposed light the LED connected to it and send a "1" to the 2 slaves...when you let go it sends a "0"...the slave trinket 3.3V waits for message...when receiving a 1 it turns on the led...a 0 turns is off...the mega does the opposite...0 turns it on the led and 1 turns it off.

what i have got working:

Trinket to Trinket I2C is working fine...where my issue comes into play is when i add in the mega...so i tried just trinket to mega...still a no go...so is there something i am doing wrong on the mega side??? the trinkets and the 4 bit level shifter are brand new from adafruit...the mega i have had for a while and have used to talk to other megas...i have attached code and schematic.

Trinket Master code

#include <TinyWireM.h>
#define SWITCH 3
#define LED 4
void setup()
{
  TinyWireM.begin();
  pinMode(LED, OUTPUT);
  pinMode(SWITCH, INPUT);
  digitalWrite(SWITCH, HIGH);
}

void loop()
{
  if (! digitalRead(SWITCH)) {
    digitalWrite(LED, HIGH);
    TinyWireM.beginTransmission(4);
    TinyWireM.send(1);
    TinyWireM.endTransmission();
    TinyWireM.beginTransmission(5);
    TinyWireM.send(1);
    TinyWireM.endTransmission();
  } 
  else {
    digitalWrite(LED, LOW);
    TinyWireM.beginTransmission(4);
    TinyWireM.send(0);
    TinyWireM.endTransmission();
    TinyWireM.beginTransmission(5);
    TinyWireM.send(0);
    TinyWireM.endTransmission();
  }
}

Trinket Slave code

#include <TinyWireS.h>

void setup()
{
  TinyWireS.begin(4);
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);
}

void loop()
{
  while(0 < TinyWireS.available())
  {
    if (TinyWireS.receive() == 1) {    
      digitalWrite(3, HIGH);
    }
    else {    
      digitalWrite(3, LOW);
    }
  }
}

Mega Slave code

#include <Wire.h>

void setup() {
  Wire.begin(5);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}

void loop() {
}

void receiveEvent(int howMany)
{
  while(0 < Wire.available())
  {
    byte c = Wire.read();
    Serial.println(c);
    if (0 == c) {    
      digitalWrite(13, HIGH);
    }
    else {    
      digitalWrite(13, LOW); 
    }  
  }
}

I didn't know WIRE and TinyWire protocols were compatible. I thought the ATtiny85 could not read WIRE and the Mega could not
read TinyWire. If they can I would like to know because then I can do the same thing with my UNO and my ATtiny85's .

from what i read...they are both I2C...and the TinyWire can read and write other I2C components...so i don't see why it wouldn't...but when i add the mega in the mix... i locks the trinkets...so i have no clue what is going on...i seen a vid of someone who is doing it here

vid not much help

i agree...doesn't show much...but the links say that it works

Did you do the schematic ? If so , what software ?

are you sure this is right ?

 while(0 < Wire.available())
  {
    byte c = Wire.read();
    Serial.println(c);
    if (0 == c) {             
      digitalWrite(13, HIGH);
    }
    else {    
      digitalWrite(13, LOW); 
    }  
  }

ARE YOU SURE IT SHOULDN'T BE if(c==0) ?

i did the schematic in Fritzing...it is easy to do there...and it doesn't matter which side the constant is on

int c = 3;
if (c == 3) {Serial.println("true");}else{Serial.println("false");}
if (3 == c) {Serial.println("true");}else{Serial.println("false");}

they will both return true...my issue happen only after connecting the mega...BUT...i got it to work...i took the code in the receiveEvent and put it in the loop...YAYAYAYAYAY

Can you post the corrected code ? (for the Mega)

SURE...i am also going to make a more detailed video on how to do this

#include <Wire.h>

void setup() {
  Wire.begin(5);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}

void loop() {
    while(0 < Wire.available())
  {
    byte c = Wire.read();
    Serial.println(c);
    if (0 == c) {    
      digitalWrite(13, HIGH);
    }
    else {    
      digitalWrite(13, LOW); 
    }  
  }
}
  #include <Wire.h>

void setup() {
  Wire.begin(5);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}

void loop() {
}

void receiveEvent(int howMany)
{
  while(0 < Wire.available())
  {
    byte c = Wire.read();
    Serial.println(c);
    if (0 == c) {    
      digitalWrite(13, HIGH);
    }
    else {    
      digitalWrite(13, LOW); 
    }  
  }
}

/////////////////////////////-- CORRECTED ---////////////////////////////////////////

  #include <Wire.h>

void setup() {
  Wire.begin(5);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}

void loop() {
    while(0 < Wire.available())
  {
    byte c = Wire.read();
    Serial.println(c);
    if (0 == c) {    
      digitalWrite(13, HIGH);
    }
    else {    
      digitalWrite(13, LOW); 
    }  
  }
}

I'm having a hard time finding the difference between your original Mega code and the corrected one you just posted.
Can you label the part you added so it is easier to see ?

Where's the link for Part-2 (both of the links are the same)

i am going to make part 2 tomorrow

I'm such an idiot. I didn't realize that was you doing the video. I thought you were posting the link for me to look at to answer my question until I read your last post. Nice vid ! That came out good.

thanks...i wanted to make something more than the others...but i didn't want to take 15 mins to explain how to hook everything up...part 1 is mostly for people who know I2C...but need to know how to link 5v and 3.3v devices together...part 2 will re-explain the I2C for arduino's and teach you it for the trinkets

Check out this video I did of a Police Car LED simulator that I made from a Scargill Youtube I saw. It's an ATtiny85 and four leds
two red and two blue.

Look at your schematic.
Level shifter LV input 3 not connected , HV 3 connected
LV input 4 connected HV 4 not connected

How does that work ?

Also, you don't show any current limiting resistors for the leds . Why ? (should be 220 ohms)
Why are there no 4.7k pullup resistors on SDA & SCL ? That's a standard requirement. I2C backpacks and lcds have them built in but when you DIY you have to include them to keep the bus stable and biased correctly.

I forgot to ask, does it matter what clock frequency the ATtiny85 is set for in the boards.txt file ? (will 1 Mhz work ?)

I programmed the chips and wired up the circuit and it's not working . I can't even get the leds to toggle.
Can you repost the working code for all three ?
The attached files are the code I am running (UNO instead of Mega)

MEGA_SLAVE.ino (409 Bytes)

TRINKET_MASTER.ino (665 Bytes)

TRINKET_SLAVE.ino (295 Bytes)

ARE YOU SURE IT SHOULDN'T BE if(c==0) ?

if ( 0 == c )

is perfectly valid, and is recommended practise by some experts. Possibly, the same experts who recommend ++i instead of i++, for no good reason in 99.9% of usages.

The ostensible reason for writing this way, is that if you write ( by mistake )

if ( 0 = c )

you will get a compilation error.

But if you write ( also by mistake )

if ( c = 0 )

then you won't get a compilation error, you will get a bug which sometimes may be very difficult to track down.