i2c Help! master with light sensors getting slave to turn LED on

Hi all!

Working on a project with 2 arduino Uno's connected through i2c, but I'm running into some errors when adapting my codes...

Expected function of the i2c code:
-Master will take light readings and send values to Slave.
-Slave will interpret the light sensor values and if under 100, will turn an LED on. When the value is over 100, the light turns off.

In case you're wondering why i don't just use 1 Uno for both functions, i also have a DMD connected to the Master, and a music shield connected to the slave. In the end, I want the master to read the light sensor, output a value to the DMD while also instructing the slave to turn an LED on, and play a sound.

I already have the sensors and DMD working together on the Master, as well as the LED and music working on the Slave, but i need the i2c codes to merge them all together.

I referenced the instructions on this instruction video:

so the wiring and everything matches whats in the video.

I pulled the code from the video and edited it to what i thought would work.
Clearly it isn't working...

======================
//i2c Master Code - Kirk
#include <Wire.h>

int light1 = A0;

void setup()
{
Serial.begin (9600);

Wire.begin();

}

void loop()
{
int L1 = analogRead(light1);
delay (500);
Serial.println (L1);

while (Serial.available())
{

if (L1 > 100)
{
Wire.beginTransmission(5);
Wire.write ('H');
Wire.endTransmission();
}
else if (L1 < 100)
{
Wire.beginTransmission(5);
Wire.write ('L');
Wire.endTransmission();
}
}
}

==================

//i2C Slave Code -Kirk
#include <Wire.h>

void setup()
{
Wire.begin(5);
Wire.onReceive (receiveEvent);

pinMode (13,OUTPUT);
digitalWrite (13,LOW);
}

void loop()
{

}

void receiveEvent (int howMany) //can be called anything receiveevent
{
while(Wire.available())
{
char c = Wire.read();

if (c == 'H')
{
digitalWrite (13, HIGH);
}
else if(c == 'L')
{
digitalWrite (13, LOW);
}
}
}

My logic is that the light sensor will take a reading, if the reading is >100, it will out put "H" to the slave, which will tell the slave to turn the LED on. If it is <100, the master will output an "L" to the slave turning the LED off.

Im sorry if this is totally obvious, I'm just not seeing it right now.

Any help would be much appreciated!

Thanks,
Kirk

kirksarduino:
Hi all!

Working on a project with 2 arduino Uno's connected through i2c, but I'm running into some errors when adapting my codes...

Expected function of the i2c code:
-Master will take light readings and send values to Slave.
-Slave will interpret the light sensor values and if under 100, will turn an LED on. When the value is over 100, the light turns off.

In case you're wondering why i don't just use 1 Uno for both functions, i also have a DMD connected to the Master, and a music shield connected to the slave. In the end, I want the master to read the light sensor, output a value to the DMD while also instructing the slave to turn an LED on, and play a sound.

I already have the sensors and DMD working together on the Master, as well as the LED and music working on the Slave, but i need the i2c codes to merge them all together.

I referenced the instructions on this instruction video:
https://www.youtube.com/watch?v=Jndb2vpAWwU

so the wiring and everything matches whats in the video.

I pulled the code from the video and edited it to what i thought would work.
Clearly it isn't working...

======================

//i2c Master Code - Kirk
#include <Wire.h>

int light1 = A0;

void setup()
{
 Serial.begin (9600);

Wire.begin();
 
}

void loop()
{
 int L1 = analogRead(light1);
 delay (500);
 Serial.println (L1);
 
 
 while (Serial.available())
 {

if (L1 > 100)
   {
     Wire.beginTransmission(5);
     Wire.write ('H');
     Wire.endTransmission();
   }
   else if (L1 < 100)
   {
     Wire.beginTransmission(5);
     Wire.write ('L');
     Wire.endTransmission();
     }
   }
}
   
   
==================

//i2C Slave Code -Kirk
#include <Wire.h>

void setup()
{
 Wire.begin(5);
 Wire.onReceive (receiveEvent);

pinMode (13,OUTPUT);
 digitalWrite (13,LOW);
}

void loop()
{
 
}

void receiveEvent (int howMany) //can be called anything receiveevent
{
 while(Wire.available())
 {
   char c = Wire.read();

if (c == 'H')
   {
     digitalWrite (13, HIGH);
   }
   else if(c == 'L')
   {
     digitalWrite (13, LOW);
   }
 }
}



======================

My logic is that the light sensor will take a reading, if the reading is >100, it will out put "H" to the slave, which will tell the slave to turn the LED on. If it is <100, the master will output an "L" to the slave turning the LED off.

Im sorry if this is totally obvious, I'm just not seeing it right now.

Any help would be much appreciated!

Thanks,
Kirk

Kirk,
How are the Arduino's wired? GND - GND, SDA - SDA, SCL-SCL, 4.7k pullup from SDA to VCC, 4.7k Pullup for SCL to VCC?

Why do you have:

while(Serial.available())

in your Master loop() code? You master will NOT send anything to the Slave unless you send data from the Serial monitor to the Master Arduino?

Also once a single character is send from the Serial Monitor, the Master will continuously send to the Slave the first reading (A0) over and over, never changing?

Maybe you wanted something like this

// Master code
#include <Wire.h>
void setup(){
Serial.begin(9600); // debug output
Wire.begin();         // init I2C hardware,software

pinMode(13,output); // going to use LED for heartbeat indicator
digitalWrite(13,LOW);
}

// loop()
void loop(){
static unsigned long timeout =millis(); // slow down the data stream

if(millis()-timeout>1000){ // only function once per second
  timeout=millis();  // reset timeout counter
  digitalWrite(13,HIGH); // turn on heartbeat indicator
  int L1 = analogRead(light1);
  Serial.println (L1);
  
  if (L1 > 100) {
      Wire.beginTransmission(5);
      Wire.write ('H');
      Wire.endTransmission();
      }
  else if (L1 < 100) {
      Wire.beginTransmission(5);
      Wire.write ('L');
      Wire.endTransmission();
      }
  } // end of active cycle
else if(millis()-timeout>250){ // turn off heartbeat indicator after 1/4 second
  digitalWrite(13,LOW);
  }  
}

Chuck.

p.s Please use the [code] [/code] markups around your code. It makes it easier to read and copy.