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