aboutsummaryrefslogtreecommitdiff
path: root/digital-driver-board/firmware/firmware.ino
blob: c024bbffdcb1b48033e7685f93544b8bf3fda97a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
////////////////////////////////////////////////////////////////////////////////////
//       Digital Photoreactor Controller Slave Program Controlled by I2C          //
//                    Run this slave program on the AtTiny85.                     //      
////////////////////////////////////////////////////////////////////////////////////

#include "TinyWireS.h"                  // wrapper class for I2C slave routines   https://github.com/rambo/TinyWire/tree/master

#define I2C_SLAVE_ADDR  0x26            // I2C slave address (38, 0x26), 

uint8_t master_data[1];               // global buffer to store data sent from the master.
uint8_t master_bytes;                  // global variable to number of bytes sent from the master.

const int LEDPin = 4;                  //Arduino pin 4, pin 3 on AtTiny85 pinout
const int FanPin = 1;                  //Arduino pin 1, pin 6 on AtTiny85 pinout
const int BlinkPin = 3;                //Arduino pin 3, pin 2 on AtTiny85 pinout

void setup()
{
  // initialize the TinyWireS and usiTwiSlave libraries
  TinyWireS.begin(I2C_SLAVE_ADDR);      // init I2C Slave mode

  // register the onReceive() callback function
  TinyWireS.onReceive(receiveEvent);
  
  pinMode(LEDPin, OUTPUT);   // set pin for LED PWM
  pinMode(FanPin, OUTPUT);   // set pin for Fan PWM
  pinMode(BlinkPin, OUTPUT); // set pin to blink LED
  
  TCCR0B = TCCR0B & 0b11111000 | 0b001; // sets internal timer of AtTiny85 to 31250 Hz for fast PWM for controlling fan speed
}

// Gets called when the ATtiny85 receives an I2C write slave request
void receiveEvent(uint8_t num_bytes)
{
  uint8_t i;
  
  // save the number of bytes sent from the master
  master_bytes = num_bytes;

  // store the data from the master into the data buffer
  for (i = 0; i < master_bytes; i++)
    master_data[i] = TinyWireS.receive();

  // set LED and Fan PWM duty to random byte values recieved from the master
  analogWrite(FanPin, master_data[0]);
  analogWrite(LEDPin, master_data[1]);

  // blink LED whenever data is successfuly recieved
  digitalWrite(BlinkPin, HIGH);
  delay(10);
  digitalWrite(BlinkPin, LOW);

}

void loop()
{
  
}