Networking and communication: send a message between two projects

Networking and communications week

FABLAB BRIGHTON 2018

This week we wanted to combine two of the different types of projects we ran as individuals.

Firstly there was Des’s project which connected several Leonardo style boards (ATmega32U4) in an I2C configuration, where lights could be turned off and by typing numbers into the serial monitor. The sequence was as follows:


Then there was Paul’s project where an LDR (light dependent resistor) was used to determine the colour of an RGB LED.

For this project we wanted to use the coding configuration of Des’s project with both types of slaves (using ATmega32U4 and ATtiny85), so that for Slave 1 (ATmega32U4) the light simply came on or of (1 or 2), but for Slave 2 (ATtiny85), the light slowly changes where it get bluer (and less red and less green) when 3 is typed, while when 4 is typed it gets more red (less green and less blue).

We connected each board in turn and uploaded the respective sketch, making sure I got the correct code with the correct board. All Grounds were connected to each other, all VCCs were connected to each other, and similarly all SCR pins were connected to each other, and all SDA pins were connected to each other. Note that for I2C communications, the SCR and SDA pins are used, along with GND and VCC.

Here’s a video of it working: https://youtu.be/nHf5xXDH7Dc

We encountered a problem where we couldn’t get the ATtiny85 to recognise the character (CHAR) as the input from the master, so we reverted that back to an integer (INT) and then it worked.

Here are links to all the .ino files for each of the boards:

Here’s the code for the master:

//i2c code to connect 2 other boards to turn on and off lights in turn
// by typing 1, 2, 3, 4,

#include <Wire.h>

void setup() {

Serial.begin(9600);

Wire.begin();

}

void loop() {

  while(Serial.available())

{char c = Serial.read();

  if(c == '1')

  { Wire.beginTransmission(100);

    Wire.write('H');

    Wire.endTransmission();

    digitalWrite(13, HIGH);    

        }

  else if(c == '2')

  { Wire.beginTransmission(100);

    Wire.write('L');

    Wire.endTransmission();

     digitalWrite(13, LOW);

    }

     if(c == '3')

  { Wire.beginTransmission(4);

    Wire.write(1);

    Wire.endTransmission();

     digitalWrite(13, HIGH);         }

  else if(c == '4')

  { Wire.beginTransmission(4);

    Wire.write(2);

    Wire.endTransmission();

     digitalWrite(13, LOW);

    }}}


And here’s the code for the Slave 1 (address 5)

//i2c Slave1, address 5
#include

void setup() {
Wire.begin(5);  //specifying that this board is address 5
Wire.onReceive(receiveEvent);
pinMode(13, OUTPUT);
digitalWrite(13,LOW);
}

void loop() {
}
void receiveEvent (int howMany)
{
 while(Wire.available())
 {
   char c = Wire.read();
 if(c == 'H')
 {digitalWrite(13, HIGH);
   }
   
   else if(c == 'L')
   {
     digitalWrite(13, LOW);
   }
 }
}

And here’s the code for the Slave 2 (address 4):

#define I2C_SLAVE_ADDRESS 0x4 // Address of the slave

#include <TinyWireS.h >

int green = 4;

int blue = 1;

int red = 3;

int redi = 255;

int bluei = 255;

int greeni = 255;

byte val;

#define I2C_SLAVE_ADDR 0x4;

void setup()

{

  pinMode(red, OUTPUT);

  pinMode(blue, OUTPUT);

  pinMode(green, OUTPUT);

  TinyWireS.begin(I2C_SLAVE_ADDRESS); // join i2c network

  redi = 0;

  analogWrite(red, redi);

  delay(500);

  redi = 255;

  analogWrite(red, redi);

  bluei = 0;

  analogWrite(blue, bluei);

  delay(500);

  bluei=255;

  analogWrite(blue, bluei);

  greeni = 0;

  analogWrite(green, greeni);

  delay(500);

  greeni = 255;

  analogWrite(green, greeni);

  analogWrite(red, 255);

  analogWrite(blue, 255);

  analogWrite(green, 255);

}

void loop()

{

  if(TinyWireS.available() > 0){

   val = TinyWireS.receive();

  }

  ///////////////////////////////////////////////////

   if(val == 2){

   

    if (redi > 0){

      redi--;

    }

    else {

      redi = 0;

    }

    if (bluei < 255){

      bluei++;

    }

    else {

      bluei = 255;

    }

    if (greeni < 255){

      greeni++;

    }

    else {

      greeni = 255;

    }

    analogWrite(red, redi);

    analogWrite(blue, bluei);

    analogWrite(green, greeni);

    }

//////////////////////////////////

    else if(val == 1){

   

    if (bluei > 0){

      bluei--;

    }

    else {

      bluei = 0;

    }

    if (redi < 255){

      redi++;

    }

    else {

      redi = 255;

    }

    if (greeni < 255){

      greeni++;

    }

    else {

      greeni = 255;

    }

    analogWrite(red, redi);

    analogWrite(blue, bluei);

    analogWrite(green, greeni);

    }

////////////////////////////////////////////////////////////

  else {

    redi = 0;

    analogWrite(red, redi);

    delay(500);

    redi = 255;

    analogWrite(red, redi);

    delay(490);

   

  }

delay(5);

    }