diff options
author | Philip Lampkin <plampkin@chem.wisc.edu> | 2020-12-30 00:10:01 -0600 |
---|---|---|
committer | Philip Lampkin <plampkin@chem.wisc.edu> | 2020-12-30 00:10:01 -0600 |
commit | c8e4120b3cc117b9b35cea99d519de23ecb9bad0 (patch) | |
tree | cee93ff81191228d45bc83d2873963891af13f2f | |
parent | df80df8ba6775f6a4897624822ed622adf45be19 (diff) |
Wrote PR controller sketches for slave and master
2 files changed, 99 insertions, 0 deletions
diff --git a/Digital Photoreactor Controller Firmware/Master_Photoreactor_Controller/Master_Photoreactor_Controller.ino b/Digital Photoreactor Controller Firmware/Master_Photoreactor_Controller/Master_Photoreactor_Controller.ino new file mode 100644 index 0000000..8c2ffa4 --- /dev/null +++ b/Digital Photoreactor Controller Firmware/Master_Photoreactor_Controller/Master_Photoreactor_Controller.ino @@ -0,0 +1,40 @@ +//////////////////////////////////////////////////////////////////////////////////// +// Digital Photoreactor Controller Slave Program Controlled by I2C // +// Run this slave program on the Arduino Uno or Rasberry Pi. // +//////////////////////////////////////////////////////////////////////////////////// + +#include <Wire.h> + +#define I2C_SLAVE_ADDR 0x26 // I2C slave address (38, 0x26) + +void setup() +{ + // init the Wire object + Wire.begin(); + + // wait for slave to finish any init sequence + delay(2000); +} + +void loop() +{ + + //generate buffer containing data to send via I2C + uint8_t buf[1]; + buf[0] = 25; // adjusting this byte changes fan speed. Can be adjusted from 0 to 256. + buf[1] = 1; // adjusting this byte changes LED intensity. Can be adjusted from 0 to 256. + + // send buffer + Wire.beginTransmission(I2C_SLAVE_ADDR); + Wire.write( buf, 2); + Wire.endTransmission(); + + // delay 20 milliseconds to accomodate slave onReceive() + delay(20); + + /////////////////////// WHY DO I NEED THIS PART?////////////////////////// + Wire.requestFrom(I2C_SLAVE_ADDR, 1); + + // delay 1 second so user can watch results + delay(1000); +} diff --git a/Digital Photoreactor Controller Firmware/Slave_Photoreactor_Controller/Slave_Photoreactor_Controller.ino b/Digital Photoreactor Controller Firmware/Slave_Photoreactor_Controller/Slave_Photoreactor_Controller.ino new file mode 100644 index 0000000..8f7acbb --- /dev/null +++ b/Digital Photoreactor Controller Firmware/Slave_Photoreactor_Controller/Slave_Photoreactor_Controller.ino @@ -0,0 +1,59 @@ +//////////////////////////////////////////////////////////////////////////////////// +// Digital Photoreactor Controller Slave Program Controlled by I2C // +// Run this slave program on the AtTiny85. // +//////////////////////////////////////////////////////////////////////////////////// + +#include "TinyWireS.h" // wrapper class for I2C slave routines + +#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); + delay(10); + +} + +void loop() +{ + +} |